-
Jul 8th, 2011, 12:28 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jul 8th, 2011, 01:04 PM
#2
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
...
-
Jul 8th, 2011, 01:32 PM
#3
Thread Starter
Fanatic Member
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.
-
Jul 8th, 2011, 01:49 PM
#4
Thread Starter
Fanatic Member
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
-
Jul 8th, 2011, 02:00 PM
#5
Re: UDT with defined list
vb.net Code:
Structure MyView Enum Positions Left Right Top Bottom End Enum Private position As Positions Public ReadOnly Property Left() As Boolean Get Return position = Positions.Left End Get End Property Public ReadOnly Property Right() As Boolean Get Return position = Positions.Right End Get End Property Public ReadOnly Property Top() As Boolean Get Return position = Positions.Top End Get End Property Public ReadOnly Property Bottom() As Boolean Get Return position = Positions.Bottom End Get End Property Public Sub SetPostion(ByVal position As Positions) Me.position = position End Sub End Structure
-
Jul 8th, 2011, 02:37 PM
#6
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
....
-
Jul 9th, 2011, 03:36 AM
#7
Re: UDT with defined list
Originally Posted by Sitten Spynne
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|