Send self defined Types by Reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • macmacro64
    New Member
    • Apr 2008
    • 7

    #1

    Send self defined Types by Reference

    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
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by macmacro64
    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
    Code:
    Option Explicit
    
    Sub my123() 'Want to call the public funtion in a different module
        Dim TestData As myNetwork1
        
        MsgBox (TestData.myOne)
        MsgBox (TestData.myTwo)
        myTest1 TestData
        MsgBox (TestData.myOne)
        MsgBox (TestData.myTwo)
    End Sub
    Module 2
    Code:
    Option Explicit
    
    Type myNetwork1
        myOne As String
        myTwo As String
    End Type
    
    Public Sub myTest1(ByRef thedata As myNetwork1)
        thedata.myOne = "123"
        thedata.myTwo = "456"
    End Sub
    ??


    MTB

    Comment

    • macmacro64
      New Member
      • Apr 2008
      • 7

      #3
      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

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Originally posted by macmacro64
        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

        Comment

        Working...