public class ElapsedTime
{
public int hours;
public int minutes;
public void ElapsedTime(int h, int m)
{
hours = h;
minutes = m;
}
}
from another event i am doing this:
ElapsedTime BeginningTime = new ElapsedTime();
how would i initialize the h and m?
when i try to do this: BeginningTime.ElapsedTime(7, 7);
it gives me this error:
Error 1 ‘ElapsedTime’: member names
cannot be the same as their enclosing
type
all i want is a class with a constructor that accepts initializing values. and i want to be able to call it.
UPDATE:
now i have :
public class ElapsedTime
{
private int hours;
private int minutes;
public ElapsedTime(int h, int m)
{
hours = h;
minutes = m;
}
}
its giving me that same erorr on public ElapsedTime(int h, int m)
The constructor of a class needs to be of the same name of the class itself. Now, the parameters
handmmake it a parameterized constructor. In order to initialize a instance of this class, you will need to specify values to those.Calling the constructor through a variable is not doable:
In inroder to be a class initializer, the constructor has no return type (neither
void).Of course, if you want a property which gives you the values under a certain format, you will need to expose a property which will provide you with the values you instantiated your instance:
Did I understand your question correctly?