usage of this()

N

nicol

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*******
{
}
}
 
K

Konrad Neitzel

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
 
N

nicol

Hi nicol,

nicol wrote on 07.05.10
in microsoft.public.dotnet.languages.csharp


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

thanks but how i could define them
 
K

Konrad Neitzel

Hi nicol,

nicol wrote on 07.05.10
in microsoft.public.dotnet.languages.csharp
thanks but how i could define them

Your example simply made no sense. Just take the class:

public class Employee
{
public string ForeName;
public string LastName;

public Employee(string foreName, string lastName)
{
ForeName = foreName;
LastName = lastName
}
}

So far it makes sense. But now you want to add a standard constructor that
takes no arguments. It is possible, but you have to make something
usefull.

So for example you can do something like:
public Employee()
{
ForeName = "unknown";
LastName = "unknown";
}

But now lets extend this class a little bit more. I want to get a list of
coworker. All coworker are also employees. So we add another field:

public List<Employee> CoWorker;

We have to make sure, that the List is initialized when an instance is
created. So we can add the following line to both constructers:

CoWorker = new List<Employee>();

This brings up something, that is not that nice: Double code. Ok, it is
just one line of code, but it could become more and more ...
So now we have multiple possible solutions, to keep the code only in one
place:

1) The generic solution is simply: Pull the code out and make it to a
method.

In this example, it could result in:

private void initialize()
{
CoWorker = new List<Employee>();
}

and inside the constructors you just have the call to initialize()
(Instead of the CoWorker = new List<..>();)

2) You only have the code in one constructor and make sure, that the
constructor is always called:

public Employee()
{
ForeName = "unknown";
LastName = "unknown";
CoWorker = new List<Employee>();
}

public Employee(string foreName, string lastName)
: this()
{
ForeName = foreName;
LastName = lastName;
}

I hope that was something that could be understood.

With kind regards,

Konrad
 
N

nicol

Hi nicol,

nicol wrote on 07.05.10
in microsoft.public.dotnet.languages.csharp


Your example simply made no sense. Just take the class:

public class Employee
{
  public string ForeName;
  public string LastName;

  public Employee(string foreName, string lastName)
  {
    ForeName = foreName;
    LastName = lastName
  }

}

So far it makes sense. But now you want to add a standard constructor that  
takes no arguments. It is possible, but you have to make something  
usefull.

So for example you can do something like:
public Employee()
{
  ForeName = "unknown";
  LastName = "unknown";

}

But now lets extend this class a little bit more. I want to get a list of 
coworker. All coworker are also employees. So we add another field:

public List<Employee> CoWorker;

We have to make sure, that the List is initialized when an instance is  
created. So we can add the following line to both constructers:

CoWorker = new List<Employee>();

This brings up something, that is not that nice: Double code. Ok, it is  
just one line of code, but it could become more and more ...
So now we have multiple possible solutions, to keep the code only in one  
place:

1) The generic solution is simply: Pull the code out and make it to a  
method.

In this example, it could result in:

private void initialize()
{
  CoWorker = new List<Employee>();

}

and inside the constructors you just have the call to initialize()  
(Instead of the CoWorker = new List<..>();)

2) You only have the code in one constructor and make sure, that the  
constructor is always called:

public Employee()
{
  ForeName = "unknown";
  LastName = "unknown";
  CoWorker = new List<Employee>();

}

public Employee(string foreName, string lastName)
    : this()
{
  ForeName = foreName;
  LastName = lastName;

}

I hope that was something that could be understood.

With kind regards,

Konrad

thanks
it get a long time of u sorry
 
G

gerry

// change this
public employee(string fname, string lname):this()
{
first_name = fname;
last_name = lname;
}
public employee():this( first, lname) // error*******
{
}


// to this
public employee(string fname, string lname) // << remove :this()
{
first_name = fname;
last_name = lname;
}
public employee()
: this( "DefaultFirstName","DefaultLastName") // << call with defined
values
{
}


Hi nicol,

nicol wrote on 07.05.10
in microsoft.public.dotnet.languages.csharp


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

thanks but how i could define them
 
N

nicol

// change this
public employee(string fname, string lname):this()
{
    first_name = fname;
    last_name = lname;}

public employee():this( first, lname) // error*******
{

}

// to this
public employee(string fname, string lname)        // << remove :this()
{
    first_name = fname;
    last_name = lname;}

public employee()
: this( "DefaultFirstName","DefaultLastName")     // << call with defined
values
{

}










thanks but how i could define them

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top