By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Send self defined Types by Reference

P: 7
I have been teaching myself vb: I have defined a Type and want to pass that to other modules/subroutines. It works if all the code is within one module but am struggling to get it to pass the values to another module. Here is my test code that works in one module.

Type myNetwork1
myOne As String
myTwo As String
End Type
Dim theData As myNetwork1

Sub my123() 'Calls the public sub myTest1 to define my variables
MsgBox (theData.myOne) 'returns blank
MsgBox (theData.myTwo) 'returns blank
myTest1 myNetwork1 = myNetwork
MsgBox (theData.myOne) '123
MsgBox (theData.myTwo) '456
End Sub

Public Sub myTest1(ByRef myNetwork1)
theData.myOne = "123"
theData.myTwo = "456"
End Sub

The routine works in this instance. However, when I try pass the object between modules it returns an object error. Can you help me spot my ommision?
Note: I have been able to manage to return arguments on an individual basis but can't do it with a Type.

Module1
Sub my123() 'Want to call the public funtion in a different module
MsgBox (theData.myOne)
MsgBox (theData.myTwo)
myTest1 myNetwork1 = myNetwork
MsgBox (theData.myOne)
MsgBox (theData.myTwo)

End Sub

Module2
Type myNetwork1
myOne As String
myTwo As String

End Type
Dim theData As myNetwork1
Public Sub myTest1(ByRef myNetwork1)

theData.myOne = "123"
theData.myTwo = "456"

End Sub
Apr 18 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 634
I have been teaching myself vb: I have defined a Type and want to pass that to other modules/subroutines. It works if all the code is within one module but am struggling to get it to pass the values to another module. Here is my test code that works in one module.

Type myNetwork1
myOne As String
myTwo As String
End Type
Dim theData As myNetwork1

Sub my123() 'Calls the public sub myTest1 to define my variables
MsgBox (theData.myOne) 'returns blank
MsgBox (theData.myTwo) 'returns blank
myTest1 myNetwork1 = myNetwork
MsgBox (theData.myOne) '123
MsgBox (theData.myTwo) '456
End Sub

Public Sub myTest1(ByRef myNetwork1)
theData.myOne = "123"
theData.myTwo = "456"
End Sub

The routine works in this instance. However, when I try pass the object between modules it returns an object error. Can you help me spot my ommision?
Note: I have been able to manage to return arguments on an individual basis but can't do it with a Type.

Module1
Sub my123() 'Want to call the public funtion in a different module
MsgBox (theData.myOne)
MsgBox (theData.myTwo)
myTest1 myNetwork1 = myNetwork
MsgBox (theData.myOne)
MsgBox (theData.myTwo)

End Sub

Module2
Type myNetwork1
myOne As String
myTwo As String

End Type
Dim theData As myNetwork1
Public Sub myTest1(ByRef myNetwork1)

theData.myOne = "123"
theData.myTwo = "456"

End Sub
Hio

Its all a bit confusing, but maybe this is what you want

Module 1
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub my123() 'Want to call the public funtion in a different module
  4.     Dim TestData As myNetwork1
  5.  
  6.     MsgBox (TestData.myOne)
  7.     MsgBox (TestData.myTwo)
  8.     myTest1 TestData
  9.     MsgBox (TestData.myOne)
  10.     MsgBox (TestData.myTwo)
  11. End Sub
Module 2
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Type myNetwork1
  4.     myOne As String
  5.     myTwo As String
  6. End Type
  7.  
  8. Public Sub myTest1(ByRef thedata As myNetwork1)
  9.     thedata.myOne = "123"
  10.     thedata.myTwo = "456"
  11. End Sub
??


MTB
Apr 22 '08 #2

P: 7
MTB I THANK YOU

Now I can see it work it's so easy I feel stupid.
That will help me cut coding in lots of modules.

Maca
Apr 22 '08 #3

Expert 100+
P: 634
MTB I THANK YOU

Now I can see it work it's so easy I feel stupid.
That will help me cut coding in lots of modules.

Maca
Hi Maca

It's no trouble, just glad to help.

BTW I thought I was the 'Oldy self taught' you'r just a youngster !!

Cheers

MTB
Apr 22 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.