Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing stringbuilder put time into string (class) C#
    text
    copied!<pre><code> 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(); } </code></pre> <p>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. </p> <p>The error i get is :</p> <p><img src="https://i.stack.imgur.com/Z9oWf.png" alt="CSharp_Null"></p> <p>Not really sure what the problem is. I have the variables declared of course</p> <pre><code> private int Hours; private int Min; private string AMPM; </code></pre> <p>I have get sets setup:</p> <pre><code>public int _hours { get { return Hours; } set { if (value &lt;= 12 &amp;&amp; value &gt;= 1) { Hours = value; } else Hours = 0; } } public int _min { get { return Min; } set { if (value &lt;= 59 &amp;&amp; value &gt;= 1) { Min = value; } else Min = 0; } } public string ampm { get { return AMPM; } set { if (AMPM == "AM" || AMPM == "PM") { AMPM = value; } else AMPM = ""; } } </code></pre> <p>and my call to the class:</p> <pre><code>string timefromclass; timefromclass = timec.getTime(); </code></pre> <p>The only thing that I think it could be would be my call to the class where i store the time is </p> <pre><code>C_Time time = new C_Time(); </code></pre> <p>but in the other form i setup another one as well.</p> <pre><code>C_Time timec = new C_Time(); </code></pre> <p>Possibly i'm trying to retrieve the time from an empty instance? </p> <p>EDIT: </p> <pre><code>public void setTime(int hours, int min, string aMPM) { Hours = hours; Min = min; AMPM = aMPM; </code></pre> <p>That's really the only place I assigned that variable anything. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload