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

发布于 2024-12-21 05:15:06 字数 1524 浏览 4 评论 0原文

我的单选按钮有问题。我正在做的是创建一个客户对象,同时我想为客户基类中的每个单选按钮设置一个 bool 属性。我收到的错误消息是“StackOverflowException Was Unhandeled”。错误指向此“IsClient = value;”在 CustomerType 类中。

这是我创建 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; }
}

}

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. The error message I get is "StackOverflowException Was Unhandeled". The error is pointing at this "IsClient = value;" in the CustomerType class.

Here is where I create the Customer object (inside 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; }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人哭 2024-12-28 05:15:06

CustomerType 中的所有属性都是递归的 - 它们会破坏堆栈。

看一下:

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

当您尝试获取 IsClient 属性的值时,您会尝试获取 IsClient 属性的值。然后尝试获取 IsClient 属性的值...

要么将这些实现为自动实现的属性:

public bool IsClient
{
    get; set;
}

要么有一个适当的支持字段:

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

All the properties in your CustomerType are recursive - they blow the stack.

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. Which then tries to get the value of the IsClient property ...

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; }
}
小苏打饼 2024-12-28 05:15:06

属性是一个函数。您编写的内容相当于编写:

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

错误代码:

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

正确代码:

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

或者在 C# 3.0 或更高版本中,您可以使用自动实现的简单属性:

public bool IsClient { get; set; }

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:

public bool IsClient { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文