• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • 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)
frmAge.Show
MsgBox("Hey you are: " & UserAge)

In Age.vb we have:

Public Sub New(ByRef UsersAge As Integer)
     MyBase.New()
     InitializeComponent()
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)
Me.Close

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

Thanks.

0
Craig_Sparks
Asked:
Craig_Sparks
1 Solution
 
MaxPolCommented:
Hi,

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

Agelbl.caption=""
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,

Max
0
 
EDDYKTCommented:
Define a global variable some where and use this global variable

0
 
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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