2011-12-14 3 views
0

라디오 버튼에 문제가 있습니다. 내가하고있는 일은 고객 객체를 만들고 동시에 고객 기본 클래스에서 각 라디오 버튼의 하나의 bool 속성을 설정하려고합니다. 내가받는 오류 메시지는 "StackOverflowException 처리되지 않았습니다"입니다. 오류는이 "IsClient = value;"를 가리 킵니다. CustomerType 클래스에서. 그들은 스택을 날려 - 내가 (CustomerForm.cs 내부에) 당신의 CustomerType라디오 버튼의 C# 설정 bool 속성

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

모든 속성을 고객 객체를 생성 재귀 어디에 여기

이다.

이것 좀 봐 :

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

당신이 IsClient property의 값을 취득하려고하면 다음 IsClient property의 값을 취득하려고합니다. 어떤은 ... IsClient 속성 값을 얻을 수

을하려고 하나이 자동으로 구현 특성 구현 :

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

OP가 자동 속성을 올바르게 구현해야합니까? ... –

+1

@ jameslewis - 그건 하나의 옵션입니다. – Oded

+0

LOL이 분명히 아닙니다! 건배 – Craig

1

프로퍼티는 다음과 같습니다 적절한 백업 필드를

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; } 
관련 문제