Results 1 to 11 of 11

Thread: Moving away from or extending Enum

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Moving away from or extending Enum

    Hello

    I had declared an enum as below, and had been using it since then
    vb Code:
    1. Enum VoucherType
    2.      Purchase  = 21
    3.      Sale = 31
    4.      etc
    5. End Enum

    Now i wish to add and use some extra information (more 3 fields) about each Vouchertype, is it possible to do so in enum, or can u please suggest an better alternative for the same

    It could be easier if i can use the 3 extra field in below fashion

    vb Code:
    1. Dim VType as VoucherType = VoucherType.Purchase
    2.  
    3. Dim s as string = VType.Field1
    4. Dim s1 as string = VType.Field2
    5. Dim s2 as string = VType.Field3

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Moving away from or extending Enum

    You could use a second enum for extended information...

    Code:
    Enum vType
        Field1
        Field2
        Field3
    End Enum

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Moving away from or extending Enum

    Doesn't really make sense what you are trying (what are you trying?).
    Enums are constants so doing this:
    Dim VType as VoucherType = VoucherType.Purchase kinda defeats the purpose
    Because you can straight out do dim something as integer = VoucherType.Purchase

    If you only use this with constants then OK but if you need to change values the a structure or a class with public properties seems a better idea.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Moving away from or extending Enum

    If you want to give your enum values distinct values, use powers of 2 i.e. 0, 1, 2, 4, 8
    Enum with distinct integer values can be manipulated with bitwise comparisons, which is especially useful if something is both Field1 and Field2, or Field2 and Field3

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Moving away from or extending Enum

    What you're suggesting doesn't really make sense. An Enum is just a set of numbers with a human-friendly label. Maybe what you actually need is a class with one property of your Enum type and then whatever other properties you need as well. It's hard to say for sure because, rather than explain what you're trying to achieve, you've told us how you're trying to achieve it and that makes no sense. If you explain the end game then we can explain how to get there. If you explain how you're trying to get there and that doesn't make sense then we're left to speculate.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Moving away from or extending Enum

    You may be looking for using custom attributes.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,148

    Re: Moving away from or extending Enum

    If you explain the end game then we can explain how to get there
    till now i had to deal only two VoucherTypes, i.e sales and purchase, which i was handling by hard coding as below

    vb Code:
    1. using cmd as New SqlCommand
    2. ..
    3. ..
    4. cmd.Parameters.AddWithValue("VoucherType", Me.VType)
    5.  
    6. If Me.VType = EntryModes.Purchase Then
    7.    cmd.Parameters.AddWithValue("RequiresAudit", True)
    8.    cmd.Parameters.AddWithValue("DefaultAccount", "Acc1")
    9.    'some More fields hardcoded
    10. ElseIf Me.VoucherType = EntryModes.Sales Then
    11.     cmd.Parameters.AddWithValue("RequiresAudit", False)
    12.     cmd.Parameters.AddWithValue("DefaultAccount", "Acc12")
    13.    'some More fields hardcoded
    14. End If
    15. ....
    16. ....

    But Now I need to add 5 more VoucherTypes, so an If Then statement will be cumbersome.

    So i thought of doing it as below (if possible)

    vb Code:
    1. cmd.Parameters.AddWithValue("VoucherType", Me.VType)
    2. cmd.Parameters.AddWithValue("RequiresAudit", Me.VType.RequiresAudit)
    3. cmd.Parameters.AddWithValue("DefaultAccount", Me.VType.DefaultAccount)
    4. 'etc

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Moving away from or extending Enum

    This is one option:
    vb.net Code:
    1. Public Enum VoucherType
    2.     VoucherType1
    3.     VoucherType2
    4.     VoucherType3
    5.     VoucherType4
    6.     VoucherType5
    7. End Enum
    8.  
    9. Public Structure VoucherInfo
    10.  
    11.     Public ReadOnly Property VoucherType As VoucherType
    12.     Public ReadOnly Property RequiresAudit As Boolean
    13.     Public ReadOnly Property DefaultAccount As String
    14.  
    15.     Public Sub New(voucherType As VoucherType,
    16.                    requiresAudit As Boolean,
    17.                    defaultAccount As String)
    18.         Me.VoucherType = voucherType
    19.         Me.RequiresAudit = requiresAudit
    20.         Me.DefaultAccount = defaultAccount
    21.     End Sub
    22.  
    23. End Structure
    vb.net Code:
    1. Private voucherInfos As VoucherInfo() = {New VoucherInfo(VoucherType.VoucherType1, True, "Acc1"),
    2.                                          New VoucherInfo(VoucherType.VoucherType2, False, "Acc2")} 'Add more as required
    vb.net Code:
    1. Dim voucherInfo = voucherInfos.Single(Function(vi) vi.VoucherType = VType)
    2.  
    3. With cmd.Parameters
    4.     .AddWithValue("VoucherType", VType)
    5.     .AddWithValue("RequiresAudit", voucherInfo.RequiresAudit)
    6.     .AddWithValue("DefaultAccount", voucherInfo.DefaultAccount)
    7.     'Etc
    8. End With
    You could use a Dictionary instead of a List and a class instead of a structure (you should use a class if there are more than four properties) but basically you just need to associate the required data with each field of the Enum in on place and then get it back using the Enum value when you need it.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Moving away from or extending Enum

    ^^^ This. Can't recommend it enough. That's essentially how we've got a number of things hooked up in our system that I work on (although in Java). We have a class that's what we calla a POJO (Plain Ordinary Java Object)... all it has is getters and setters, nothing else. Then we have a listing of enums of all our "objects" what ever they are. We then create a "map" which would be a List(Of enum, class) and then fill it... the enum becomes the key and the class becomes the value. When we need to add a new value to it, which is what we're doing now (process of adding some 20+ more) it's a simple as adding to the enum, then the map (plus of course what ever functionality is associated with the new entry).

    -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??? *

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Moving away from or extending Enum

    Quote Originally Posted by techgnome View Post
    ^^^ This. ... (although in Java). ...We then create a "map" which would be a List(Of enum, class)
    You mean a Dictionary(Of enum, class)???

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

    Re: Moving away from or extending Enum

    Quote Originally Posted by .paul. View Post
    You mean a Dictionary(Of enum, class)???
    hehehe... yeah. That'd be the one.

    -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??? *

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