2016-12-05 2 views
-1

私はコンストラクタを? これは次のようにコンストラクタを呼び出す方法があります、私のクラスのメインコードでc#を代入することによってコンストラクタを呼び出す

public class Person 
{ 
public string sName; 
public string sAge; 

public Person (string sPerson) 
{ 
//in sPerson i have Name and Age separate from , 
sName=sPerson.split(',')[0]; 
sAge=sPerson.split(',')[1]; 
} 

私は私のオブジェクトを付加価値化必要がありますが、私は、コンストラクタを呼び出すことはできませんでしょうか?

Person myObject = newPerson(); 
myPerson = sPeson; 

}

+0

あなたは '人を意味しますかmyObject = myPerson; '? – RandomStranger

+0

'newPerson'はタイプミス(' new Person')ですか?パラメータのないコンストラクタはありません。 'sPeson'と' myPerson'とは何ですか?ワットは*私のオブジェクト*を意味しますか? –

答えて

0

はい、あなたは別のPersonを取るcopy-constructorを提供することができます。

public Person (Person otherPerson) 
{ 
    sName = otherPerson.sName; 
    sAge = otherPerson.sAge; 
} 

あなたはこの方法でそれを呼び出すことができます。

Person myObject = new Person(sPerson); 
関連する問題