Board index » Visual Studio » Passing variable to and from a Class Module?

Passing variable to and from a Class Module?

Visual Studio174
I have created a class module that I want to use to convert my Latitude and

Longitude, what is the proper way to declare the variable in the class

module and what is the proper way to reference them to return the new value.

I've been reading and there seems to be several ways to pass information to

the class module but what's the right way?



Do I make variable PUBLIC?

Do I pass them like a FUNCTION (Lat as single, Long as single)


-
 

Re:Passing variable to and from a Class Module?

"HotRod" <NOSPAM@youremail.com>wrote in message

Quote
I have created a class module that I want to use to convert my Latitude and

Longitude, what is the proper way to declare the variable in the class

module and what is the proper way to reference them to return the new

value. I've been reading and there seems to be several ways to pass

information to the class module but what's the right way?



Do I make variable PUBLIC?

Do I pass them like a FUNCTION (Lat as single, Long as single)



You can do it with a sub/function or properties. Property procedure are

"better" than simple public variables since you can do all kinds of things

in the class itself.

'======================Form Code

Option Explicit



Private Sub Command1_Click()

Dim o As clsLocation

Set o = New clsLocation

o.Lat = 123

o.Lng = 345

Debug.Print o.Lat, o.Lng

End Sub

'======================Class Code

Option Explicit



Private mfLat As Single

Private mfLng As Single



Public Property Get Lat() As Single

Lat = mfLat

End Property



Public Property Let Lat(ByVal Setting As Single)

mfLat = Setting

'you can add code here to do calculations, range checking, etc

End Property



Public Property Get Lng() As Single

Lng = mfLng

End Property



Public Property Let Lng(ByVal Setting As Single)

mfLng = Setting

'you can add code here to do calculations, range checking, etc

End Property

'======================





--

Ken Halter - MS-MVP-VB - www.vbsight.com">www.vbsight.com

DLL Hell problems? Try ComGuard - www.vbsight.com/ComGuard.htm">www.vbsight.com/ComGuard.htm

Please keep all discussions in the groups..







-

Re:Passing variable to and from a Class Module?

Quote
I have created a class module that I want to use to convert my Latitude and

Longitude, what is the proper way to declare the variable in the class

module and what is the proper way to reference them to return the new value.

I've been reading and there seems to be several ways to pass information to

the class module but what's the right way?



Do I make variable PUBLIC?

Do I pass them like a FUNCTION (Lat as single, Long as single)



If the calculation is performed on both at the same time then create a public sub and pass the lat/long in as ByRef

parameters (that can be modified by the class.) If the calculations can be performed separately then you may want to

create two separate public functions which have the advantage that they don't destroy the original values.

Depending on exactly what your class does though, you may want to approach the problem a different way with a more OO

solution. Assuming for the time being that all your class does is to do with lat/long then you could create two public

properties which expose the two member variables, then have a method of the class which performs the conversion, i.e:



'***

Private m_Latitude As Single, m_Longitude As Single



Public Property Get Latitude() As Single

Latitude = m_Latitude

End Property

Public Property Let Latitude(ByVal inNew As Single)

m_Latitude = inNew

End Property



Public Property Get Longitude() As Single

Longitude = m_Longitude

End Property

Public Property Let Longitude(ByVal inNew As Single)

m_Longitude = inNew

End Property



Public Sub ConvertCoords()

' Do whatever you want to do here

m_Latitude = -m_Latitude

m_Longitude = -m_Longitude

End Sub

'***



It all depends on what your requirements are though as to how you arrange the class.

Hope this helps,



Mike





- Microsoft Visual Basic MVP -

E-Mail: EDais@mvps.org

WWW: EDais.mvps.org/





-

Re:Passing variable to and from a Class Module?

"HotRod" <NOSPAM@youremail.com>wrote in message

Quote


Do I make variable PUBLIC?

Do I pass them like a FUNCTION (Lat as single, Long as single)





I forgot to mention.... You can't use the word "Long" as a

variable/parameter name because it's a VB data type.



--

Ken Halter - MS-MVP-VB - www.vbsight.com">www.vbsight.com

DLL Hell problems? Try ComGuard - www.vbsight.com/ComGuard.htm">www.vbsight.com/ComGuard.htm

Please keep all discussions in the groups..





-

Re:Passing variable to and from a Class Module?

"HotRod" <NOSPAM@youremail.com>wrote in message

Quote
I have created a class module that I want to use to convert my

Latitude and Longitude



To and from what?



Quote
what is the proper way to declare the variable in the class module

and what is the proper way to reference them to return the new

value.



Variables? Private. [Almost] always.

Functions and Propeties? Public.



Quote
I've been reading and there seems to be several ways to pass

information to the class module but what's the right way?



There is no, one Right Way - just works best for a given application.



Quote
Do I make variable PUBLIC?



No.



Quote
Do I pass them like a FUNCTION (Lat as single, Long as single)



Probably, but something more like



Public Function GetSomething( _

ByVal Latitude as Single _

, ByVal Longitude as Single _

) as SomethingElse



Alternatively, you could have a class that encapsulates a

"position", that can be seen in many different ways, something

like



[Position.cls]

Public Property Get Latitude() as Single

Public Property Get Longitude() as Single

Public Property Get Something() as SomethingElse



Public Sub FromLatitudeLongitude( _

ByVal Latitude as Single _

, ByVal Longitude as Single _

)

m_nLatitude = Latitude

m_nLongitude = Longitude

End Sub



Public Sub FromSomethingElse( _

ByVal Something as SomethingElse _

)

m_nLatitude = foo1( Something )

m_nLongitude = foo2( Something )

End Sub



Private m_nLatitude as Single

Private m_nLongitude as Single



Then use this class as



Dim here as Position



Set here = New Position

Call here.FromLatitudeLongitude( 0, 0 )



MsgBox here.Something



HTH,

Phill W.





-

Re:Passing variable to and from a Class Module?

Sorry I should have been more specific



I am currently in a class module but before I create the shape I need to

check that the Latitude and Longitude are real, they might be in a UTM

format instead. In this case I want to take the current numbers and pass

them to the ConvertUTM class, have both numbers converted to a real Latitude

and Longitude and then have them returned to the original calling class.

I've been trying to figure out how to declare everything and then how to

call the class module, load the values into the variables utmLatitude &

utmLongitude and then send them back to the calling class.



I apologise it's been a while since I did any real work in VB.





-

Re:Passing variable to and from a Class Module?

So can I assume once I've set the Latitude how do I run the calculations?



Do I



set o as MyNewClass



call o.ConvertCoords(myVAR, MyVAR2)























"Mike D Sutton" <EDais@mvps.org>wrote in message

Quote
>I have created a class module that I want to use to convert my Latitude

>and

>Longitude, what is the proper way to declare the variable in the class

>module and what is the proper way to reference them to return the new

>value.

>I've been reading and there seems to be several ways to pass information

>to

>the class module but what's the right way?

>

>Do I make variable PUBLIC?

>Do I pass them like a FUNCTION (Lat as single, Long as single)



If the calculation is performed on both at the same time then create a

public sub and pass the lat/long in as ByRef

parameters (that can be modified by the class.) If the calculations can

be performed separately then you may want to

create two separate public functions which have the advantage that they

don't destroy the original values.

Depending on exactly what your class does though, you may want to approach

the problem a different way with a more OO

solution. Assuming for the time being that all your class does is to do

with lat/long then you could create two public

properties which expose the two member variables, then have a method of

the class which performs the conversion, i.e:



'***

Private m_Latitude As Single, m_Longitude As Single



Public Property Get Latitude() As Single

Latitude = m_Latitude

End Property

Public Property Let Latitude(ByVal inNew As Single)

m_Latitude = inNew

End Property



Public Property Get Longitude() As Single

Longitude = m_Longitude

End Property

Public Property Let Longitude(ByVal inNew As Single)

m_Longitude = inNew

End Property



Public Sub ConvertCoords()

' Do whatever you want to do here

m_Latitude = -m_Latitude

m_Longitude = -m_Longitude

End Sub

'***



It all depends on what your requirements are though as to how you arrange

the class.

Hope this helps,



Mike





- Microsoft Visual Basic MVP -

E-Mail: EDais@mvps.org

WWW: EDais.mvps.org/









-

Re:Passing variable to and from a Class Module?

Quote
So can I assume once I've set the Latitude how do I run the calculations?



Do I



set o as MyNewClass



call o.ConvertCoords(myVAR, MyVAR2)



The ConvertCoords() function knows how to perform the conversion, and will operate directly on the Lat/Long stored in

the member variables of the object.

You'd use something like this:



'***

Dim o As MyNewClass



Set o = New MyNewClass

o.Latitude = 123

o.Longitude = 456

If (CoordinatesRequireConversion) Then Call o.ConvertCoords



' Use o.Latitude and o.Longitude here



Set o = Nothing

'***



Hope this helps,



Mike





- Microsoft Visual Basic MVP -

E-Mail: EDais@mvps.org

WWW: EDais.mvps.org/





-

Re:Passing variable to and from a Class Module?

Mike

THANKS that makes a lot more sense. I'll give it a try and keep you

posted. Honestly I understand the reusability issues with a Class Module but

there seems to be a lot of passing the same data.







"Mike D Sutton" <EDais@mvps.org>wrote in message

Quote
>So can I assume once I've set the Latitude how do I run the calculations?

>

>Do I

>

>set o as MyNewClass

>

>call o.ConvertCoords(myVAR, MyVAR2)



The ConvertCoords() function knows how to perform the conversion, and will

operate directly on the Lat/Long stored in

the member variables of the object.

You'd use something like this:



'***

Dim o As MyNewClass



Set o = New MyNewClass

o.Latitude = 123

o.Longitude = 456

If (CoordinatesRequireConversion) Then Call o.ConvertCoords



' Use o.Latitude and o.Longitude here



Set o = Nothing

'***



Hope this helps,



Mike





- Microsoft Visual Basic MVP -

E-Mail: EDais@mvps.org

WWW: EDais.mvps.org/









-