c#: beginners CLASS constructor problems
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)
Leave out void
. By having it there, you're declaring it is a method, not a constructor.
public class ElapsedTime
{
public int hours;
public int minutes;
public ElapsedTime(int h, int m)
{
hours = h;
minutes = m;
}
}
Then to create an instance is just a matter of calling the constructor.
ElapsedTime BeginningTime = new ElapsedTime(7, 7);
The constructor of a class needs to be of the same name of the class itself. Now, the parameters h
and m
make it a parameterized constructor. In order to initialize a instance of this class, you will need to specify values to those.
ElapsedTime beginningTime = new ElapsedTime(7, 7);
Calling the constructor through a variable is not doable:
ElapsedTime beginningTime;
beginningTime.ElapsedTime(7, 7); // This will cause an error, as your constructor isn't a method...
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:
public class ElapsedTime {
public ElapsedTime(int h, int m) {
Hours = h;
Minutes = m;
}
public int Hours { get; private set; }
public int Minutes { get; private set; }
public string ElapsedTime {
get {
return string.Format("{0}:{1}", Hours, Minutes);
}
}
}
Did I understand your question correctly?
Your constructor is wrong. Constructors don't have a return type. It needs to look like this
public class ElapsedTime {
private int hours;
private int minutes;
public ElapsedTime(int h, int m) {
hours = h;
minutes = m;
}
}
and then you can create a new instance like this
ElapsedTime BeginningTime = new ElapsedTime(7, 7);
ElapsedTime BeginningTime = new ElapsedTime(7,7);
ofcourse remove void from ElapsedTime()
The member being referred to is the method ElapsedTime()
. Specifically, you have not created a constructor. You have created a method with the name that the constructor would have.
Try removing the void
in front of your method definition. That will make it a .ctor.
The way you have things set up indicates that you would like to pass some values h
and m
to the constructor when you create the object. You should do this:
public class ElapsedTime
{
// you should probably makes these private
private int hours;
private int minutes;
// I took out the "void" as it is not correct
public ElapsedTime(int h, int m)
{
hours = h;
minutes = m;
}
}
And then you would do this to instantiate an object of class ElapsedTime
:
ElapsedTime BeginningTime = new ElapsedTime(7, 7);
If you wanted to do things the way that you have it now (where you set the values of hours and minutes after you create a new instance of the object you should expose those Fields
through Properties
, like so:
Public int Hours
{
set {
hours = value;
}
get {
return hours;
}
}
Public int Minutes
{
set {
minutes = value;
}
get {
return minutes;
}
}
And then you could use these as follows:
ElapsedTime BeginningTime = new ElapsedTime(7, 7);
BeginningTime.Hours = 7;
BeginningTime.Minutes = 7;
The above is equivalent to ElapsedTime BeginningTime = new ElapsedTime(7, 7);
so you'd end up with this in your class file:
public class ElapsedTime
{
// you should probably makes these private
private int hours;
private int minutes;
// I took out the "void" as it is not correct
public ElapsedTime(int h, int m)
{
hours = h;
minutes = m;
}
Public int Hours
{
set {
hours = value;
}
get {
return hours;
}
}
Public int Minutes
{
set {
minutes = value;
}
get {
return minutes;
}
}
}
You meant the method would be a constructor, so it should not contain a return type (delete the void
)
Now you can use:
ElapsedTime BeginningTime = new ElapsedTime(7,7);
You can't call the constructor after the class has been created.
Constructors are for giving the class it's initial
values.
精彩评论