Results 1 to 7 of 7

Thread: [RESOLVED] UDT with defined list

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [RESOLVED] UDT with defined list

    Hard to describe this but.

    Instead of a UDT containing a string variant ie
    If .view="Left" then

    Can it be changed to a list of defined entries in the UDT
    If .view.Left then

    I could define the subentities as boolean but each time I change one I would have to change the others
    .view.Left = True
    .view.Right=False
    .view.Top=False

    It would just make coding a bit faster. I don't know if it's possible but I've been surprised in the past when I didn't think something was possible.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: UDT with defined list

    I think you're speaking the wrong language. This forum's for VB .NET.

    In .NET I'd use an enum to represent the value; much more reliable than a string. If desired, I could expose 4 properties that return a boolean result based on the enum, but it seems superflous. How is one of these better than the other?

    Code:
    If .view = "Left" Then
        ' Do left
    ElseIf .view = "Right" Then
        ' Do right
    ...
    Code:
    If .view = Direction.Left Then
        ' Do left
    ElseIf .view = Direction.Right Then
        ' Do right
    ...
    Code:
    If .view.Left Then
        ' Do left
    ElseIf .view.Right Then
        ' Do right
    ...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: UDT with defined list

    Not to good at .NET. More adapt to VB6 unfortunately.
    How do I apply that in a structure?
    I googled ENUM. not much help.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: UDT with defined list

    The only reason I ask is for intellisense to pick it up.
    Bit of a pain writing = "string"
    If I spell it incorrectly then I need to deal with the errors

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: UDT with defined list

    vb.net Code:
    1. Structure MyView
    2.     Enum Positions
    3.         Left
    4.         Right
    5.         Top
    6.         Bottom
    7.     End Enum
    8.  
    9.     Private position As Positions
    10.     Public ReadOnly Property Left() As Boolean
    11.         Get
    12.             Return position = Positions.Left
    13.         End Get
    14.     End Property
    15.  
    16.     Public ReadOnly Property Right() As Boolean
    17.         Get
    18.             Return position = Positions.Right
    19.         End Get
    20.     End Property
    21.  
    22.     Public ReadOnly Property Top() As Boolean
    23.         Get
    24.             Return position = Positions.Top
    25.         End Get
    26.     End Property
    27.  
    28.     Public ReadOnly Property Bottom() As Boolean
    29.         Get
    30.             Return position = Positions.Bottom
    31.         End Get
    32.     End Property
    33.  
    34.     Public Sub SetPostion(ByVal position As Positions)
    35.         Me.position = position
    36.     End Sub
    37. End Structure
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: UDT with defined list

    pradeep posted the implementation of what I argued is the worst implementation. Here's how I'd do it in .NET.

    An "enum" is short for "enumerated value". It's a way to say, "I want a variable that can only have a certain set of values, and I want names for those values." Which is what you want; you want Left, Right and maybe others to be valid. So this Enum will work:
    Code:
    Public Enum Direction
        Left
        Right
        Up
        Down
    End Enum
    Under the covers, those names get assigned a number. You don't need to worry about that. Now, in the class that needs a property of that type:
    Code:
    Public Class SomeClass
        Private _direction As Direction
    
        Public Property Direction As Direction
            Get
                Return _direction
            End Get
            Set(ByVal value As Direction)
                _direction = value
            End Set
        End Property
    
    End Class
    Now when you set or test the values, you can use the symbolic name for the direction. Select Case statements are a good fit:
    Code:
    Select Case _myObject.Direction
        Case Direction.Left
            ' Do left
        Case Direction.Right
            ' Do right
        ....

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: UDT with defined list

    Quote Originally Posted by Sitten Spynne View Post
    pradeep posted the implementation of what I argued is the worst implementation.
    Yes, its a bad implementation... but its exactly what he needed... something that could be set to true/false and other property automatically changing to opposite of it.

    I understand that ideally it should have been just one property. But it is always not possible to change all the related code and we have to do workarounds many times.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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