public string getTime()
{
StringBuilder RetString = new StringBuilder();
RetString.Append(Hours.ToString().PadLeft(2, '0') + "-" + Min.ToString().PadLeft(2, '0') + "-" + AMPM.PadLeft(2, '0'));
return RetString.ToString();
}
I’m attempting to return a time (in string format) back to a form string variable using this. I have the user setup to enter a time and the time is stored in this class, and all im doing now is bringing that data to the form im working in. I’ve verified that the data is going into the class and being stored in the variables.
The error i get is :

Not really sure what the problem is.
I have the variables declared of course
private int Hours;
private int Min;
private string AMPM;
I have get sets setup:
public int _hours
{
get
{
return Hours;
}
set
{
if (value <= 12 && value >= 1)
{
Hours = value;
}
else
Hours = 0;
}
}
public int _min
{
get
{
return Min;
}
set
{
if (value <= 59 && value >= 1)
{
Min = value;
}
else
Min = 0;
}
}
public string ampm
{
get
{
return AMPM;
}
set
{
if (AMPM == "AM" || AMPM == "PM")
{
AMPM = value;
}
else
AMPM = "";
}
}
and my call to the class:
string timefromclass;
timefromclass = timec.getTime();
The only thing that I think it could be would be my call to the class where i store the time is
C_Time time = new C_Time();
but in the other form i setup another one as well.
C_Time timec = new C_Time();
Possibly i’m trying to retrieve the time from an empty instance?
EDIT:
public void setTime(int hours, int min, string aMPM)
{
Hours = hours;
Min = min;
AMPM = aMPM;
That’s really the only place I assigned that variable anything.
From you code, I assume your
AMPMfield isnull.You do not seem to instantiate it anywhere apart from the setter, which you are not calling in your code.
Initializing it should solve the issue: