2011-12-14 4 views
0

ラジオボタンに問題があります。私がやっていることは、顧客オブジェクトを作成すると同時に、顧客基本クラスの各ラジオボタンの1つのboolプロパティを設定することです。私が得るエラーメッセージは、 "StackOverflowExceptionは手作業ではありませんでした"です。エラーは、この "IsClient = value;"を指しています。 CustomerTypeクラスに追加します。私は(CustomerForm.cs内側)Customerオブジェクトを作成するのはここラジオボタンからboolプロパティを設定するC#

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; } 
} 

} 

答えて

3

あるごCustomerTypeのすべてのプロパティは、再帰的である - 彼らはスタックを吹きます。

は、このを見てみましょう:あなたはIsClientプロパティの値を取得しようとすると

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

、あなたはその後、IsClientプロパティの値を取得しよう。さ

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

これを修正するにはOPはおそらく自動プロパティを実装しようとする必要がありますか? ... –

+1

@jameslewis - それは一つの選択肢です。 – Oded

+0

LOLは明らかにそうではありません!乾杯! – Craig

1

プロパティを:どのその後、どちらか... IsClientプロパティの値を取得するために

をしようとするこれらのように自動的に実装プロパティを実装します。

public bool IsClient 
{ 
    get; set; 
} 

または適切なバッキングフィールドを持っているに機能。あなたが書いたことを書いていると等価である:

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; } 
関連する問題