Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI guys

C#
km =Convert.ToDecimal(txtKMPerHr.Text);




foreach(decimal v in km.ToString())

{

km = v;

total = rate * km;


lblToTalValue.Text = total.ToString();




}


decimal v has not been assigned anywhere but every time v become 50 when code comes in the foreach...Y is this hapening?
Posted

1 solution

What is happing is complicated (and very, very silly)
foreach loops round providing one at a time each of the items in the list of items to teh right of the in keyword. Each time round, the item is provided in the variable v which is declared on the left side of the in keyword.

In you case, km.ToSting() returns a string, which when treated as an iterator =by anything (including foreach) retunrs a sequence of char - so:
C#
foreach (char c in "hello")
    {
    Console.WriteLine(c);
    }
Would give you:
h
e
l
l
o
Your code forces the char to be a decimal so each digit in the original decimal value is handed to you as a decimal in v. Your number in txtKMPerHr.Text starts with '2', so the first value passed to the loop is 50 - ASCII value of the character '2'

As I said - very silly. But what of earth are you trying to do that you think that this would help? :laugh:
 
Share this answer
 
Comments
Anele Ngqandu 16-Mar-12 5:02am    
hehe...find this intresting: 50 - ASCII value of the character '2'...am playing around anyway

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