Results 1 to 16 of 16

Thread: Period vs Comma problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Period vs Comma problem

    Hey!

    So I enter a standard value of my labels. Which is 1,20.
    When I add 0.2 to it on my pc. It becomes 1,40.

    On my brother's pc, it's 1.20,20?


    Can someone explain, please?

    Greetz
    Flexam

  2. #2

    Re: Period vs Comma problem

    There are a few reasons why, the most obvious one is region/culture settings which dictate what the actual "period" character is. However, why are you adding 0.2 (supposedly a number) to a Label with a text value of 1,20. That doesn't make much sense. Why don't you show us the code you're using so we can further see what's going on.

    You also might want to set Option Strict and Option Explicit to On.
    Last edited by formlesstree4; Apr 25th, 2013 at 12:39 PM. Reason: Clarification

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    There's two problems here, the first problem is that you and your brother have different culture settings, one of you have a setting where comma is treated as the decimal point while the other doesn't. The second (and this is the main problem) is that you're trying to add a number to a string. If you would convert both values to a number (a floating point value such as a Single or a Double) the problem would go away.

    If you post the code you have I'll show you how to do it, but basically you probably do something like this:
    Code:
    Label1.Text = Label1.Text + 0.2
    If you would turn Option Strict to On VB wouldn't even allow that code, which would have helped you in debugging it. (My point being, always use Option Strict On).

    Edit: Sometimes other people types quicker than me. Good work Formless!
    Last edited by Joacim Andersson; Apr 25th, 2013 at 12:44 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    If it works on my settings (dutch). Are there any complications except for other languages?
    How could I resolve it?

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    Quote Originally Posted by flexam View Post
    If it works on my settings (dutch). Are there any complications except for other languages?
    How could I resolve it?
    By not mixing strings with numbers... Post your code and we'll show you how to correct it. (You don't have to change your regional settings).

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    Code:
     
    dim aJupiler as integer
    dim Lichtbier as list(of integer)
    
    Select Case aJupiler 'Bereken de plaats in arraylist
                        Case Is = Lichtbier(4)
                            Select Case bLbl1.Text
                                Case Is = 3
                                    bPictureBox1.Image = My.Resources.greyarrow
                                    bLbl1.Text = 3
                                Case Is > 3
                                    bPictureBox1.Image = My.Resources.greenarrow
                                    bLbl1.Text = 3
                                Case Is < 2.81
                                    bPictureBox1.Image = My.Resources.greenarrow
                                    bLbl1.Text += 0.2
                                Case Is = 2.9
                                    bPictureBox1.Image = My.Resources.greenarrow
                                    bLbl1.Text += 0.1
                            End Select
    End Select

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    See, here's your problem:
    Code:
    bLbl1.Text += 0.2
    Actually you have several of those but with other numbers besides 0.2, you also have 0.1 and so on. Even your Select Case assumes there are numbers in your bLbl1.Text (which I assume is a Label). You need to understand that the Text property is a string (of text) not numeric, even if it contains a number. Text is Text and nothing else. What you need to do is to first convert that to the correct type, for example a Double. Since this is not user input (meaning the Text property will never be something like "Hello World" or something like that) you can get away by doing a direct conversion.
    Code:
    Dim value As Double = CDbl(bLbl1.Text)
    '...
    Select Case value ' <- Not the Text property but the numeric value you just converted the Text property into
      '...
      Case Is 2.9
        bPictureBox1.Image = ...
        value += 0.1
    End Select
    'Now convert the number back into a string
    bLbl1.Text = CStr(value)

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    Okay, I'll implement this in my code! Thanks so much

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    Just to make sure you understand the difference:

    1 + 2 = 3
    "a" + "b" = "ab"
    "1" + "2" = "12"

    If you would turn Option Strict On VB would tell you that you where mixing text with numbers and asked you to correct it (well, it would have given you a compile time error).

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    I see, do you have any good sites/tutorials for vb?

  11. #11
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: Period vs Comma problem


  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    Yeah sure. It's not a tutorial site but it's a great place to learn VB, all the experts hang around there.

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    Thanks a lot guys! Appreciate it! Too bad i'm not an expert

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    If you turn Option Strict On you will become an expert much faster than if you keep it off. The thing is that when it's turned Off (which unfortunately is the default) VB will allow you to mix things that are not the same and try to guess what you really want. To go back to my earlier explanation:

    1 + 2 = 3
    "a" + "b" = "ab"
    "1" + "2" = "12"
    "1" + 2 = ????? Either 3 or 12, what do you want? I don't know so let me just put one of them in and assume that that is what you want...

    The last comment is true when you have Option Strict turned Off. If you turn it On then VB will stop guessing and ask you to be specific instead.

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    13

    Re: Period vs Comma problem

    I've put it on now. Seems like I have to correct a whole lot

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Period vs Comma problem

    Quote Originally Posted by flexam View Post
    I've put it on now. Seems like I have to correct a whole lot
    But correcting those mistakes will get you one big step closer to becoming an expert.

Posting Permissions

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



Click Here to Expand Forum to Full Width