Results 1 to 15 of 15

Thread: Best Substitute for Enum of Strings?

  1. #1

    Thread Starter
    Member RHankinsJr's Avatar
    Join Date
    Nov 2011
    Location
    Texarkana, TX
    Posts
    47

    Best Substitute for Enum of Strings?

    For lack of better words, I would like to make an enum of strings. There are several masks for text boxes that I would like to standardize in our system.

    For simplicity lets say there are 2 masks:
    "PhoneNumber" which is "(999)000-0000"
    "Zip Code" which is "00000-9999"

    I would like to be able to reference these through out the entire program in a manner such as:

    DefaultMasks.ZipCode or DefaultMasks.PhoneNumber

    Since enums can not be of string type, what would your best suggestions be as to how I could do this.

    I had thought about a class with constants in it but I was not sure how to implement that properly.

    Thanks in advance for any help,

    RH

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Best Substitute for Enum of Strings?

    Dictionary (of String,String)

    Then you would create entries like:

    myDict.Add("ZipCode", "00000-9999")

    and you would access them with:

    myDict("ZipCode")

    That's not much different from your DefaultMasks.ZipCode.

    In fact, you could extend that by making an Enum called something like Masks, and making the Dictionary be:

    defaultMasks As New Dictionary(of Masks, String)

    That may not work. I'm not sure whether an Enum type can be the key of a Dictionary, and it would be a bit silly to do anyways, because that would turn referencing the dictionary into:

    defaultMasks(Masks.ZipCode)
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member RHankinsJr's Avatar
    Join Date
    Nov 2011
    Location
    Texarkana, TX
    Posts
    47

    Re: Best Substitute for Enum of Strings?

    Thank you Shaggy Hiker for the quick response. I guess that I have not seen any references to Dictionary before. I will do some investigating, it looks like that would be a good route to go.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Best Substitute for Enum of Strings?

    Yeah, they are a VERY useful tool.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member RHankinsJr's Avatar
    Join Date
    Nov 2011
    Location
    Texarkana, TX
    Posts
    47

    Re: Best Substitute for Enum of Strings?

    After thinking more into the task at hand I am not sure if a dictionary would be the way to go. I think that the best thing for me to do would be to do a single use class.

    For many of the situations a dictionary would be the correct path to take, but there are several "default" masks that would require a method to create.

    For example in most of our application the user wants all the input to go in in caps and the lengths vary between the various data requests in our application. a bin number is 8 length with each 2 character pair having a specific sub mask , a name is 50 in length, a state is 2 in length, etc...

    I would need to be able to do something like this.

    txtZipCode.mask = DefaultMasks.ZipCode
    txtBinNumber.mask = DefaultMasks.BinNumber
    txtLastName.mask = DefaultMasks.ustring(50)
    txtCity.mask = DefaultMasks.ustring(25)

    If I was using a dictionary I would need to have keys for "ustring50", "ustring25", "ustring10", etc...

    That is why I think that I would need to do the single use class instead. I could instantiate it at program load then reference it throughout the program effectively without much overhead.

    I might be overlooking something again, but I am glad I asked because the Dictionary is going to be of great use in another section of the program that is currently on the drawing board.
    Last edited by RHankinsJr; Feb 20th, 2012 at 05:47 PM.

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Best Substitute for Enum of Strings?

    Sounds like a Resource Dictionary would be useful to you. You create string entries, give them a name and VS will generate properties with the same name to access the values through.

    The other option would be Shared properties:
    vbnet Code:
    1. Public Class DefaultMasks
    2.     Public Shared ReadOnly Property PhoneNumber As String
    3.         Get
    4.             Return "(999)000-0000"
    5.         End Get
    6.     End Property
    7.     Public Shared ReadOnly Property ZipCode As String
    8.         Get
    9.             Return "00000-9999"
    10.         End Get
    11.     End Property
    12. End Class

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Best Substitute for Enum of Strings?

    I favor an approach like EG's however with a little bit more since i'm utterly spoiled by the intuitiveness of the VS IDE. You can get his shared properties approach to behave in the IDE as a true enum like the Color and Pen classes:-
    vbnet Code:
    1. ''' <completionlist cref="DefaultMasks"/>
    2. Public Class DefaultMasks
    3.     Private _mask As String
    4.     Private Sub New(ByVal mask As String)
    5.         _mask = mask
    6.     End Sub
    7.  
    8.     Public Shared ReadOnly Property PhoneNumber() As DefaultMasks
    9.         Get
    10.             Return New DefaultMasks("(999)000-0000")
    11.         End Get
    12.     End Property
    13.     Public Shared ReadOnly Property ZipCode() As DefaultMasks
    14.         Get
    15.             Return New DefaultMasks("00000-9999")
    16.         End Get
    17.     End Property
    18.     Public Shared Widening Operator CType(ByVal dm As DefaultMasks) As String
    19.         Return dm._mask
    20.     End Operator
    21.  
    22. End Class
    The key to making this work is the completionlist tag above the class declarations. Notice that ive also made a couple of adjustments to EG's example. First I have the properties return a DefaultMasks object instead of a string so we can declare a variable as DefaultMasks without provoking a type mismatch. Ive also overriden the assignment operator so you can assign an object of type DefaultMasks directly to a string as:-
    vbnet Code:
    1. Dim dm As DefaultMasks = DefaultMasks.PhoneNumber
    2.  
    3. Dim s As String = dm
    However, it may be better to override the ToString method of the class to make this assignment more explicit and obvious to anyone reading the code.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Best Substitute for Enum of Strings?

    See, and I would have just gone with a shared class/properties... negating the need for instanciating anything.
    Code:
    Public Class DefaultMasks
      
        Public Shared ReadOnly Property PhoneNumber() As String
            Get
                Return  "(999)000-0000"
            End Get
        End Property
        Public Shared ReadOnly Property ZipCode() As String
            Get
                Return "00000-9999"
            End Get
        End Property
    End Class
    But then I guess it depends on your use and how you want to use it. It does directly address the OP's desire to just use DefaultMasks.PhoneNumber when setting the mask.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Best Substitute for Enum of Strings?

    Quote Originally Posted by Niya View Post
    The key to making this work is the completionlist tag above the class declarations.
    I've never seen that before, can't find any information on Google, and can't see any difference when I try it in code. What does this do?

    [Edit: I would agree with tg that in this case an object enumeration seems slightly overkill. Normally I'd agree with that approach though, but there doesn't really seem to be a need for encoding the concept of a Mask. They are consumed by the UI controls as strings, and I don't see any particular reason to worry about type safety here. YMMV of course.]

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Best Substitute for Enum of Strings?

    Quote Originally Posted by Evil_Giraffe View Post
    I've never seen that before, can't find any information on Google
    Here is a page about it.

    Quote Originally Posted by Evil_Giraffe View Post
    can't see any difference when I try it in code. What does this do?
    It does this:-


    Quote Originally Posted by Evil_Giraffe View Post
    [Edit: I would agree with tg that in this case an object enumeration seems slightly overkill. Normally I'd agree with that approach though, but there doesn't really seem to be a need for encoding the concept of a Mask. They are consumed by the UI controls as strings, and I don't see any particular reason to worry about type safety here. YMMV of course.]
    I tend to favor it because it allows the intellisense to help me by narrowing the choices to only the choices that make sense. Its a matter of personal preference especially since I have a tendancy to define a lot of enums in my code whenever I need hard coded choices and a boolean is not enough.

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Best Substitute for Enum of Strings?

    And that's fine... but that necessitates the need to instantiate an object, just to get to a single property. And when you get down to someMaskTextBox.Mask = dm .... you've now lost the readability the OP is after. Sometimes simplicity is better. Actually, after thinking about it, I would probably use application settings to hold the masking, then expose a shared property/class to actually return the value, then that way I could swap out different maskings based on user preferences or even locale.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Best Substitute for Enum of Strings?

    Quote Originally Posted by techgnome View Post
    And that's fine... but that necessitates the need to instantiate an object, just to get to a single property.
    Actually, you don't. One object is instantiated by the type initialiser per value, and you access that instance through a shared property. It is just like an enum, except you get back a proper object. There's no cognitive overhead to the calling code at all.


    Quote Originally Posted by techgnome View Post
    Actually, after thinking about it, I would probably use application settings to hold the masking, then expose a shared property/class to actually return the value, then that way I could swap out different maskings based on user preferences or even locale.
    Resources would do all of that automatically.

    *cough*
    Quote Originally Posted by Evil_Giraffe View Post
    Sounds like a Resource Dictionary would be useful to you. You create string entries, give them a name and VS will generate properties with the same name to access the values through.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Best Substitute for Enum of Strings?

    my point is that if I see "sometextbox.mask = dm" .... That doesn't tell me what the mask is... I've lost the readability... you're requiring me to go back to where you instantiated dm to figure out what dm is... and then what the mask it. In a short procedure, it's a minor thing... but in a longer procedure... it could be an issue... and if I need more than one masking in the proc... now I need two variables... dmPhone, dmZipCode.... bah! ... I still don't see why a simple shared property that returns a simple string doesn't work.

    but hey, what ever boats your float.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Best Substitute for Enum of Strings?

    Oh, I see. I think that may have been just to demonstrate the type conversion?

    I agree about a simple shared property for this scenario, but I've used the technique outlined in slightly different scenarios to very good effect.

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Best Substitute for Enum of Strings?

    Quote Originally Posted by techgnome View Post
    my point is that if I see "sometextbox.mask = dm" .... That doesn't tell me what the mask is... I've lost the readability...
    -tg
    Like I said, if one prefers they can override the ToString method, in fact I strongly recommend it for the sake of readability:-
    vbnet Code:
    1. sometextbox.mask = dm.ToString
    Some people might say this approach is overkill. Perhaps it is so in this particular case but either way I really find this:-

    to be a lot more convenient than this:-
    Last edited by Niya; Feb 21st, 2012 at 04:02 AM.

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