Jump to content

VB Can't Convert String to Decimal

Go to solution Solved by JT8101997,

I think i fixed it actually. Instead of leaving the labels blank i just put 0 into the text for each, because it was trying to add them all before i completed every level, so there was no score yet and that's why it was empty. Thank you all for the help :) I should be fine now.

I'm trying to write some code for a program i'm working on, but i'm having a problem. I wrote the code i need help with in bold, but i included the rest of the sub so you could see what's going on.

 

Example:

If LVL1.Visible And My.Forms.LVL1.lblFinish.BackColor = Color.Black Then
            lbl1.Text = My.Forms.LVL1.lblScore.Text
        End If
        'Level 2
        If LVL2.Visible And My.Forms.LVL2.lblFinish.BackColor = Color.Black Then
            lbl2.Text = My.Forms.LVL2.lblScore.Text
        End If
        'Level 3
        If LVL3.Visible And My.Forms.LVL3.lblFinish.BackColor = Color.Black Then
            lbl3.Text = My.Forms.LVL3.lblScore.Text
        End If
        'Level 4
        If LVL4.Visible And My.Forms.LVL4.lblFinish.BackColor = Color.Black Then
            lbl4.Text = My.Forms.LVL4.lblScore.Text
        End If
        'Level 5
        If LVL5.Visible And My.Forms.LVL5.lblFinish.BackColor = Color.Black Then
            lbl5.Text = My.Forms.LVL5.lblScore.Text
        End If
        'Level 6
        If LVL6.Visible And My.Forms.LVL6.lblFinish.BackColor = Color.Black Then
            lbl6.Text = My.Forms.LVL6.lblScore.Text
        End If
lblTotalScore.Text = lbl1.Text + lbl2.Text + lbl3.Text + lbl4.Text + lbl5.Text + lbl6.Text
 
 
But my problem is that it adds them as strings so the value of each appears next to each other. I want to add the values of each, so i used CDec() for each one, but i got an error saying it was not formated properly. I've had this happen multiple times with different code in this program. Kinda urgent as this is the final piece i need to work and this is due on tuesday next week. Thanks everyone.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to comment
https://linustechtips.com/topic/362805-vb-cant-convert-string-to-decimal/
Share on other sites

Link to post
Share on other sites

Try:

lblTotalScore.Text = CDbl(lbl1.Text) + CDbl(lbl2.Text) + CDbl(lbl3.Text) + CDbl(lbl4.Text) + CDbl(lbl5.Text) + CDbl(lbl6.Text)

(https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx)

Just tried it. Gives me this every time. Just replace double with integer, decimal, etc.

 

Conversion from string "" to type 'Double' is not valid.

 

EDIT: this is under a timer tick sub if that matters.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to post
Share on other sites

Can you create variables for each of the labels. and then just add them and then convert the result to the totalscore label?

  Dim var01 As Decimal = Convert.ToDecimal(Label1.Text)

  Dim var02 As Decimal = Convert.ToDecimal(Label2.Text)
  Dim result As Decimal = var01 + var02
 
  Label3.Text = Convert.ToString(result)
Link to post
Share on other sites

 

Just tried it. Gives me this every time. Just replace double with integer, decimal, etc.

 

Conversion from string "" to type 'Double' is not valid.

 

EDIT: this is under a timer tick sub if that matters.

 

An empty string (ie: "") isn't a valid number so your conversion is throwing an exception.

// this worksDim s As String = "6.55"Dim d As Decimal = CDec(s)// this throws an exceptionDim s As String = ""Dim d As Decimal = CDec(s)// Also note that these don't throw an exception// but instead and just converts to 0Dim s As StringDim d As Decimal = CDec(s)Dim s As String = NothingDim d As Decimal = CDec(s)

You have some options depending on how you want to handle it. You can check that the string isn't empty before you call the conversion method on it, you can use a TryParse method (ex: Decimal.TryParse), or you can use try/catch statements to handle the exception.

Link to post
Share on other sites

 

Can you create variables for each of the labels. and then just add them and then convert the result to the totalscore label?

  Dim var01 As Decimal = Convert.ToDecimal(Label1.Text)

  Dim var02 As Decimal = Convert.ToDecimal(Label2.Text)
  Dim result As Decimal = var01 + var02
 
  Label3.Text = Convert.ToString(result)

 

Tried it. I got this.

 

"Input string was not in a correct format."

 

Idk what's wrong with this, because that's the code that should work.

OMFG I HATE VB. Can't wait to be done with this class so i can learn something more useful with my time -__-

I'm beginning to think it's just an error with visual studio and not actually my code at all.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to post
Share on other sites

Tried it. I got this.

 

"Input string was not in a correct format."

 

See my above answer about converting empty strings. The same thing is happening here only because you're using a different method, Convert.ToDecimal instead of CDec, it's just throwing a different exception. 

Link to post
Share on other sites

Yeah... must've overlooked that sorry. Your real issue here is that your strings are empty, as @madknight3 said... Have you tried printing them out/showing them individually to make sure they show the content you're looking for?

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

See my above answer about converting empty strings. The same thing is happening here only because you're using a different method, Convert.ToDecimal instead of CDec, it's just throwing a different exception. 

How would i do that though? I'm just confused by that. I understand the code but the problem is that I'm not declaring it as a string. It just turns itself into that and wont let me convert it back. This is for a game and it pulls the score from each level and i wanted it to add them all together to give you a total score for the chapter which has these six levels in it, but i can't do that.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to post
Share on other sites

Yeah... must've overlooked that sorry. Your real issue here is that your strings are empty, as @madknight3 said... Have you tried printing them out/showing them individually to make sure they show the content you're looking for?

How would i fix that then? I don't see why they're empty they should contain the value of the labels text though right?

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to post
Share on other sites

How would i do that though? I'm just confused by that. I understand the code but the problem is that I'm not declaring it as a string. It just turns itself into that and wont let me convert it back. This is for a game and it pulls the score from each level and i wanted it to add them all together to give you a total score for the chapter which has these six levels in it, but i can't do that.

 

Many controls like Labels and TextBoxes have a Text property. These require what they display to be a String. So even if you put a Decimal into the Text field, VB automatically converts it to a String for you and you'll get a String when you get it out of the Text property.

// Given your decimalDim d As New Decimal(6.55)// doing this will turn it into a stringLabel1.Text = d// the result is the same as thisLabel1.Text = d.ToString()// So now when using the Label1.Text, you have to treat it as a stringDim s As String = Label1.Text
Link to post
Share on other sites

I think i fixed it actually. Instead of leaving the labels blank i just put 0 into the text for each, because it was trying to add them all before i completed every level, so there was no score yet and that's why it was empty. Thank you all for the help :) I should be fine now.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to post
Share on other sites

How about this to stop the error.

 

Dim var01 As Decimal = Nothing
        Dim var02 As Decimal = Nothing
 
 
        If (Label1.Text.Equals("")) Then
            Label1.Text = 0.0
        End If
 
        If (Label2.Text.Equals("")) Then
            Label2.Text = 0.0
        End If
 
        var01 = Convert.ToDecimal(Label1.Text)
        var02 = Convert.ToDecimal(Label2.Text)
 
        Dim result As Decimal = var01 + var02
 
        Label3.Text = Convert.ToString(result)
Link to post
Share on other sites

I think i fixed it actually. Instead of leaving the labels blank i just put 0 into the text for each, because it was trying to add them all before i completed every level, so there was no score yet and that's why it was empty. Thank you all for the help :) I should be fine now.

 

Yes, that is another way to get it to work. If you don't allow something to be an empty string, then it can't throw that exception. The others I mentioned are good to learn though as you may need them in other situations.

Dim s As String = Label1.TextDim d As Decimal// Check for empty stringIf s.Equals("") Then    // Do something to prevent exception    // Before calling CDec(s)End If// Use TryParseIf (Decimal.TryParse(s, d)) Then    // Conversion successful    // d contains the converted valueElse    // Conversion failed    // d contains zeroEnd If// Use try/catchTry    CDec(s)Catch ex As Exception    // Do something in the case of an exception    // For example, set d to zeroEnd Try
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×