1

The title probably isn't very accurate from a developer slang standpoint, but this is what I'm trying to achieve:

I have a structure z with one variable x and upon creating an instance of the structure z, I want x to be constrained to a list of types of x... so z.x = xType.1 or z.x = xType.2 where xType 1 and 2 are strings and x is also a string when it comes down to it.

Dim a As z
a.x = xType.1
Print(a.x)      'outputs "abc" because xType.1 = "abc"

EDIT 1:

Structure Z
    Dim X as String
End Structure

Sub Main()
    Dim a As Z
    a.X = "abc"
    Print(a.X) 'outputs "abc"
End Sub

This would be the simplest way where I can assign a.X any value... I want to achieve something like this:

Structure Z
    Dim X as ???
End Structure

Sub Main()
    Dim a As Z
    a.X = XType.abc
    Print(a.X) 'outputs "abc"
    a.X = XType.Zebra
    Print(a.X) 'outputs "Melons"
End Sub

So I have to make another structure or define those XTypes somewhere somehow.

10
  • What prevents you from using a List(Of String) as field in z(note that you should follow .NET naming conventions, so uppercase class/structure names). Commented Aug 20, 2013 at 22:33
  • I want z.x to only take 1 value, wouldn't a List allow x to take more than one? (and the lowercase usage was simply for demonstration purposes, I will be following convention in the application itself) Commented Aug 20, 2013 at 22:35
  • If it is just a single string, why don't you simply use a string field? Sorry, the question is not clear and compiling, readable code helps to understand a problem. Pseudo code is often self-defeating. Commented Aug 20, 2013 at 22:37
  • I edited the original question to give you a better idea of what I want... hope that makes better sense. Commented Aug 20, 2013 at 22:45
  • Just what, exactly, are you trying to do here - what is the point of all of this? Feels like an X-Y problem to me. Why do you need to do this? Commented Aug 20, 2013 at 23:12

1 Answer 1

1

I think what you may be wanting to use is an enum. You could do something like this:

Structure Z
    Public Enum XType
        abc
        bcd
    End Enum
    Dim X As XType
End Structure

Sub Main()
    Dim a As Z
    a.X = Z.XType.abc
End Sub

This way you could create predefined values for X, and it would be similar to how a MsgBox has different MsgBoxStyles you can choose from.

EDIT:

If you wanted to make it so that you didn't have to call ToString() to get the string value of X, you could try something like this (probably not the most effective, but it works):

Structure Z
    Public Enum XType
        abc
        bcd
    End Enum
    Private Xt As XType
    Public Property X As Object
        Get
            Return Xt.ToString()
        End Get
        Set(ByVal value As Object)
            If value.GetType().Name = "XType" Then Xt = value
        End Set
    End Property
End Structure

Or you could have two properties like this:

Public X as XType
Public Property XString as String
    Get
        Return X.ToString()
    End Get
End Property

This link may also be helpful: http://msdn.microsoft.com/en-us/library/essfb559(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Sign up to request clarification or add additional context in comments.

4 Comments

Can I assign abc and bcd string values? I need a.X to output a string.
Yeah, you can just use a.X.ToString() and you will get a string of the value of X.
Is there any other way in which I wouldn't have to use ToString in order to get the value?
@Theveloper, I edited my answer and put in a way you could get the string value of the Enum without having to call ToString() every time you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.