Prev: FindExecutable API
Next: help me!
From: nicol on 7 May 2010 04:59 Hi I have a problem at this program a bout the usage of this() Error 1 The name 'first' does not exist in the current context Error 2 The name 'lname' does not exist in the current context using System; class Program { static void Main(string[] args) { employee first = new employee ( "peter", "young" ); employee id = new employee(); id.Id=int.Parse(Console.ReadLine()); employee salary = new employee(); salary.Salary = int.Parse(Console.ReadLine()); } } class employee { #region private fields private decimal salary; private int id; #endregion public decimal Salary { set { if (value > 0) salary = value; else { Exception ex = new Exception(" invalid id"); throw ex; } } } public int Id { set { if (value > 0) id = value; else { Exception e = new Exception("invalid number !"); throw e; } } } #region public fields public string first_name; public string last_name; #endregion; // constructurs public employee(string fname, string lname):this() { first_name = fname; last_name = lname; } public employee():this( first, lname) // error******* { } }
From: Konrad Neitzel on 7 May 2010 05:11 Hi nicol, nicol wrote on 07.05.10 in microsoft.public.dotnet.languages.csharp > Hi > I have a problem at this program a bout the usage of this() > Error 1 The name 'first' does not exist in the current context > Error 2 The name 'lname' does not exist in the current context > public employee(string fname, string lname):this() > { > first_name = fname; > last_name = lname; > } > public employee():this( first, lname) // error******* > { > } You use the ":this(...)" in constructors to call other constructors, too. But of course, you can only use parameters, that are defined. So the other way round could make sense: public employee() { // Do something } public employee(string param) : this() { // Do some more } public employee(string param1, string param2) : this(param1) { } This has the advantage, that the code inside employee() must not be copied. In your example, you are using first and lname but they are not defined. IN my example with param1 and param2, both are defined as parameters, so I can use param1 as parameter in the other call. With kind regards, Konrad -- Konrad Neitzel - neitzel(a)neitzel.de MCTS SQL Server 2008, Database Development
|
Pages: 1 Prev: FindExecutable API Next: help me! |