Results 1 to 12 of 12

Thread: referencing members of UDTs

  1. #1

    Thread Starter
    Registered User
    Join Date
    May 2004
    Posts
    3

    referencing members of UDTs

    Hi,

    I'm having a headache trying to assign a variable that will reference a member of a user defined type, i.e

    i've designed the type thus:

    public type Details
    A as variant
    B as variant
    ......(so on and so forth)
    end type

    later in the programme I want to reference particular members of an instance of this type depending on a case statement; my idea is to set a variable that will be set for each Case e.g

    case 1
    variable = "A"
    case 2
    variable = "B"
    etc

    i then try to reference the member like:

    if Details.variable = 0 then
    ....

    it doesn't work and throws the error: 'Method or data member not found.' Is there any way you can reference a data member in this manner??

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I'm afraid I dont understand what you are trying to do, hopefully this will help you!

    a Type is basically a data type (like String, or Integer), but it contains one or more data types.

    So with your Details type you could do the following:
    VB Code:
    1. Dim my_var as Details
    2. my_var.A = "hello"
    3. my_var.B = 37
    4.  
    5. ....
    6.  
    7. If my_var.B = 0 Then
    8.   Msgbox "B is 0"
    9. End If

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    You can not use a UDT as you wish if you want to use a select case with a UDT it has to be something like this.

    VB Code:
    1. Option Explicit
    2. Private Type test
    3. x As String
    4. y As Integer
    5. End Type
    6.  
    7. Private q As test
    8. Private Sub Command1_Click()
    9.     Select Case q.x
    10.         Case Is = 3
    11.             MsgBox "cought it"
    12.         Case Else
    13.             MsgBox "not there"
    14.     End Select
    15.  
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     q.x = "bombdrop"
    20.     q.x = 2
    21. End Sub

    Hope this helps

  4. #4

    Thread Starter
    Registered User
    Join Date
    May 2004
    Posts
    3
    thanks Si but maybe I've made it sound more complicated than it is: all I want is to be able to use a variable that will identify which member of Details i want to get the value of, like say I want Details.A but I'm only going to know at runtime that i want "A" so for different situations I may want "B" or "C" etc, that's why I created the variable in the Case statements.

    I would like to be able to reference the members dynamically by saying:

    .....Details.variable.....

    Hope that's a little clearer

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I'm afraid that's not possible using a Type, this is roughly equivalent tho:
    VB Code:
    1. Dim fred as Details
    2. ....
    3.  
    4. Select Case ....
    5. case 1
    6.    variable = fred.A
    7. case 2
    8.    variable = fred.B
    9. ...
    10.  
    11. if variable = 0 then

    It sounds as though an Array could well be more appropriate in this case.

  6. #6

    Thread Starter
    Registered User
    Join Date
    May 2004
    Posts
    3
    I was afraid you fellas would say that - I'll come up with some other work around.

    Cheers though

  7. #7
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    how about this

    VB Code:
    1. Option Explicit
    2.  
    3. Enum testType
    4. x
    5. y
    6. End Enum
    7.  
    8. Private Type test
    9. x As String
    10. y As Integer
    11. End Type
    12.  
    13. Private q As test
    14. Private GetType As testType
    15. Private Sub Form_Load()
    16.     'Add the values in the UDT
    17.     List1.AddItem x
    18.     List1.AddItem y
    19.  
    20.     q.x = "bombdrop"
    21.     q.y = 2
    22. End Sub
    23.  
    24. Private Sub List1_Click()
    25.     'on click assign the select listitem to then enum
    26.     Dim i As Integer
    27.     i = CInt(List1.Text)
    28.     GetType = i
    29.     Select Case GetType
    30.  
    31.         Case x
    32.             MsgBox q.x
    33.  
    34.         Case y
    35.             MsgBox q.y
    36.  
    37.     End Select
    38.  
    39.  
    40. End Sub

    hope this helps

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    you should use a enumeration instead of a udt

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    VB -> Using Objects

    You need a class and a collection to do something like what you want.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Try this
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    I'm not sure the Class/collection route is need I stil believ my second pice of code is more in tune with what the poster whats.
    The attached shows a cleaner version of my previous post.
    Attached Files Attached Files

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Sorry, but I think my approach is nicer. You're not using a "variable" to get to the member the user wants, but a select case to show one or other member. I think.... your code does not fit what smydo needs. But this is something he could only answer because your approach might fit perfectly on what he is doing.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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