tags:

views:

96

answers:

7
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)

+5  A: 

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);
Jeff M
+1, didn't even notice the void there.
Brandon
now i am getting this error on public ElapsedTime(int h, int m)
i am a girl
@jenny- hi again (-: What error do you get?
Oren A
@jenny, what error? Jeff's answer should compile.
Brandon
There shouldn't be an error the way this is written. You should note that this class contains no additional methods (besides `ToString()` for example) and one constructor that takes 2 arguments.
Jeff M
Error 1 'ElapsedTime': member names cannot be the same as their enclosing type
i am a girl
@jenny - Do you call it as Jeff showed i.e. ElapsedTime BeginningTime = new ElapsedTime(7, 7); ?
Oren A
@Oren: I think jenny forgot to save the changes before compiling because it's working now. :)
Jeff M
(-: oh, missed that... Great
Oren A
+1  A: 

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.

Oren A
+1  A: 
ElapsedTime BeginningTime = new ElapsedTime(7,7);

ofcourse remove void from ElapsedTime()

mumtaz
+1  A: 

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);
Brian Rasmussen
+1  A: 

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.

Greg D
now i am getting this error on public ElapsedTime(int h, int m)
i am a girl
+1  A: 

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;
        }
    }
}
samandmoore
+1  A: 

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?

Will Marcouiller