简体   繁体   English

更新:将布尔默认值设置为true

[英]Updated: Setting bool default value to true

I need to find a way to set the default value of ischecked to true, I am passing it as a parameter and I don't always set it. 我需要找到一种方法将ischecked的默认值设置为true,我将其作为参数传递,但我并不总是将其设置。

ischecked is defined in my config files, I have multiple configs and in some it is defined and some it isn't. ischecked是在我的配置文件中定义的,我有多个配置,在一些定义中,有的则没有。 I won't get into why I use different configs. 我不会理解为什么我使用不同的配置。 When it isn't define it is defaulted to false but I need it to be true. 如果未定义,则默认为false,但我需要为true。

function showSaveCardControl(ischecked) { 
    $(".saveCardControl").slideDown().find("input").prop("checked", ischecked); 
}

if it hasn't been defined I need the checkbox to be checked basically but since the default is false it is unchecked. 如果尚未定义,则需要基本检查该复选框,但由于默认值为false,因此未选中。

Update: 更新:

I am passing an xml element to ac# property. 我正在将xml元素传递给ac#属性。 The xml would be where I set the value of ischecked but since I haven't set it somewhere either in the xml or once it has been turned into ac# property it is set to false. xml是我设置ischecked值的地方,但是由于我没有在xml中的某个地方设置它,或者一旦将其变成ac#属性,就将其设置为false。 I am guessing it is set as the property tries to call that xml value. 我猜它被设置为该属性尝试调用该xml值。

I have tried setting the default using an attribute. 我尝试使用属性设置默认值。

[XmlElement("EasyPaySaveCardControlChecked")]
[DefaultValue(true)]
public bool ischecked { get; set; }

I have tried setting it using a constructor. 我尝试使用构造函数设置它。

public className()
{
    ischecked = true;
}

I have also tried making all the bools nullable and the value is still being set to false 我也尝试过使所有布尔值都可为空,并且该值仍设置为false

none of these solutions work and I have no idea why. 这些解决方案均无效,我也不知道为什么。 no matter what I do the property is set to false, if it was set to undefined it would be fine but bools have to defaulted to false 无论我做什么,将属性设置为false,如果将其设置为undefined都可以,但是布尔值必须默认为false

Try something like this: 尝试这样的事情:

function showSaveCardControl(ischecked) { 
    if ('undefined' === typeof ischecked) ischecked = true;
    $(".saveCardControl").slideDown().find("input").prop("checked", ischecked);
}

this should do the trick : 这应该可以解决问题:

function showSaveCardControl(ischecked) { 
    if(typeof ischecked == 'undefined') 
       ischecked = true;

    $(".saveCardControl").slideDown().find("input").prop("checked", ischecked); 
}

Assuming the property is part of a class then just set it's default value to true in the class constructor and it will be overwritten down the line if you have logic to set the value from the config. 假设该属性是类的一部分,则只需在类构造函数中将其默认值设置为true即可,如果您有逻辑从配置中设置值,则它将被覆盖。

public class LukesClass
{
 public String whatever {get;set;}
 public bool isChecked {get;set;}

 public LukesClass()
 {
  isChecked = true;
 }
}

Try using auto property initializers (C# 6.0 and higher): 尝试使用自动属性初始化程序(C#6.0和更高版本):

public bool isRequired { get; set; } = true;

I had a single case where I needed an unspecified xml bool to import as true instead of false. 我有一种情况,我需要一个未指定的xml bool导入为true而不是false。 The above code will result in isRequired being true when either the tags specify true, or when they do not exist. 当标签指定true或不存在标签时,以上代码将导致isRequired为true。 It will only be false when tags define it as false. 仅当标签将其定义为false时,它才会为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM