Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple helper class for DateTime?

Simple helper class for DateTime?

Scheduled Pinned Locked Moved C#
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goaty65109
    wrote on last edited by
    #1

    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.

    G D D V 4 Replies Last reply
    0
    • G Goaty65109

      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.

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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'

      1 Reply Last reply
      0
      • G Goaty65109

        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.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • D 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 Kreskowiak

          G Offline
          G Offline
          Goaty65109
          wrote on last edited by
          #4

          LOL 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.

          1 Reply Last reply
          0
          • G Goaty65109

            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.

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

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

            G 1 Reply Last reply
            0
            • D DaveyM69

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

              G Offline
              G Offline
              Goaty65109
              wrote on last edited by
              #6

              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?

              D 1 Reply Last reply
              0
              • G Goaty65109

                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?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                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)

                G 1 Reply Last reply
                0
                • D DaveyM69

                  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)

                  G Offline
                  G Offline
                  Goaty65109
                  wrote on last edited by
                  #8

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

                  D 1 Reply Last reply
                  0
                  • G Goaty65109

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

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    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)

                    1 Reply Last reply
                    0
                    • G Goaty65109

                      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.

                      V Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #10

                      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.

                      V.
                      (MQOTD Rules and previous Solutions )

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups