AuthorQuestion
HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
04/11/2008 = 4+1+1+2+0+0+8=16  =1+6=7

15/11/2012= 1+5+1+1+2+0+1+2=13   =1+3=4

How do i do this in visual syudio 2008

• 0
• 0
accepted
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 17 Dec 2012   Accepted Answer
Well, the AddDate method adds up the digits contained in whatever string is passed to it until it eventually arrives at a single digit.

So, if Saturday produces a total of 22 and you want to reduce this to the single digit 4, then you need to pass total.ToString() i.e. "22" to the method.

Your code should therefore be:

if(comboBox2.SelectedIndex >1)
{
string text1 = comboBox2.SelectedItem.ToString().ToLower();
int total1 = 0;

for (int i = 0; i < text1.lenght; i++)
{
if (dict.ContainKeys(text1[i]))
{
total1 += dict[text1[i]];
}
}

int number = AddDate(total1.ToString());
textBox3.Text = number.ToString();
}

• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
Just add this method to your form:

private int AddDate(string date)
{
string text = date;
int total;

do
{
total = 0;

foreach(char c in text)
{
if (char.IsDigit(c)) total += c - 48; // '0' is ASCII 48
}

text = total.ToString();
}
while (text.Length > 1);

}

and call it with your date as a string.

For example:

int number = AddDate("04/11/2008");
label1.Text = number.ToString(); // 7

• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
the date is entered in the main form and i have it shown in the 2nd form as a label.text.

how do i change the last two lines

int number = AddDate(label1.text)
textBox5 = number.ToString()

can i do it like tis or do i convertTo Int32

• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
That should work. The last line should be textBox5.Text = number.ToString();
• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
Thank you once again. I really appreciate it. In fact i see it is easier to work with functions.

tell me if i can use the same function when it comes to DAYS.EXAMPLE
Saturday,Monday.etc

I am trying to use  the prev solution with arrays.

I INCLUDED in my Form LOAD AN ARRAY LIKE
STRING [] DAYS = {"SATURDAY","Monday",etc ...};

WHEN I RUN THE CODE I AM GETTING 22. I NEED TO SPLIT 2+2=4 (correct)

WHEN I RUN THE CODE WITH FUNCTION i get 8(incorrect)
if(comboBox2.SelectediNDEX >1)
{
string text1 = comboBox2.SelectedItem.ToString().ToLower();
int total1 = 0;
for (int i=0; i < text1.lenght; i++)
if (dict.ContainKeys(text1 [i]))
{
total1 += dict[text1 [i]];
int s = convert.ToInt32(total1 += dict[text1 [i]]);
int number = AddDate(s.ToString());
textBox3.Text = number.ToString();

• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 16 Dec 2012
hi, What is your test case?
• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 17 Dec 2012
i am working on visual studio 2008 using c#

i have a label on form 2 called label1. it inherits the day from form1 when the user selects a date from datepicker1.

on form2 i have a textbox3. i want to take the label1.text which for example is SATURDAY.
EACH LETTER FROM SATURDAY HAS A VALUE IN ARRAY. FOR EXAMPLE S=2,A=1,T=3,U=1 ETC

SATURDAY WILL THEN BE 2+1+3..= 22

I MANAGED TO GET TO THE ANSWER OF 22. NOW I NEED TO SPLIT 22 INTO 2+2=4.  vULPES HAS HELP THUS FAR. HE ALSO GAVE ME A FUNCTION WHICH WORKS WITH DATE. NOW IT IS WITH DAY

• 0
• 0
Re: HOW TO ADD DATES AND RETURN WITH A SINGLE NO
Posted on: 17 Dec 2012
MAGIC! Works perfectly.

Thank you ONCE AGAIN

HOT QUESTIONS