dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Windows 10 - Unexplained InvalidCastException ???

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2016
    Location
    Pretoria, South Africa
    Posts
    44

    Resolved [RESOLVED] Windows 10 - Unexplained InvalidCastException ???

    I have some code the decrypts some text and then casts it to a decimal value. With windows 7 it works fine, but on another computer running windows 10 it throws an exception stating "83.96" cannot be cast to type decimal??

    Code:
    dim deciphertext as string = "83.96"
    dim dec_value as decimal = CDec(deciphertext)   'Throws exception here
    How is this even possible? Some google answers suggest is might be something to do with the cultureinfo.currentculture on this specific machine, but the answers are very vague.

    Any help on where to start repairing would be greatly appreciated.
    Last edited by Vegito_ZA; Nov 29th, 2016 at 02:55 PM. Reason: Typo

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,112

    Re: Windows 10 - Unexplained InvalidCastException ???

    If you change "83.96" to "83,96" does it work then? If so then it is a culture issue. If you use Convert.ToDecimal or Decimal.Parse then you get to specify a format provider so you won;t implicitly rely on the current culture. Personally, I only use CDec and the like for casting rather than converting, i.e. I only use it when the object is already type Decimal. There cannot be any issue in that case.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2016
    Location
    Pretoria, South Africa
    Posts
    44

    Re: Windows 10 - Unexplained InvalidCastException ???

    @ jm, thanks for the tip. Yup it's a culture thing. Windows 10 allows a user to customize the way numbers are shown and in this specific case, the user chose " , " as the decimal separator. The moment I changed that setting, the exception went away.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width