C# 从单选按钮设置 bool 属性
我的单选按钮有问题。我正在做的是创建一个客户对象,同时我想为客户基类中的每个单选按钮设置一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CustomerType
中的所有属性都是递归的 - 它们会破坏堆栈。看一下:
当您尝试获取
IsClient
属性的值时,您会尝试获取IsClient
属性的值。然后尝试获取IsClient
属性的值...要么将这些实现为自动实现的属性:
要么有一个适当的支持字段:
All the properties in your
CustomerType
are recursive - they blow the stack.Take a look at this:
When you try to get the value of the
IsClient
property, you then try to get the value of theIsClient
property. Which then tries to get the value of theIsClient
property ...Either implement these as automatically implemented properties:
Or have a proper backing field:
属性是一个函数。您编写的内容相当于编写:
错误代码:
正确代码:
或者在 C# 3.0 或更高版本中,您可以使用自动实现的简单属性:
A property is a function. What you wrote is the equivalent of writing:
Error code:
Proper code:
Or in C# 3.0 or later, you can use auto implemented properties for simple ones: