Click here to Skip to main content
15,394,023 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
[My idea: When i change a variable, it will be driven by the DateTime BUT not to depend by DateTime.]
So basically, DateTime will count in it's way how it pleases, but my variable will support any changes, anytime, and count from the change is set. Up or down.
I managed to make this code for CountUp.And is working perfectly, exactly how i wanted, and you are welcome to try it too, to feel it.
Now i am struggling to make the CountDown...and i only have some ideas, but not a full path how to do it in my mind. Maybe you know a solution. I will make it in the end, but with your help maybe will be faster and better.

What I have tried:

C#
public _Time()
{
}
public void Reset()
{
    StartTime = DateTime.Now;
    diference = new TimeSpan(h, m, s);
    int xa = 60;
    EndTime = new DateTime(9999, 01, 01, h * xa, m * xa, s * xa);
}

/// <summary>
/// set or get this value
/// </summary>
public int h;
/// <summary>
/// set or get this value
/// </summary>
public int m;
/// <summary>
/// set or get this value
/// </summary>
public int s;
DateTime StartTime;
TimeSpan diference;
TimeSpan elapsed;
public void CountUp()
{
    elapsed = DateTime.Now - StartTime + diference;
    //now update the values
    h = elapsed.Hours;
    m = elapsed.Minutes;
    s = elapsed.Seconds;
}

//TimeSpan unu = new TimeSpan(0, 0, 1);
//int inch = 0;
//int incm = 0;
//int incs = 0;
DateTime EndTime;
public void CountDown()
{
    //10s/10s = 0
    //
    //
    //
    elapsed = EndTime.Subtract(DateTime.Now);
    //now update the values
    h = elapsed.Hours;
    m = elapsed.Minutes;
    s = elapsed.Seconds;
}

And to use it in Form1:
C#
public Form1()
{
    InitializeComponent();
    t = new Timer(); t.Tick += new EventHandler(t_Tick); t.Start();

    //time.h = 6;
    //time.m = 59;
    //time.s = 55;
    time.Reset();
}

Timer t;
_Time time = new _Time();
void t_Tick(object sender, EventArgs e)
{
    //time.CountUp();
    time.CountDown();
    label1.Text = time.h + ":"+time.m + ":" + time.s;
}
Posted
Updated 2-Sep-19 1:41am
v2
Comments
Richard MacCutchan 2-Sep-19 6:31am
   
CountDown is just the opposite of CountUp. So instead of adding values, you would subtract them at each point that the time is supposed to change.
_Q12_ 2-Sep-19 6:34am
   
ok, present a workable code.
and check my [public void CountDown()] that i already tried, down in the code.
Richard MacCutchan 2-Sep-19 6:34am
   
Sorry, I am not here to do your work for you.
_Q12_ 2-Sep-19 6:36am
   
then this question is not for you :) It's no problem.
Richard MacCutchan 2-Sep-19 6:47am
   
Perhaps if you actually explained what is not working, rather than demanding a solution, then people would be able to offer suggestions.
BillWoodruff 2-Sep-19 10:39am
   
"ok, present a workable code." poor attitude !
phil.o 2-Sep-19 6:50am
   
When i change a variable, it will be driven by the DateTime BUT not to depend by DateTime.
What does it mean?
_Q12_ 2-Sep-19 7:08am
   
with "elapsed = EndTime.Subtract(DateTime.Now);" is counting down alright but... not how i wanted. If i set time.m = 30, is not starting from 30 to count down. Thats my problem. And that is what i mean with that idea.
phil.o 2-Sep-19 7:19am
   
At the beginning, set EndTime to DateTime.Now.AddMinutes(30). Then at each step you can get the remaining time with EndTime - DateTime.Now. You could also use a Stopwatch component if you need more precision on your time measurements.
Anyway I would get rid of the _Time class you defined; it just adds some unnecessary complexity to something that can be handled with genuine DateTime and TimeSpan structures provided by the framework.
_Q12_ 2-Sep-19 7:27am
   
i think this is a better explanation (by comparison)
CountUp() is working excelent ! And by that i mean, if i set time.s or time.m to 44, the time will start from that value, counting up, corectly and as a clock (and not to 10000000).
This functionality i want it for CountDown. If i set time.s or time.m to 12, it will count it down, as a clock (not to 1000000) and from that value i set it.
Right now is just doing it VERY wrong with the code i have.
_Q12_ 2-Sep-19 7:30am
   
interesting suggestion and i will try it now

As I said in my comment, try to stick to genuine structures and classes provided by the framework:
DateTime Struct[^]
TimeSpan Struct[^]
Stopwatch Class[^] (optionally)

Once you have clearly understood what these objects provide and how to use them, it's just a matter of mathematical computing:
- couting up: set the start time to DateTime.Now and measure the difference (DateTime.Now - startTime);
- counting down: set the end time to DateTime.Now.AddMinutes(yourDelta) and measure the difference (endTime - DateTime.Now).

The key concept here is that subtracting two DateTime values results in a TimeSpan value, which holds all information you need (and that you tried to mimic in your unnecessary _Time class).
   
Comments
_Q12_ 2-Sep-19 8:07am
   
        public void Reset()
        {
            StartTime = DateTime.Now;
            diference = new TimeSpan(h, m, s);

            //int xa = 60;
            //EndTime = new DateTime(9999, 01, 01, 0, 0, 0);

            // HERE Endtime is updating normally 
            EndTime = DateTime.Now.AddHours(h);
            EndTime = DateTime.Now.AddMinutes(m);
            EndTime = DateTime.Now.AddSeconds(s);
        }
        public void CountDown()
        {
            // HERE Endtime is Reseted to DateTime.Now and not keeping the set values from before. 
            elapsed = EndTime - DateTime.Now;


            //elapsed = EndTime.Subtract(DateTime.Now);
            //now update the values
            h = elapsed.Hours;
            m = elapsed.Minutes;
            s = elapsed.Seconds;
        }
//And in Form1
        public Form1()
        {
            InitializeComponent();
            t = new Timer(); t.Tick += new EventHandler(t_Tick); t.Start();


            time.h = 9;
            time.m = 9;
            time.s = 9;
            time.Reset();
        }

        Timer t;
        _Time time = new _Time();
        void t_Tick(object sender, EventArgs e)
        {
            time.CountDown();
            label1.Text = time.h + ":"+time.m + ":" + time.s;
        }
_Q12_ 2-Sep-19 8:16am
   
// HERE Endtime is Reseted to DateTime.Now and not keeping the set values from before.
elapsed = EndTime - DateTime.Now;
phil.o 2-Sep-19 8:27am
   
You did not get rid of the ugly _Time class.
Moreover, try to debug these lines:
EndTime = DateTime.Now.AddHours(h);
EndTime = DateTime.Now.AddMinutes(m);
EndTime = DateTime.Now.AddSeconds(s);
You will find out that you overwrite the value at each line; meaning that EndTime will only get the value DateTime.Now.AddSeconds(s).
_Q12_ 2-Sep-19 9:10am
   
Da, here i made a mistake not seeing that value change. My bad.
I correct it now with this:
        public void Reset()
        {
            TimeSpan h66t = new TimeSpan(h, m, s);
            EndTime = DateTime.Now.Add(h66t);
        }
        DateTime EndTime;
        public void CountDown()
        {
            elapsed = EndTime - DateTime.Now;
        }

I cleanned the code to make it visible.
Right now we managed to SET EndTime with the corect values, and count down SOMETHING. But, i need to count down from those values i set in the variables. I really dont see how i can do it. Sorry for this but im blind. And i do apreciate your help very much.
- Another problem is when it reaches 0:0:0, it does not decrement to 23:59:59 but to 0:0:-1 which is really very bad.
phil.o 2-Sep-19 9:21am
   
"those values i set in the variables"
I do not know what "those values" nor "the variables" are.

"Another problem is when it reaches 0:0:0, it does not decrement to 23:59:59 but to 0:0:-1 which is really very bad"
Why? It is mathematically correct. If you want a specific logic to be applied, you have to implement it.
BillWoodruff 2-Sep-19 10:37am
   
+5 good example of educating the OP rather than writing code for them.
phil.o 2-Sep-19 10:44am
   
Thank you!
Two questions:
1) Why define a variable to hold 60 when all you do is multiply numbers by it? And why call it "xa" which have no meaning at all? If you want to avoid "magic numbers" then make them const and name them appropriately.
2) h is an int. So is m, and s. And xa is a constant int. So what do you expect new DateTime(9999, 01, 01, h * xa, m * xa, s * xa) to produce? Except to produce an ArgumentOutOfRangeException if h, m, or s is nonzero?

Since you don't seem to have run the code at all - or at least, with anything other than 0 in your offsets - there doesn't seem to be much point in "fixing it" for you.

A suggestion: if you want a "end time", take the current DateTime and add the amount you want to count to it, using the various DateTime.AddXxx methods.
Then to find out how far you are away, subtract the current time from that and use the resulting Timespan components.
   
Comments
_Q12_ 2-Sep-19 7:12am
   
haha, why do you presume i dont test my code and not debugging it before asking my questions here? I suppose many are doing it like this and is easy to presume, but... im not like everyone else. I do test and debug my code all the time, yes even before posting it here.
My code so far is working ...in a way.(this suggest that i test it already)
with "elapsed = EndTime.Subtract(DateTime.Now);" is counting down alright but... not how i wanted. If i set time.m = 30, is not starting from 30 to count down. Thats my problem.
OriginalGriff 2-Sep-19 7:39am
   
Because when you post code that clearly won't work except in specific conditions (i.e. it hasn't been used before) it's pretty obvious that your test / debug cycle isn;t particularly intensive, if it exists at all. And that's the same for all your questions: you do one quick check (if that) and assume it works in all cases.
That's wrong. Try it: set any one of h, m or s to a non-zero value and call you Reset method. Does it work flawlessly?
_Q12_ 2-Sep-19 7:15am
   
oh...and about my magic names, i use them to quickly test what i want to make. So those are some leftovers from my testings i did so far. I let them there purposly this time, to show you i tried what i could but i made only 50% of the functionable code that is present now. Sorry if is confusing. It means well.
_Q12_ 2-Sep-19 7:25am
   
CountUp() is working excelent ! And by that i mean, if i set time.s or time.m to 44, the time will start from that value, counting up, corectly and as a clock (and not to 10000000).
This functionality i want it for CountDown. If i set time.s or time.m to 12, it will count it down, as a clock (not to 1000000) and from that value i set it.
Right now is just doing it VERY wrong with the code i have.
OriginalGriff 2-Sep-19 7:40am
   
Yes it is. And that why I added the suggestion at the end ...
_Q12_ 2-Sep-19 8:21am
   
//Still, is not working.
       public void Reset()
        {
            StartTime = DateTime.Now;
            diference = new TimeSpan(h, m, s);


// HERE Endtime is updating normally 
            EndTime = DateTime.Now.AddHours(h);
            EndTime = DateTime.Now.AddMinutes(m);
            EndTime = DateTime.Now.AddSeconds(s);
        }
        public void CountDown()
        {
// HERE Endtime is Reseted to DateTime.Now and not keeping the set values from before. 
            elapsed = EndTime - DateTime.Now;


            //elapsed = EndTime.Subtract(DateTime.Now);
            //now update the values
            h = elapsed.Hours;
            m = elapsed.Minutes;
            s = elapsed.Seconds;
        }
//And in Form1
        public Form1()
        {
            InitializeComponent();
            t = new Timer(); t.Tick += new EventHandler(t_Tick); t.Start();


            time.h = 9;
            time.m = 9;
            time.s = 9;
            time.Reset();
        }

        Timer t;
        _Time time = new _Time();
        void t_Tick(object sender, EventArgs e)
        {
            time.CountDown();
            label1.Text = time.h + ":"+time.m + ":" + time.s;
        }
OriginalGriff 2-Sep-19 9:51am
   
"is not working" tells us nothing!
What is it doing that you didn't expect or not doing that you did?
What have you tried to find oue why? (Hint: what does the debugger show is going on?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900