Results 1 to 35 of 35

Thread: [RESOLVED] Problem when passing UDTs to subs

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Problem when passing UDTs to subs

    In a module I have the following declarations:
    Code:
    Public Type MyUDT
       X As Single
       Y() As Single
    End Type
    '
    Public MyUDTInstance() As MyUDT
    In a different module:
    Code:
    Sub MySub(DummyVarName)
    '...
    ' Some operations are performed here
    'with the quantities:
    'DummyVarName(SomeIndex).X
    'and
    'DummyVarName(SomeIndex).Y(SomeOtherIndex)
    '...
    End Sub
    Finally, in a form there's the calling statement:
    Code:
    Call MySub(MyUDTInstance)
    Of course, MyUDTInstance and MyUDTInstance(SomeIndex).Y(SomeOtherIndex) have been previously ReDimensioned and given values.

    Now, when the sub is called I get the error you can see in the attached picture which can be translated into English more or less as follows:

    "Compiler error.

    Only those user defined types from public obejct modules can be passed to functions linked at run time or forced to or from a variant"

    Anyone knows the reason?
    Attached Images Attached Images  
    Last edited by krtxmrtz; Jun 28th, 2007 at 05:16 PM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Problem when passing UDTs to subs

    try
    Code:
    Sub MySub(DummyVarName as MyUDT) 
    ... 
    End Sub
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by robbedaya
    try
    Code:
    Sub MySub(DummyVarName as MyUDT) 
    ... 
    End Sub
    Now I get

    "Compiler error.

    The type of argument of ByRef does not coincide"
    Attached Images Attached Images  
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem when passing UDTs to subs

    I've doing something like that last night (..when trying first steps in VB2005).
    Didn't get any errors there?
    Since your UDT is Public, did you try to declare the Sub without the handing over the UDT like:
    Code:
    Sub MySub()
    ..
    End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by opus
    I've doing something like that last night (..when trying first steps in VB2005).
    Didn't get any errors there?
    Since your UDT is Public, did you try to declare the Sub without the handing over the UDT like:
    Code:
    Sub MySub()
    ..
    End Sub
    OK, I've tried and it works. But I'd like to know what was wrong, I'd like my sub to be as general as possible.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    All right, after trying out various possibilities I've found the correct syntax that won't fire any error. This is it:

    Code:
    Public Type MyUDT
       X As Single
       Y() As Single
    End Type
    '
    Public MyUDTInstance() As MyUDT
    In a different module:
    Code:
    Sub MySub(DummyVarName() As MyUDT)
    '...
    '
    End Sub
    The calling statement in a form:
    Code:
    Call MySub(MyUDTInstance())
    I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by krtxmrtz
    I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
    I always figured you as a guru, now I'm lost
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by opus
    I always figured you as a guru, now I'm lost
    No kidding !!!???
    Programming is just a hobby for me, I don't make a living out of it, I make programs for my own use and that of my workmates'.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by krtxmrtz
    All right, after trying out various possibilities I've found the correct syntax that won't fire any error. This is it:

    Code:
    Public Type MyUDT
       X As Single
       Y() As Single
    End Type
    '
    Public MyUDTInstance() As MyUDT
    In a different module:
    Code:
    Sub MySub(DummyVarName() As MyUDT)
    '...
    '
    End Sub
    The calling statement in a form:
    Code:
    Call MySub(MyUDTInstance())
    I hope some guru sets his/her eyes on this thread and provides a clear explanation that makes sense.
    MyUDT is not an array type. Your sub is using it as an array, so you have to declare it as an array in the sub
    Code:
    MyName() As MyType
    the usual way of declaring an array. The caller has to pass the sub an array too.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] Problem when passing UDTs to subs

    About your last code, you don't need to send MyUDTInstance() as parameter, since its scope is the whole project, it can be used anywhere.

    but if you want to send it as parameter, something like this would be better..

    Module:
    Code:
    Public Type MyUDT
       X As Single
       Y() As Single
    End Type
    
    Public Sub MySub(DummyVarName() As MyUDT)
        'So something here
    End Sub
    Form:
    Code:
        Dim lUdt() As MyUDT
        
        'Do something with lUdt
        Call MySub(lUdt())
    Last edited by jcis; Jun 26th, 2007 at 04:32 PM.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by krtxmrtz
    OK, I've tried and it works. But I'd like to know what was wrong, I'd like my sub to be as general as possible.
    Then use classes/collections rather than UDTs, and receive it As Object.

  12. #12

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    Quote Originally Posted by jcis
    About your last code, you don't need to send MyUDTInstance() as parameter, since its scope is the whole project, it can be used anywhere.
    ...
    Actually MyUDT was declared initially in the form, but then I started fooling around with the code to test the various possibilities and it ended up in the same module as the type declaration.

    But thanks for the clarification anyway. If anything at least it's a reminder for me to restore the previous situation.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by Al42
    MyUDT is not an array type....
    Is there such a thing as an array type, like

    Type Whatever()
    '...
    '...
    End Type

    ???
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by leinad31
    Then use classes/collections rather than UDTs, and receive it As Object.
    Believe it or not I have never used classes (of my own) just because I've never found the time to learn. All the VB I know I have learned either on my own or -to a large extent- in the forums. But I tend to learn new things only when I really need them and clearly see the benefit. And I don't see the benefit of using classes just because I'm hardly acquainted with them. Maybe I'd need a simple template to start with and learn gradually from that. Any hint, suggestion, tutorial, link in that direction will be most welcome.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    Actually the simplest class is a pure data class. You would define the elenments the same way you would in a module but you could reference them differently.

    IE.

    Code:
    In a Class Module names clsData
    
    Public sStr as String
    Public lngOffset as Long
    That's it!!!

    In your program you would use it like this:
    Code:
    Dim myClass as clsData
    
       Set myClass = New clsData
    
       myClass.lngOffset = 5677
       myClass.sStr = "Test"
    That's it. You can pass myClass as an object and place it into arrays and collections and create as may to hold the data as you need. In my opinion this is more flexible that using a UDT.

  16. #16

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    Quote Originally Posted by randem
    Actually the simplest class is a pure data class. You would define the elenments the same way you would in a module but you could reference them differently.

    IE.

    Code:
    In a Class Module names clsData
    
    Public sStr as String
    Public lngOffset as Long
    That's it!!!

    In your program you would use it like this:
    Code:
    Dim myClass as clsData
    
       Set myClass = New clsData
    
       myClass.lngOffset = 5677
       myClass.sStr = "Test"
    That's it. You can pass myClass as an object and place it into arrays and collections and create as may to hold the data as you need. In my opinion this is more flexible that using a UDT.
    Thanks but I still don't see the advantage:
    1. In which sense is it more flexible?
    2. Can I make an array such as "Set myClass() = New clsData" and ReDim it at a later stage?
    3. What do you exactly mean by passing it as an aobject? (To subs and functions?)
    4. Do I have to destroy the class instance at program exit or at any other point in the program?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    1 - In every use

    2 - No use dim myClass() as clsData, then redim it

    3 - Since myClass is an object now, you can pass it like this.
    Code:
    Sub MySub(ArrayName() As Object)
    '...
    '
    End Sub
    This way you are passing an array of classes.

    I believe you can even do this
    Code:
    Sub MySub(ArrayName() As clsData)
    '...
    '
    End Sub

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    You can also add it to a collection
    Code:
       colData.Add myClass, myClass.sStr  ' or whatever key you would need

  19. #19

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    Quote Originally Posted by randem
    ...
    You sound very convincing...
    How about my fourth?
    "Do I have to destroy the class instance at program exit or at any other point in the program?"
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    4 - Yes... Preferable when you are done with it.

    Sorry.

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    You can also code the class to enumerate thru the variables if you want. You can't do that with a UDT!

  22. #22

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    Quote Originally Posted by randem
    You can also code the class to enumerate thru the variables if you want. You can't do that with a UDT!
    For example...? (Never done that!)
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Re: [RESOLVED] Problem when passing UDTs to subs

    You would then add a Emum function to your class that would return each data element name (type, length, etc...) from the class.

  24. #24

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    Quote Originally Posted by randem
    You would then add a Emum function to your class that would return each data element name (type, length, etc...) from the class.
    I see. In your example above,
    Code:
    In a Class Module names clsData
    
    Public sStr as String
    Public lngOffset as Long
    How would that work?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  25. #25

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    As far as I know Enum provides a useful way to remind you of constant names like intellisense. How can it be used to get the data information, as you say, like length, type, etc?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  26. #26

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Problem when passing UDTs to subs

    I have tried on my own and had to quit right after I'd started for it does not allow me to declare the variables below as public (produces an error)

    Code:
    In a Class Module names clsData
    
    Public sStr as String
    Public lngOffset as Long
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  27. #27
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Problem when passing UDTs to subs

    Quote Originally Posted by krtxmrtz
    Is there such a thing as an array type, like

    Type Whatever()
    '...
    '...
    End Type

    ???
    Actually, no, it's an array of type.

    Type Whatever
    '...
    '...
    End Type

    Dim myWhatever() as Whatever

    But you declared your variable of your type as a single element, then used it as an array.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Code:
    In a Class Module names clsData
    This is a statement not code...

    Sure it works, I use Pure Data Classes extensively.

  29. #29

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Quote Originally Posted by randem
    Code:
    In a Class Module names clsData
    This is a statement not code...

    Sure it works, I use Pure Data Classes extensively.
    All right, it works with simple data definitions, but not with UDTs:
    Code:
    Option Explicit
    
    Public Start As Single
    Public Finish As Single
    Public Step As Single
    Public Kounter As Long
    
    Public Type Animal
        Size As Single
        CanFly As Boolean
        CanSwim As Boolean
        NumberOfLegs As Integer
    End Type
    It gives an error when run so, looks like it doesn't allow UDTs here. Shall I then dismiss using classes?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  30. #30
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    The point of using classes is to drop the use of UDTs (eg. such is the case with Java)

  31. #31

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Quote Originally Posted by leinad31
    The point of using classes is to drop the use of UDTs (eg. such is the case with Java)
    Then is it possible to implement the following using classes?
    VB Code:
    1. Type Series
    2.     nPts As Integer
    3.     Nam As String
    4.     X() As Single
    5.     Y() As Single
    6. End Type
    7. Type DataChart
    8.     ID As String
    9.     nSeries As Integer
    10.     S() As Series
    11. End Type
    12. Public DC() As DataChart
    13. Public nDDCs As Integer
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  32. #32
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Yes. You simply have to look around to see samples.

    If I'm not mistaken you can retain the array of primitive data types (like the SubItems() property of listview listitem).

    The array of UDT you will implement as a collection of objects (like the ListItems collection of listview). You can have a heirarchy (such as each ListItem in ListItems collection having ListSubItems collection).

  33. #33

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Quote Originally Posted by leinad31
    Yes. You simply have to look around to see samples.

    If I'm not mistaken you can retain the array of primitive data types (like the SubItems() property of listview listitem).

    The array of UDT you will implement as a collection of objects (like the ListItems collection of listview). You can have a heirarchy (such as each ListItem in ListItems collection having ListSubItems collection).
    Great, but I've never worked with collections of my own. Could you provide a couple of lines to show how you'd declare/define a suitable collection for the example I've presented in my previous post?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  34. #34
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    If you have MSDN Library installed, look for "House of Straw"

  35. #35

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED but last issue NOT YET] Problem when passing UDTs to subs

    Quote Originally Posted by leinad31
    If you have MSDN Library installed, look for "House of Straw"
    I'll take a look at it. Thanks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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