Variable scope

T

tshad

I have a class with a variable employeeID as global variable.

I also have a subroutine that uses the same name:

Sub GetEmployee(employeeID as String)

Get the data
Assign the Global variables.

End sub

I want to assign the Global variable to the local variable. Is there a way
to do this easily without changing the name. I know there is a way to
reference the global variable but I can't remember what it is.

Thanks,

Tom
 
M

Mattias Sjögren

I have a class with a variable employeeID as global variable.

There are no truly global variables in VB.NET. I assume you mean a
field declared at the class level.

I also have a subroutine that uses the same name:

Sub GetEmployee(employeeID as String)

Get the data
Assign the Global variables.

End sub

I want to assign the Global variable to the local variable. Is there a way
to do this easily without changing the name. I know there is a way to
reference the global variable but I can't remember what it is.

Just prefix it with Me (or MyClass)

Me.employeeID = employeeID


Mattias
 
P

Phill W.

tshad said:
I have a class with a variable employeeID as global variable.

I /really/ hope you don't /mean/ global.

The whole point of a Class is that it "encapsulates" (i.e contains) data
that is relevant to a particular /instance/ of itself.
For example:

Class Employee
Friend Sub New( ByVal Id as Integer )
_Id = Id
End Sub

Public ReadOnly Property Id() as Integer
Get
Return _Id
End Get
End Property

Private _Id as Integer
End Class

....then...

Dim fred as New Employee( 55 )
I want to assign the Global variable to the local variable.

If you want to pass a value into a Class, use a Property.

. . .
Public Property DepartmentId() as Integer
Get
Return _DepartmentId
End Get
Set( ByVal DepartmentId as Integer )
_DepartmentId = DepartmentId
End Get
End Property
Private _DepartmentId as Integer
. . .

HTH,
Phill W.
 
T

tshad

That was what I was looking for.

Thanks,

Tom
Mattias Sjögren said:
There are no truly global variables in VB.NET. I assume you mean a
field declared at the class level.



Just prefix it with Me (or MyClass)

Me.employeeID = employeeID


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top