1

i am pretty new to c# and for some reason my number wont convert for the life of me. My code is:

foreach(var descriptionid in test.items)
{
    ulong description = Convert.ToUInt32(descriptionid.Value.descriptionid);
    Console.WriteLine(description);
}

Any help is really appreciated!

Edit: This is the error message: http://gyazo.com/ed87941f4c8226ad6ebfd60879a5f173

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
ETurns
  • 73
  • 1
  • 12
  • Whats the problem and error message? – Peyman May 28 '15 at 05:41
  • Noo, its already a string and I need to convert it to a uint – ETurns May 28 '15 at 05:42
  • 1
    Name your variables according to their functionality. Anyway, `descriptionid.Value.descriptionid` is really not a good practice. About your question: debug it, step into a foreach loop and learn what is this value equal to when it breaks. Btw, long is a Int64 not an Int32. – Yeldar Kurmangaliyev May 28 '15 at 05:42
  • @ETurns, What's the descriptionId, I can not understand descriptionid.Value.descriptionid????? – Peyman May 28 '15 at 05:47
  • @ETurns - for persistence? Like, for storing in a database or... – wahwahwah May 28 '15 at 05:47
  • @DoKZ this is what i get: http://gyazo.com/0c44c4bfb238e59930569299ba9e63f9 – ETurns May 28 '15 at 05:48
  • @wahwahwah its so that I can call my get description function. It requires the descriptionid but as a ulong, not a string – ETurns May 28 '15 at 05:49
  • I edited the post to include the errormessage – ETurns May 28 '15 at 05:50
  • @ETurns, descriptionid.Value.descriptionid = "310776840_0", ofcourse you can not convert it to long or any other digit type. – Peyman May 28 '15 at 05:54
  • @ETurns, you can split by '_' and use the first part, check my answer – Peyman May 28 '15 at 06:01
  • Is the "310776840_0" _0 supposed to be a decimal point ? In that case you should replace it with your decimal separator and try your convert again – Oddmar Dam May 28 '15 at 06:10
  • @OddmarDam no its supposed to be an underscore. Its a weird way that the bot works to store the descriptions. Im just recoding it for my purposes, thankfully these guys helped me fix it! – ETurns May 28 '15 at 06:17

2 Answers2

1

Fisrt of all you need to get rid of the "_0" that exist at the end of the number, like that:

string number =  descriptionid.Value.descriptionid.ToString();
string[] nums = number.Split ('_');

And than to write the following code:

 ulong description = Convert.ToUInt64(nums[0]);
israel altar
  • 1,768
  • 1
  • 16
  • 24
0

I don't know what's your problem and error (whats descriptionid.Value.descriptionid ?????), but you can change your code like this:

foreach(var descriptionid in test.items)
{
    //var description = Convert.ToUInt64(descriptionid.Value.descriptionid);
    var description = Convert.ToUInt64(descriptionid.Value.descriptionid.Split(new char[]{'_'})[0]);

    Console.WriteLine(description);
}
Peyman
  • 3,068
  • 1
  • 18
  • 32