Asday


I have this code:

Sub monSet(ByVal arg)
If arg = "rat" Then
Dim aatt As Integer
Dim adef As Integer
Dim lvl_ As Integer
Dim aspd As Integer
aatt = 1
adef = 1
lvl_ = 1
aspd = 1
End If
If arg = "thief" Then
Dim aatt As Integer
Dim adef As Integer
Dim lvl_ As Integer
Dim aspd As Integer
aatt = 17
adef = 13
lvl_ = 15
aspd = 21
End If
End Sub
'...
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
monSet("thief")
MessageBox.Show(aatt + ", " + adef)
End Sub

And it tells me off for not telling it what aatt and adef are. I thought that it would call monSet, then decide whether to shout at me or not, but apparently not.

How do I fix that, then



Re: newbie call errors

Spidermans_DarkSide - MSP, VSIP


Hi,

Please see the code comments below.

Regards,

S_DS

___________________________________

Public Class Form1

'Moved so the Button_Click Sub also "sees" these

'these two variables.

Dim aatt As Integer

Dim adef As Integer

Sub monSet(ByVal arg)

'Do one set of DIM statements at the top of your SUB

Dim lvl_ As Integer

Dim aspd As Integer

If arg = "rat" Then

'These two DIM'ed outside the SUB to make the variables

'GLOBAL to all the SUBroutines and Functions within the FORM.

'Dim aatt As Integer

'Dim adef As Integer

aatt = 1

adef = 1

lvl_ = 1

aspd = 1

End If

If arg = "thief" Then

Dim aatt As Integer

'No need to DIM any variable twice within a Sub

'Do all the local declarations at the top.

'Dim adef As Integer

aatt = 17

adef = 13

lvl_ = 15

aspd = 21

End If

End Sub

'...

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

monSet("thief")

'Use ToString to change a variable TYPE

'for use in a MessageBox.

'Try to use "&" to join strings and "+" for addition too,

'it makes it clearer that you are joining strings.

MessageBox.Show(aatt.ToString & ", " & adef.ToString)

End Sub

End Class






Re: newbie call errors

Spidermans_DarkSide - MSP, VSIP

Tip No.2

If you go to the project menu and add a MODULE with PUBLIC variables, you can then use them within every SUB or FUNCTION and within every FORM. >>>>

Code Snippet

Module Module1

Public myPublicInteger As Integer

Public myPublicString As String

'etcetra.>>>>

End Module

Code Snippet

Public Class Form1

'Sets the "myPublicString" to "Hello there!!"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

myPublicString = "Hello there!!"

End Sub

'Shows the same "myPublicString" in a message.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBox.Show(myPublicString)

End Sub

End Class

Use the above idea with caution.

Insert the word PUBLIC if you like in the variable names if you want to keep track of their values easier as it easy for a SUB or a FUNCTION to change a value, especially if you have forgotten a particular SUB or FUNCTION changes it.

Regards,

S_DS







Re: newbie call errors

Asday

Excellent, thanks!