Resolved integer double string?

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
Hey i've got another problem with my coding in this sub...


Code:
    Private Sub buyLemon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buyLemon.Click
        lemonL.Text += 1
    End Sub 'BuyLemons

can someone please tell me why its coming up with this error


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

its for a lemonadeTycoon game im making
 
Last edited:

BlackByte

Well-known member
Joined
Jun 29, 2008
Messages
126
Location
South Africa, Durban
Programming Experience
1-3
Because youre trying to increment a string using an integer, why are you adding 1 to string "lemons", maybe if you could tell me what you're trying to archieve i could help you.
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
When does the error appear, before you compile it or at runtime?

If it's at runtime, go back and turn Option Strict On. It wont 'stop' the error, but it will make your coding better in the long run :)
 

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
Because youre trying to increment a string using an integer, why are you adding 1 to string "lemons", maybe if you could tell me what you're trying to archieve i could help you.

im trying to increase a string by one everytime you click a button but for some reason its taking it in as a double there are no other variables or objects call lemonL and i cant realy change anything until i can test it
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I think you've missed BlackByte's point. You cannot increase a string by 1. For example, the following is invalid :-

Code:
 Dim TestString As String = "ABC"
        TestString += 1
but this is essentially what you are trying to do. Your program doesnt know that your textbox LemonL holds a number.

If you categorically know that LemonL holds a number value, the simplest way would be :-
Code:
LemonL.Text = (Convert.ToInt32(LemonL.Text) + 1).ToString
If you cant guarantee it, you'll need to TryParse the value first before adding to it.

Separately, you could try using a NumericUpDown control instead for LemonL, and then you wouldnt need to do any conversions.
 

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
thanks that seemed to work but could explain how it works (what means what)
nothing much its just i like to know what im typing:)
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
Code:
LemonL.Text = (Convert.ToInt32(LemonL.Text) + 1).ToString
means

Take the value of the text in LemonL textbox
Convert it to a 32-bit integer value
Add 1 to the result
Convert the result back to a string
Put the result back into the LemonL textbox
 

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
ok but what if i wanted something to equal that value ( lemon = lemonL.text ) what would i do in that case


and i was using numeric up down but i couldnt find out how to add to the value of something
 
Last edited:

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
no i mean like if numericupdown1.value gets bigger

Upbutton / DownButton methods?

like what ive got a button that increses the value and one that decreases the value already
Code:
Private Sub buyLemon_Click <---- There
 
Last edited:

iamyou

Member
Joined
Sep 7, 2009
Messages
12
Programming Experience
Beginner
lol i just relized i could have just changed the variable then making the LemonL.text = lemons.toString
 
Top Bottom