Greetings,

I'm creating a calculator for a game.. However when i use the code below, i get this error message:
Conversion from string "" to type 'Double' is not valid.

``````Private Sub DualTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DualTimer.Tick
If BuildCombo.Text = "Main-Hand + Off-Hand" Then
APSDPS3.Text = APSDPS.Text * ((15 / 100) + 1)
End If
End Sub
``````

The APSDPS3.Text = APSDPS.Text * ((15/100) + 1) in the code seems to be the problem, but what can i do to fix it?

Regards,
fRodzet

## All 15 Replies

You have to convert it to a number before you use it in the calculation. Typically, this is done by first checking to see if the string CAN be converted to a number as in

``````If IsNumeric(APSDPS.Text) Then
APSDPS3.text = (CDbl(APSDPS.Text) * (1.0 + 15.0/100.0)).ToString
``````
commented: Useful +0

I tried another function

``````Private Sub DualTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DualTimer.Tick
If BuildCombo.Text = "Main-Hand + Off-Hand" Then
If Not String.IsNullOrEmpty(APSDPS.Text) Then
APSDPS3.Text = APSDPS.Text * ((15 / 100) + 1)
Else : APSDPS3.Text = ""
End If
End If
End Sub
``````

seems to work, but which is "best" ?

And why do you use CDbl ? :)

Because APSDPS.Text is a String and you can't use strings in numeric calculations. CDbl converts a string to a double (or raises an exception if the string cannot be converted). For example, you can't execute

``````x = 5.3 + "19.12"
``````

but you can do

``````x = 5.3 + Cdbl("19.12")
``````

Very nice indeed, makes sense :) Ty.

I have a pretty long code here, you might want to have a look, anyway to simplify it?

Public Class Form1

``````Private Sub ClassCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClassCombo.SelectedIndexChanged

'Class Selection
If ClassCombo.Text = "Wizard" Then

BuildCombo.Items.Clear()
OffCombo.Enabled = False

ElseIf ClassCombo.Text = "Barbarian" Or ClassCombo.Text = "Monk" Then

BuildCombo.Items.Clear()
OffCombo.Enabled = True

ElseIf ClassCombo.Text = "Witch Doctor" Then

BuildCombo.Items.Clear()
OffCombo.Enabled = False

ElseIf ClassCombo.Text = "Demon Hunter" Then

BuildCombo.Items.Clear()
OffCombo.Enabled = True

End If
End Sub

Private Sub BuildCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildCombo.SelectedIndexChanged

'Build Selection

'Wizard Build Settings
If ClassCombo.Text = "Wizard" And BuildCombo.Text = "Main-Hand + Shield" Or ClassCombo.Text = "Wizard" And BuildCombo.Text = "Main-Hand + Source" Then

MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"

ElseIf ClassCombo.Text = "Wizard" And BuildCombo.Text = "Two-Handed Weapon" Then

MainCombo.Items.Clear()
Label5.Text = "Select Two-Handed"

'Barbarian Build Settings
ElseIf ClassCombo.Text = "Barbarian" And BuildCombo.Text = "Main-Hand + Shield" Then

MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = False

ElseIf ClassCombo.Text = "Barbarian" And BuildCombo.Text = "Main-Hand + Off-Hand" Then

'MainCombo Settings
MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = True

'OffCombo Settings
OffCombo.Items.Clear()

ElseIf ClassCombo.Text = "Barbarian" And BuildCombo.Text = "Two-Handed Weapon" Then

MainCombo.Items.Clear()
OffCombo.Enabled = False
Label5.Text = "Select Two-Handed"

'Monk Build Settings
ElseIf ClassCombo.Text = "Monk" And BuildCombo.Text = "Main-Hand + Shield" Then

MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = False

ElseIf ClassCombo.Text = "Monk" And BuildCombo.Text = "Main-Hand + Off-Hand" Then

'MainCombo Settings
MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = True

'OffCombo Settings
OffCombo.Items.Clear()

ElseIf ClassCombo.Text = "Monk" And BuildCombo.Text = "Two-Handed Weapon" Then

MainCombo.Items.Clear()
OffCombo.Enabled = False
Label5.Text = "Select Two-Handed"

'Witch Doctor Build Settings
ElseIf ClassCombo.Text = "Witch Doctor" And BuildCombo.Text = "Main-Hand + Shield" Or ClassCombo.Text = "Witch Doctor" And BuildCombo.Text = "Main-Hand + Mojo" Then

MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"

ElseIf ClassCombo.Text = "Witch Doctor" And BuildCombo.Text = "Two-Handed Weapon" Then

MainCombo.Items.Clear()
Label5.Text = "Select Two-Handed"

'Demon Hunter Build Settings
ElseIf ClassCombo.Text = "Demon Hunter" And BuildCombo.Text = "Main-Hand + Shield" Or ClassCombo.Text = "Demon Hunter" And BuildCombo.Text = "Main-Hand + Quiver" Then

MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = False

ElseIf ClassCombo.Text = "Demon Hunter" And BuildCombo.Text = "Main-Hand + Off-Hand" Then

'MainCombo Settings
MainCombo.Items.Clear()
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = True

'OffCombo Settings
OffCombo.Items.Clear()

ElseIf ClassCombo.Text = "Demon Hunter" And BuildCombo.Text = "Two-Handed + Quiver" Then

MainCombo.Items.Clear()
Label5.Text = "Select Two-Hand"
OffCombo.Enabled = False

End If

End Sub

Private Sub MHTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MHTimer.Tick

If BuildCombo.Text = "Two-Handed Weapon" Then
GroupBox2.Text = "Two-Handed Properties"
Else
GroupBox2.Text = "Main-Hand Properties"
End If

If MainCombo.Text = "Spear" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Spear" And BuildCombo.Text = "Main-Hand + Source" Or MainCombo.Text = "Spear" And BuildCombo.Text = "Main-Hand + Mojo" Or MainCombo.Text = "Spear" And BuildCombo.Text = "Main-Hand + Off-Hand" Or MainCombo.Text = "Spear" And BuildCombo.Text = "Main-Hand + Quiver" Then
APSDPS.Text = 1.2 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Mace" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Mace" And BuildCombo.Text = "Main-Hand + Source" Or MainCombo.Text = "Mace" And BuildCombo.Text = "Main-Hand + Mojo" Or MainCombo.Text = "Mace" And BuildCombo.Text = "Main-Hand + Off-Hand" Or MainCombo.Text = "Mace" And BuildCombo.Text = "Main-Hand + Quiver" Then
APSDPS.Text = 1.2 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Sword" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Sword" And BuildCombo.Text = "Main-Hand + Source" Or MainCombo.Text = "Sword" And BuildCombo.Text = "Main-Hand + Mojo" Or MainCombo.Text = "Sword" And BuildCombo.Text = "Main-Hand + Off-Hand" Or MainCombo.Text = "Sword" And BuildCombo.Text = "Main-Hand + Quiver" Then
APSDPS.Text = 1.4 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Wand" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Wand" And BuildCombo.Text = "Main-Hand + Source" Then
APSDPS.Text = 1.4 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Fist Weapon" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Fist Weapon" And BuildCombo.Text = "Main-Hand + Off-Hand" Then
APSDPS.Text = 1.4 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Ceremonial Knife" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Ceremonial Knife" And BuildCombo.Text = "Main-Hand + Mojo" Then
APSDPS.Text = 1.4 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Dagger" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Dagger" And BuildCombo.Text = "Main-Hand + Source" Or MainCombo.Text = "Dagger" And BuildCombo.Text = "Main-Hand + Mojo" Or MainCombo.Text = "Dagger" And BuildCombo.Text = "Main-Hand + Off-Hand" Or MainCombo.Text = "Dagger" And BuildCombo.Text = "Main-Hand + Quiver" Then
APSDPS.Text = 1.5 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Axe" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Axe" And BuildCombo.Text = "Main-Hand + Source" Or MainCombo.Text = "Axe" And BuildCombo.Text = "Main-Hand + Mojo" Or MainCombo.Text = "Axe" And BuildCombo.Text = "Main-Hand + Off-Hand" Or MainCombo.Text = "Axe" And BuildCombo.Text = "Main-Hand + Quiver" Then
APSDPS.Text = 1.3 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Mighty Weapon" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Mighty Weapon" And BuildCombo.Text = "Main-Hand + Off-Hand" Then
APSDPS.Text = 1.3 * ((Val(AsDPS.Text) / 100) + 1)

ElseIf MainCombo.Text = "Hand Crossbow" And BuildCombo.Text = "Main-Hand + Shield" Or MainCombo.Text = "Hand Crossbow" And BuildCombo.Text = "Main-Hand + Quiver" Or MainCombo.Text = "Hand Crossbow" And BuildCombo.Text = "Main-Hand + Off-Hand" Then
APSDPS.Text = 1.6 * ((Val(AsDPS.Text) / 100) + 1)

Else
APSDPS.Text = ""
End If

End Sub

Private Sub DualTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DualTimer.Tick
If BuildCombo.Text = "Main-Hand + Off-Hand" Then
If Not String.IsNullOrEmpty(APSDPS.Text) Then
APSDPS3.Text = APSDPS.Text * ((15 / 100) + 1)
Else : APSDPS3.Text = ""
End If
End If
End Sub
``````

End Class

As you see my code is pretty.. aaagh annoying.. id like to make it more understandable and readable without sending the same code over and over again.. Also maybe you know some great basic tutorials ?

Regards,
fRodzet

For a start I would set up tables or some other data structures instead of hardcoding the parameters. As I posted earlier, I would start by doing something like

``````Private Weapons As New Dictionary(Of String, String())

'create lists of weapons for each character class

Weapons.Add("Wizard", {"Main-Hand + Shield", "Main-Hand + Source", "Two-Handed Weapon"})
Weapons.Add("Barbarian", {"Maiin-Hand + Shield", "Main-Hand + Off-Hand", "Two-Handed Weapon"})
Weapons.Add("Monk", {"Main-Hand + Shield", "Main-Hand + Off-Hand", "Two-Handed Weapon"})
Weapons.Add("Witch Doctor", {"Main-Hand + Mojo", "Main-Hand + Shield", "Two-Handed Weapon"})
Weapons.Add("Demon Hunter", {"Main-Hand + Off-Hand", "Main-Hand + Quiver", "Two-Handed + Quiver", "Main-Hand + Shield"})

'populate class combobox

ClassCombo.Items.Clear()

For Each key As String In Weapons.Keys
Next

End Sub

Private Sub ClassCombo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ClassCombo.SelectedIndexChanged

'populate the weapons combobox with weapons for the selected character class

Dim cmb As ComboBox = sender
ComboBuild.Items.Clear()

End Sub
``````

Except I would likely store the strings in a file and read it in at run time.

Note that you can add all of the array items in one statement using AddRange (I neglected to do that in the earlier example). As for the remaining logic, there are just too may combinations to work through. You'd have to try to set up the tables. I don't know the logic behind your assignments.

commented: Damn TY! +0

Sorry for my english.. But id like to know how:

Weapons.Add("Wizard", {"Main-Hand + Shield", "Main-Hand + Source", "Two-Handed Weapon"})
Weapons.Add("Barbarian", {"Maiin-Hand + Shield", "Main-Hand + Off-Hand", "Two-Handed Weapon"})
Weapons.Add("Monk", {"Main-Hand + Shield", "Main-Hand + Off-Hand", "Two-Handed Weapon"})
Weapons.Add("Witch Doctor", {"Main-Hand + Mojo", "Main-Hand + Shield", "Two-Handed Weapon"})
Weapons.Add("Demon Hunter", {"Main-Hand + Off-Hand", "Main-Hand + Quiver", "Two-Handed + Quiver", "Main-Hand + Shield"})

Can be added so that, if i select Wizard from the combobox under select class then next to select build these settings will appear?

EDIT: Oh lol, i get it now - TY very much ! :)

Weapons is declared at the class level (outside of all subs/functions so it is accessible from from all subs/functions. The ClassCombo_SelectedIndexChanged event gets fired when the user selects a new item from the ClassCombo control. The code in that handler:

``````Dim cmb As ComboBox = sender
ComboBuild.Items.Clear()
``````

first clears the ComboBuild (sorry about the name change) combobox, then adds all of the items from Weapons that are associated with that choice. For example, if the user selects "Wizard" then effectively what you get is

``````ComboBuild.Items.AddRange({"Main-Hand + Shield", "Main-Hand + Source", "Two-Handed Weapon"})
``````

If the user selects Barbarian then the items from that entru in Weapons gets added to ComboBuild. It would be a simple matter to create a text file containing

Wizard,Main-Hand + Shield,Main-Hand + Source,Two-Handed Weapon
Barbarian,Main-Hand + Shield,Main-Hand + Off-Hand,Two-Handed Weapon
Monk,Main-Hand + Shield,Main-Hand + Off-Hand,Two-Handed Weapon
Witch Doctor,Main-Hand + Mojo,Main-Hand + Shield,Two-Handed Weapon
Demon Hunter,Main-Hand + Off-Hand,Main-Hand + Quiver,Two-Handed + Quiver,Main-Hand + Shield

Then build the Weapons dictionary by reading the file in at runtime.

By the way, you can also rewrite blocks such as

``````MainCombo.Items.Add("Spear")
``````

as

``````MainCombo.Items.AddRange({"Spear","Sword","Mace","Wand","Dagger","Axe"})
``````

and it's a matter of style but I prefer Select Case blocks to lengthy If-Then-ElseIf constructs. For example

``````Private Sub BuildCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildCombo.SelectedIndexChanged

'Build Selection

MainCombo.Items.Clear()
OffCombo.Items.Clear()

Select Case ClassCombo.Text

Case "Wizard"

Select Case BuildCombo.Text

Case "Main-Hand + Shield" or "Main-Hand + Source"
Label5.Text = "Select Main-Hand"

Case "Two-Handed Weapon"
Label5.Text = "Select Two-Handed"

End Select

Case "Barbarian"

Select Case BuildCombo.text

Case "Main-Hand + Shield"
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = False
``````

and so on. If you have code that is common to all blocks then move it out of the block.

Thanks :)

However, if i write:

Case "Main-Hand + Shield" or "Main-Hand + Source"

i get an error

i have to write it like:

Case "Main-Hand + Shield"

and then make a new case

Case "Main-Hand + Source"

if i do it like you, i get this error:
Conversion from string "Main-Hand + Source" to type 'Long' is not valid.

Sorry. My mistake. Change the "or" to a comma and it works as in

``````Case  "Main-Hand + Shield", "Main-Hand + Source"
``````
commented: Ty +0

Thank you once again, damn this really made my code way more easy to read :)

is there any way you can write private messages or something on this forum ? or anyway to contact u - my mail is sifferjb@msn.com, you can send me an mail if you want not to share urs here, and if not at all - its fine :)

``````Public Class Form1
Private Builds As New Dictionary(Of String, String())
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Selected Builds

Builds.Add("Wizard", {"Main-Hand + Source", "Main-Hand + Shield", "Two-Handed Weapon"})
Builds.Add("Barbarian", {"Main-Hand + Off-Hand", "Main-Hand + Shield", "Two-Handed Weapon"})
Builds.Add("Demon Hunter", {"Main-Hand + Off-Hand", "Main-Hand + Quiver", "Main-Hand + Shield", "Two-Handed + Quiver"})
Builds.Add("Witch Doctor", {"Main-Hand + Mojo", "Main-Hand + Shield", "Two-Handed Weapon"})
Builds.Add("Monk", {"Main-Hand + Off-Hand", "Main-Hand + Shield", "Two-Handed Weapon"})

ClassCombo.Items.Clear()

For Each key As String In Builds.Keys
Next

End Sub

Private Sub ClassCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClassCombo.SelectedIndexChanged
Dim Weapon As ComboBox = sender
BuildCombo.Items.Clear()

Select Case ClassCombo.Text
Case "Wizard", "Witch Doctor"
OffCombo.Enabled = False
GroupBox4.Enabled = False
Case "Barbarian", "Monk", "Demon Hunter", ""
OffCombo.Enabled = True
GroupBox4.Enabled = True
End Select

End Sub

Private Sub BuildCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildCombo.SelectedIndexChanged

MainCombo.Items.Clear()
OffCombo.Items.Clear()

Select Case BuildCombo.Text
Case "Two-Handed Weapon", "Two-Handed + Quiver"
Label5.Text = "Select Two-Handed"
OffCombo.Enabled = False
GroupBox4.Enabled = False
Case "Main-Hand + Quiver", "Main-Hand + Source", "Main-Hand + Mojo", "Main-Hand + Shield", ""
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = False
GroupBox4.Enabled = False
Case "Main-Hand + Off-Hand"
Label5.Text = "Select Main-Hand"
OffCombo.Enabled = True
GroupBox4.Enabled = True
End Select

Select Case ClassCombo.Text

Case "Wizard"

Select Case BuildCombo.Text
Case "Main-Hand + Source", "Main-Hand + Shield"
MainCombo.Items.AddRange({"Spear", "Sword", "Wand", "Mace", "Dagger", "Axe"})
Case "Two-Handed Weapon"
MainCombo.Items.AddRange({"Axe", "Bow", "Crossbow", "Mace", "Staff", "Sword"})
End Select

Case "Barbarian"

Select Case BuildCombo.Text
Case "Main-Hand + Shield", "Main-Hand + Off-Hand"
MainCombo.Items.AddRange({"Axe", "Sword", "Spear", "Mighty Weapon", "Dagger", "Mace"})
OffCombo.Items.AddRange({"Axe", "Sword", "Spear", "Mighty Weapon", "Dagger", "Mace"})
Case "Two-Handed Weapon"
MainCombo.Items.AddRange({"Axe", "Mighty Weapon", "Polearm", "Sword", "Mace"})
End Select

Case "Monk"

Select Case BuildCombo.Text
Case "Main-Hand + Shield", "Main-Hand + Off-Hand"
MainCombo.Items.AddRange({"Axe", "Spear", "Sword", "Mace", "Fist Weapon", "Dagger"})
OffCombo.Items.AddRange({"Axe", "Spear", "Sword", "Mace", "Fist Weapon", "Dagger"})
Case "Two-Handed Weapon"
MainCombo.Items.AddRange({"Staff", "Daibo", "Polearm", "Mace", "Sword", "Axe"})
End Select

Case "Witch Doctor"

Select Case BuildCombo.Text
Case "Main-Hand + Mojo", "Main-Hand + Shield"
MainCombo.Items.AddRange({"Axe", "Ceremonial Knife", "Spear", "Sword", "Mace", "Dagger"})
Case "Two-Handed Weapon"
MainCombo.Items.AddRange({"Axe", "Bow", "Crossbow", "Mace", "Polearm", "Staff", "Sword"})
End Select

Case "Demon Hunter"

Select Case BuildCombo.Text
Case "Main-Hand + Off-Hand", "Main-Hand + Quiver", "Main-Hand + Shield"
MainCombo.Items.AddRange({"Hand Crossbow", "Axe", "Dagger", "Sword", "Spear", "Mace"})
OffCombo.Items.AddRange({"Hand Crossbow", "Axe", "Dagger", "Sword", "Spear", "Mace"})
Case "Two-Handed + Quiver", "Two-Handed Weapon"
End Select

End Select

End Sub

End Class
``````

This is my code by now, you got any other smart tips to simplify it even more, feel free to post it here then :)

You can send private messages on DaniWeb. When you click on a user's name, you go to their profile page. There is a "send private message" under CONTACT ME on that page. At the top of every page there is a link labeled "PRIVATE MESSAGES". If you have any unread messages it will be flagged there. We discourage asking for help in private messages because any help given there cannot be seen by anyone else. However, if you have to pass sensitive data this is one way to do it.

commented: Ty +0

You been to very much Help - Appreciated very much! I now got much better focus of my "Newb Programming" thanks to you! :) +rep