C #: beginners CLASS constructor issues

public class ElapsedTime
{
    public int hours;
    public int minutes;
    public void ElapsedTime(int h, int m)
    {
        hours = h;
        minutes = m;
    }
}

from another event I'm doing:

    ElapsedTime BeginningTime = new ElapsedTime();

how would i initialize hand m?

when i try to do this: BeginningTime.ElapsedTime(7, 7);

he gives me this error:

Error 1 'ElapsedTime': member names cannot be the same as their type

all i want is a constructor class that takes initialization values. and I want it to be called.

UPDATE:

Now I have:

 public class ElapsedTime
        {
            private int hours;
            private int minutes;
            public ElapsedTime(int h, int m)
            {
                hours = h;
                minutes = m;
            }
        }

he gives me the same erorr on public ElapsedTime(int h, int m)

+3
source share
7 answers

. h m . , .

ElapsedTime beginningTime = new ElapsedTime(7, 7);

:

ElapsedTime beginningTime;
beginningTime.ElapsedTime(7, 7); // This will cause an error, as your constructor isn't a method...

inroder ( void).

, , , , , :

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

?

+2

void. , , , .

public class ElapsedTime
{
    public int hours;
    public int minutes;
    public ElapsedTime(int h, int m)
    {
        hours = h;
        minutes = m;
    }
}

.

ElapsedTime BeginningTime = new ElapsedTime(7, 7);
+6

. . .

public class ElapsedTime {
   private int hours;
   private int minutes;

   public ElapsedTime(int h, int m) {
     hours = h;
     minutes = m;
   }
}

,

ElapsedTime BeginningTime = new ElapsedTime(7, 7);
+2

, , ( void)
:

ElapsedTime BeginningTime = new ElapsedTime(7,7);  

.
, initial.

+1
ElapsedTime BeginningTime = new ElapsedTime(7,7);

of the course void ElapsedTime()

+1

- ElapsedTime(). , . , .

void . .ctor.

+1

, , , h m . :

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;
    }
}

, ElapsedTime:

ElapsedTime BeginningTime = new ElapsedTime(7, 7);

, ( , Fields Properties, :

Public int Hours
{
    set {
        hours = value;
    }
    get {
        return hours;
    }
}

Public int Minutes
{
    set {
        minutes = value;
    }
    get {
        return minutes;
    }
}

:

ElapsedTime BeginningTime = new ElapsedTime(7, 7);
BeginningTime.Hours = 7;
BeginningTime.Minutes = 7;

ElapsedTime BeginningTime = new ElapsedTime(7, 7);

:

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

Source: https://habr.com/ru/post/1770391/


All Articles