Captain Caboose Captain Caboose - 1 month ago 5
Vb.net Question

How many .ToString("N0") can I have?

I am getting a warning:


Severity Code Description Project File Line Suppression State
Warning BC42322 Runtime errors might occur when converting 'String' to 'IFormatProvider'.


I have 2
.ToString("N0")
in my code in the same Sub. Can I not have 2 in the same sub? They go to different labels but, I am new to VB as well so please do not judge. Thanks!

If Integer.TryParse(input, infantry) Then
Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture)
frmMainGame.lblHPAI.Text = (hpai - infantry * 2).ToString("N0")

frmMainGame.lblInfantryNumberPlayer.Text -= input.ToString("N0") '<---- One that gets the warning

Else
' handle not an int inputted case
End If

Answer

Let's look at this line:

frmMainGame.lblInfantryNumberPlayer.Text -= input.ToString("N0") 

You're applying the -= operator to STRINGS. This operator has no meaning for strings. Are you trying to apply some kind of reverse-concatenation? Removing any occurrence of input from within the label text? That code just doesn't make sense at all.

If you're actually trying to do a numeric operation, you need to actually work with numbers... convert the label string to an integer, not the integer to a string.

If Integer.TryParse(input, infantry) Then

    Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture)
    frmMainGame.lblHPAI.Text = (hpai - infantry * 2).ToString("N0")

    Dim numPlayer = Integer.Parse(frmMainGame.lblInfantryNumberPlayer.Text)

    frmMainGame.lblInfantryNumberPlayer.Text = (numplayer - input).ToString("N0") '<---- One that gets the warning

Else
    ' handle not an int inputted case
End If
Comments