Results 1 to 20 of 20

Thread: User Defined type in Class Function

  1. #1

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Post User Defined type in Class Function

    I write the following testing code for checking how I can pass user defined type in function running well with in module. But when I made the same the function in class it
    Not works? Give me the Error

    Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types


    How can I make the same function in Class?
    VB Code:
    1. Public Type MyType ' In Module
    2.     First As String
    3.     Second As Long
    4. End Type
    5. Public Function TestType(ByRef TypeIN() As MyType) 'In Module
    6. Dim A As Integer
    7.     For A = 1 To 2
    8.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    9.         TypeIN(A).First = "Test" & A 'Set Data
    10.         TypeIN(A).Second = A 'Set Data
    11.     Next A
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15. Dim TestingType(1 To 2) As MyType
    16.     TestingType(1).First = "First A" ' Set Data
    17.     TestingType(1).Second = 1 'Set Data
    18.     TestingType(2).First = "First B"
    19.     TestingType(2).Second = 2
    20.    
    21.     Call TestType(TestingType())
    22.     MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
    23.     MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
    24.    
    25. End Sub
    Sorry for Bad English.

  2. #2
    Addicted Member
    Join Date
    Mar 2003
    Location
    Minneapolis, MN
    Posts
    151
    Try changing
    VB Code:
    1. Public Function TestType(ByRef TypeIN() As MyType) 'In Module
    2. Dim A As Integer
    3.     For A = 1 To 2
    4.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    5.         TypeIN(A).First = "Test" & A 'Set Data
    6.         TypeIN(A).Second = A 'Set Data
    7.     Next A
    8. End Function
    To
    VB Code:
    1. Public Function TestType(ByVal TypeIN() As Variant) 'In Module
    2. Dim A As Integer
    3.     For A = 1 To 2
    4.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    5.         TypeIN(A).First = "Test" & A 'Set Data
    6.         TypeIN(A).Second = A 'Set Data
    7.     Next A
    8. End Function

    See MSDN KB Article: Q185700 for more information.

  3. #3
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: User Defined type in Class Function

    Originally posted by VB IT
    Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types
    What this means, is that in your class module you defigned a custom type. Because the class module is the only thing that can 'see' that type it causes an error.

    Keep the definition of your custom type in a module.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Unhappy Err

    Originally posted by rlwhealdon
    Try changing
    VB Code:
    1. Public Function TestType(ByVal TypeIN() As Variant) 'In Module
    2. Dim A As Integer
    3.     For A = 1 To 2
    4.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    5.         TypeIN(A).First = "Test" & A 'Set Data
    6.         TypeIN(A).Second = A 'Set Data
    7.     Next A
    8. End Function
    I Get The Error
    Type Mismach Array or user Defined Type Expected
    Sorry for Bad English.

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Remove all the code from ya module and paste it into a class module, EXCEPT:
    VB Code:
    1. Public Type MyType ' In Module
    2.     First As String
    3.     Second As Long
    4. End Type
    Leave that in the module on it's own...

    Woka

  6. #6
    Addicted Member
    Join Date
    Mar 2003
    Location
    Minneapolis, MN
    Posts
    151
    Okay, here's how I got it to work.
    VB Code:
    1. ' Module1.bas
    2. Option Explicit
    3.  
    4. Public Type MyType ' In Module
    5.     First As String
    6.     Second As Long
    7. End Type
    8.  
    9. Public Function TestType(ByRef TypeIN() As MyType) 'In Module
    10.  
    11. Dim A As Integer
    12.     For A = 1 To 2
    13.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    14.         TypeIN(A).First = "Test" & A 'Set Data
    15.         TypeIN(A).Second = A 'Set Data
    16.     Next A
    17. End Function
    VB Code:
    1. ' Form1.frm
    2. Option Explicit
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     Dim TestingType(1 To 2) As MyType
    7.  
    8.     TestingType(1).First = "First A" ' Set Data
    9.     TestingType(1).Second = 1 'Set Data
    10.     TestingType(2).First = "First B"
    11.     TestingType(2).Second = 2
    12.  
    13.     TestType TestingType()
    14.     MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
    15.     MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
    16.    
    17. End Sub

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by Wokawidget
    Remove all the code from ya module and paste it into a class module, EXCEPT:
    VB Code:
    1. Public Type MyType ' In Module
    2.     First As String
    3.     Second As Long
    4. End Type
    Leave that in the module on it's own...

    Woka
    That sounds familiar...
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You can also do this. Create a class called Class1 that looks like
    VB Code:
    1. Option Explicit
    2.  
    3. Public First As String
    4. Public Second As Long
    Then slightly change your code to
    VB Code:
    1. Option Explicit
    2. Public Function TestType(ByRef TypeIN() As Class1) 'In Module
    3. Dim A As Integer
    4.     For A = 1 To 2
    5.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    6.         TypeIN(A).First = "Test" & A 'Set Data
    7.         TypeIN(A).Second = A 'Set Data
    8.     Next A
    9. End Function
    10.  
    11. Private Sub Command1_Click()
    12. Dim TestingType(1 To 2) As New Class1
    13.     TestingType(1).First = "First A" ' Set Data
    14.     TestingType(1).Second = 1 'Set Data
    15.     TestingType(2).First = "First B"
    16.     TestingType(2).Second = 2
    17.    
    18.     Call TestType(TestingType())
    19.     MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
    20.     MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
    21.    
    22. End Sub

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Exclamation Thanks!

    Originally posted by Wokawidget
    Remove all the code from ya module and paste it into a class module, EXCEPT:
    VB Code:
    1. Public Type MyType ' In Module
    2.     First As String
    3.     Second As Long
    4. End Type
    Leave that in the module on it's own...

    Woka
    First Thanks to all For There Kind Replies
    Wokawidget, I Get the Same Error By using Your method, may be I don’t understand it, here is the code.

    Error:

    Only Public User Defined Types Defined In Public Object Module Can Be Used As Parameters Or Return Types For Public Procedures of Class Modules Or as Fields of public User Defined Types.


    VB Code:
    1. 'In Module
    2. Public Type MyType
    3.     First As String
    4.     Second As Long
    5. End Type
    6.  
    7. 'Class1
    8. Public Function TestType(ByRef TypeIN() As MyType)
    9. Dim A As Integer
    10.     For A = 1 To 2
    11.         MsgBox TypeIN(A).First & TypeIN(A).Second ' Get Data
    12.         TypeIN(A).First = "Test" & A 'Set Data
    13.         TypeIN(A).Second = A 'Set Data
    14.     Next A
    15. End Function
    16.  
    17. 'From1.Frm
    18. Private Sub Command1_Click()
    19. Dim TestingType(1 To 2) As MyType
    20. Dim C As New Class1
    21.    
    22.     TestingType(1).First = "First A" ' Set Data
    23.     TestingType(1).Second = 1 'Set Data
    24.     TestingType(2).First = "First B"
    25.     TestingType(2).Second = 2
    26.    
    27.     Call C.TestType(TestingType())
    28.     MsgBox TestingType(1).First & TestingType(1).Second ' Get Data
    29.     MsgBox TestingType(2).First & TestingType(2).Second ' Get Data
    30.    
    31. End Sub
    Sorry for Bad English.

  11. #11

  12. #12

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Exapmle!

    Originally posted by Wokawidget
    Errrr...Hmmmm...that should work...I think

    Woka

    PS: It should be "Sorry for my bad English"
    Thank you for your Kind Reply
    Can You Please Give The Working Example
    Sorry for Bad English.

  13. #13

  14. #14

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB IT, Wokawidget objected to the Public variables in my class. You can of course do it the traditional way.

    VB Code:
    1. Option Explicit
    2.  
    3. Private mstrFirst As String
    4. Private mlngSecond As Long
    5.  
    6. Public Property Get First() As String
    7.  
    8.     First = mstrFirst
    9.    
    10. End Property
    11.  
    12. Public Property Let First(ByVal strFirst As String)
    13.  
    14.     mstrFirst = strFirst
    15.  
    16. End Property
    17. Public Property Get Second() As Long
    18.  
    19.     Second = mlngSecond
    20.    
    21. End Property
    22.  
    23. Public Property Let Second(ByVal strSecond As Long)
    24.  
    25.     mlngSecond = strSecond
    26.  
    27. End Property

  16. #16

  17. #17

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Re: Re: Exapmle!

    Originally posted by MartinLiss
    Mine works
    Thanks for Your Kind Reply
    Your Example works but a little bit more complex as compare to pass user defined type but end result is same. You right it is the other way to make the same result. But I want to clear my mind
    I can’t Pass User Define Type In Class Function!?
    Sorry for Bad English.

  18. #18

  19. #19

    Thread Starter
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381
    Originally posted by MartinLiss
    I don't think you can. Do a search of the General VB Coding Questions forum for user AND type AND class. There are a number of threads on the subject.
    Thank you for You Kind Reply

    After reading Tread I Think I Can’t Pass UDT (User Define Type) In Class Function.
    Sorry for Bad English.

  20. #20
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    VB IT


    No, you can not get there from here!!!. There are numerous other way to attempt to achieve this but I have found NONE of them reliable in all cases and only asking for trouble.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width