• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Newbie ByRef Sub question

VB.NET 2002

Form1.vb launches a custom dialog box form, Age.vb, to say ask for the users age:

frmAge = New Age(UserAge)
MsgBox("Hey you are: " & UserAge)

In Age.vb we have:

Public Sub New(ByRef UsersAge As Integer)
End Sub

a TextBox1 where they enter their age and a Button_OK to exit the dialog

When the user presses the OK button I can validate the entry they put in the TextBox1 but I do not know how to get the entry from TextBox1 to pass back when the user presses OK because the variable "UsersAge" is local to the Sub "New" only.

How do I make it global so I can change it and pass it back?

If I try the following in the event handler for Button_OK.Click  :

UsersAge = CInt(TextBox1.Text)

I of course get a: Name 'UsersAge' is not declared , complaint from the IDE.


1 Solution

Try this:
In form1.vb, create a label called Agelbl
In the Age form, in the button, do the following:
sub Command1_OnClick()
end sub

frmAge = New Age
frmAge.Show 1 ' it should be modal or the next line will be executed immediatly after showing the form
MsgBox("Hey you are: " & Agelbl)

Hope it helps,

Define a global variable some where and use this global variable

Craig_SparksAuthor Commented:
I was showing the dialog as non modal, so the modal thing fixed being able to read from it on return.

I tried the global thing at first but I can't seem to figure out how to make a global that spans all forms.

Public Shared VarName

atop form1

just gets me 'not declared' in all other forms.

Thanks all.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now