Difference between revisions of "Passing UDTs to Classes"

From HashVB
Jump to: navigation, search
m (Reverted edit of Jh2Woo, changed back to last version by Dee)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{VB6}}
 
This is actually extremely simple to do - all you need to do is declare the method (be it a property, function or sub) to which you are passing the UDT as Friend - like this:
 
This is actually extremely simple to do - all you need to do is declare the method (be it a property, function or sub) to which you are passing the UDT as Friend - like this:
  
  '-- In some module
+
'-- In some module
  Public Type myType
+
Public Type myType
      x As Integer
+
    x As Integer
      y As Integer
+
    y As Integer
  End Type
+
End Type
 
+
  '-- In the class
+
'-- In the class
  Friend Function myFunction(foo As myType) As Integer
+
Friend Function myFunction(foo As myType) As Integer
      myFunction = foo.x + foo.y
+
    myFunction = foo.x * foo.y
  End Function
+
End Function
 
+
  '-- Passing to the function
+
'-- Passing to the function
  Dim foo As myType
+
Dim foo As myType
  foo.x = 10: foo.y = 100
+
foo.x = 10: foo.y = 100
  Debug.Print myClassInstance.myFunction(foo) '-- Prints 1000 to Immediate window
+
Debug.Print myClassInstance.myFunction(foo) '-- Prints 1000 to Immediate window
  
 
Simple :-)
 
Simple :-)
  
<b>NOTE: </b>This will only work when using the class from within the same project in VB.  If compiled and used as a COM component, then any methods declared as 'Friend' will not appear.  See MSDN on the topic of the Friend keyword: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vakeyFriend.asp
+
'''NOTE:''' This will only work when using the class from within the same project in VB.  If compiled and used as a COM component, then any methods declared as 'Friend' will not appear.  See MSDN on the topic of the Friend keyword:<br>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vakeyFriend.asp

Latest revision as of 08:12, 23 July 2007

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

This is actually extremely simple to do - all you need to do is declare the method (be it a property, function or sub) to which you are passing the UDT as Friend - like this:

'-- In some module
Public Type myType
    x As Integer
    y As Integer
End Type

'-- In the class
Friend Function myFunction(foo As myType) As Integer
    myFunction = foo.x * foo.y
End Function

'-- Passing to the function
Dim foo As myType
foo.x = 10: foo.y = 100
Debug.Print myClassInstance.myFunction(foo) '-- Prints 1000 to Immediate window

Simple :-)

NOTE: This will only work when using the class from within the same project in VB. If compiled and used as a COM component, then any methods declared as 'Friend' will not appear. See MSDN on the topic of the Friend keyword:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vakeyFriend.asp