简体   繁体   English

C#从单选按钮设置bool属性

[英]C# setting bool property from radio button

I have a problem with my radio buttons. 我的单选按钮有问题。 What I'm doing is I create a customer object and at the same time I want to set one bool property of each radio button in the customer base class. 我正在做的是创建一个客户对象,同时我想在客户基础类中为每个单选按钮设置一个bool属性。 The error message I get is "StackOverflowException Was Unhandeled". 我收到的错误消息是“未处理StackOverflowException”。 The error is pointing at this "IsClient = value;" 错误指向此“ IsClient = value;”。 in the CustomerType class. 在CustomerType类中。

Here is where I create the Customer object (inside CustomerForm.cs) 这是我创建Customer对象的地方(在CustomerForm.cs内部)

m_customer = new Customer(radioClient.Checked, radioProspect.Checked, radioCompany.Checked, radioPrivate.Checked);


public class Customer : CustomerType
{
private Contact m_contact;
private string m_id;

public Customer()
{
    m_id = string.Empty;
}

public Customer(bool client, bool prospect, bool company, bool priv)
{
    base.IsClient = client;
    base.IsProspect = prospect;
    base.IsCompany = company;
    base.IsPrivate = priv;
    m_id = string.Empty;
}

public Customer(Contact contactData)
{ m_contact = contactData; }

public Customer(string id, Contact contact)
{
    m_id = id;
    m_contact = contact;
}

public Contact ContactData
{
    get { return m_contact; }
    set {
        if (value != null)
            m_contact = value;  
    }
}

public string Id
{
    get { return m_id; }
    set { m_id = value; }
}

public override string ToString()
{
    return m_contact.ToString();
}
}


public class CustomerType 
{

public bool IsClient
{
    get { return IsClient; }
    set { IsClient = value; }
}

public bool IsCompany
{
    get { return IsCompany; }
    set { IsCompany = value; }
}

public bool IsPrivate
{
    get { return IsPrivate; }
    set { IsPrivate = value; }
}

public bool IsProspect
{
    get { return IsProspect; }
    set { IsProspect = value; }
}

}

All the properties in your CustomerType are recursive - they blow the stack. CustomerType中的所有属性都是递归的-它们使堆栈崩溃。

Take a look at this: 看看这个:

public bool IsClient
{
    get { return IsClient; }
    set { IsClient = value; }
}

When you try to get the value of the IsClient property, you then try to get the value of the IsClient property. 当您尝试获取IsClient属性的值时,然后尝试获取IsClient属性的值。 Which then tries to get the value of the IsClient property ... 然后尝试获取IsClient属性的值...

Either implement these as automatically implemented properties: 可以将这些实现为自动实现的属性:

public bool IsClient
{
    get; set;
}

Or have a proper backing field: 或有适当的支持领域:

private bool isClient;
public bool IsClient
{
    get { return isClient; }
    set { isClient = value; }
}

A property is a function. 属性是一个函数。 What you wrote is the equivalent of writing: 您写的内容等同于写作:

public void DoSomething()
{
    DoSomething();  // infinite recursion
}

Error code: 错误代码:

public bool IsClient
{
    get { return IsClient; }
    set { IsClient = value; }
}

Proper code: 正确的代码:

public bool IsClient
{
    get { return _isClient; }
    set { _isClient = value; }
}
private bool _isClient;

Or in C# 3.0 or later, you can use auto implemented properties for simple ones: 或者在C#3.0或更高版本中,您可以对简单属性使用自动实现的属性:

public bool IsClient { get; set; }

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

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