Simple helper class for DateTime?
-
Hey guys, dunno what I am doing wrong here... seems so simple. Helper Class:
Class Paramaters
{
Public DateTime time = DateTime.Now;
public string formatDate = "MM/dd";
}The Call:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
}Can anyone shed some light on what exactly I am doing wrong? Thank you.
-
Hey guys, dunno what I am doing wrong here... seems so simple. Helper Class:
Class Paramaters
{
Public DateTime time = DateTime.Now;
public string formatDate = "MM/dd";
}The Call:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
}Can anyone shed some light on what exactly I am doing wrong? Thank you.
taking a punt, since you dont say what error(s) you are getting .. shouldnt Class Parameters and/or its members be 'public static' ? (else, iirc, you have to create an instance of it) 'g'
-
Hey guys, dunno what I am doing wrong here... seems so simple. Helper Class:
Class Paramaters
{
Public DateTime time = DateTime.Now;
public string formatDate = "MM/dd";
}The Call:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
}Can anyone shed some light on what exactly I am doing wrong? Thank you.
Wrong?? That would mean we need to know what you expected it to do. Whether it works right or wrong is relative to the expected result. If you wanted the code to fail a certain way, well, then it would be working correctly, now wouldn't it?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Wrong?? That would mean we need to know what you expected it to do. Whether it works right or wrong is relative to the expected result. If you wanted the code to fail a certain way, well, then it would be working correctly, now wouldn't it?
A guide to posting questions on CodeProject[^]
Dave KreskowiakLOL I understand the concerns. What I have is a huge list of if statements associated with one of my buttons. They all will return this date in some way. Instead of having a million copies of the same code, I want to reference that class so when I change something within that class, it applies to all instances of the code. The error I am getting is An object reference is require for non-static field, method, or property. Copying the code
DateTime time = DateTime.Now;
string formatDate = "MM/dd";textBox1.Text = time.ToString(formatDate);
In each and every if statement seems like it would be a big waste of code and space. I have about 20 buttons or so that would use it, each with 2 if statements attached. I want to just reference that code in a helper class and just have to type something like this:
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
which would return "03/11" in the text box, for today's date. I hope this helps clarify my intentions, thanks.
-
Hey guys, dunno what I am doing wrong here... seems so simple. Helper Class:
Class Paramaters
{
Public DateTime time = DateTime.Now;
public string formatDate = "MM/dd";
}The Call:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
}Can anyone shed some light on what exactly I am doing wrong? Thank you.
If you want to use
Parameters
without creating an instance then the methods/properties/fields you are exposing need to be static. If they are all static, then the class should be static too. You will have a problem withtime
if you make it static as it will only be set the first time you call it. This will fix your problems:public static class Paramaters
{
public const string MonthDayFormat = "MM/dd";
public static DateTime Time
{
get { return DateTime.Now; }
}
}textBox1.Text = Paramaters.Time.ToString(Paramaters.MonthDayFormat);
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
If you want to use
Parameters
without creating an instance then the methods/properties/fields you are exposing need to be static. If they are all static, then the class should be static too. You will have a problem withtime
if you make it static as it will only be set the first time you call it. This will fix your problems:public static class Paramaters
{
public const string MonthDayFormat = "MM/dd";
public static DateTime Time
{
get { return DateTime.Now; }
}
}textBox1.Text = Paramaters.Time.ToString(Paramaters.MonthDayFormat);
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Worked exactly as intended Dave! Thank you for clearing that up for me. Time was a misleading description IN this case. However, this was also going to serve as my template for the time stamp portion of my next step. How can I make Time (one that actually displays the current system time), dynamic? So if I push a button every 5 seconds, it updates the field I am pointing to to the current time? Gosh not only do I sound like a noob, I am a babbling, confusing... noob. :) Edit: Could I have it return my result into the box (or in my literal case, a log.txt file) and then turn around and reset that value?
-
Worked exactly as intended Dave! Thank you for clearing that up for me. Time was a misleading description IN this case. However, this was also going to serve as my template for the time stamp portion of my next step. How can I make Time (one that actually displays the current system time), dynamic? So if I push a button every 5 seconds, it updates the field I am pointing to to the current time? Gosh not only do I sound like a noob, I am a babbling, confusing... noob. :) Edit: Could I have it return my result into the box (or in my literal case, a log.txt file) and then turn around and reset that value?
No problem! By using a property as in the code I posted, or using
DateTime.Now
each time directly rather than a cached field as you were attempting previously. Edit: If you want to make it a little less wordy to access you could just create a static property or method that does it all for you:public static class DateTimeHelpers
{
private const string MonthDayFormat = "MM/dd";public static string NowMonthDayString { get { return DateTime.Now.ToString(MonthDayFormat); } }
}
textBox1.Text = DateTimeHelpers.NowMonthDayString;
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
No problem! By using a property as in the code I posted, or using
DateTime.Now
each time directly rather than a cached field as you were attempting previously. Edit: If you want to make it a little less wordy to access you could just create a static property or method that does it all for you:public static class DateTimeHelpers
{
private const string MonthDayFormat = "MM/dd";public static string NowMonthDayString { get { return DateTime.Now.ToString(MonthDayFormat); } }
}
textBox1.Text = DateTimeHelpers.NowMonthDayString;
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Sorry Dave, didn't realize you had already prevented that from becoming a problem. I just added another private const string to display hours and minutes how I desired, and it works flawlessly, you sir, are a gentleman and a scholar :) :) :) :)
-
Sorry Dave, didn't realize you had already prevented that from becoming a problem. I just added another private const string to display hours and minutes how I desired, and it works flawlessly, you sir, are a gentleman and a scholar :) :) :) :)
You're welcome :beer:
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hey guys, dunno what I am doing wrong here... seems so simple. Helper Class:
Class Paramaters
{
Public DateTime time = DateTime.Now;
public string formatDate = "MM/dd";
}The Call:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Parameters.time.ToString(Parameters.formatDate);
}Can anyone shed some light on what exactly I am doing wrong? Thank you.
Class Paramaters Parameters.time.ToString(Parameters.formatDate); And also - or you should instintiate the Parameter class - or make the members static. I would also advice in using properties instead of members directly.