-
Apr 3rd, 2021, 05:43 PM
#1
Thread Starter
Frenzied Member
Moving away from or extending Enum
Hello
I had declared an enum as below, and had been using it since then
vb Code:
Enum VoucherType
Purchase = 21
Sale = 31
etc
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:
Dim VType as VoucherType = VoucherType.Purchase
Dim s as string = VType.Field1
Dim s1 as string = VType.Field2
Dim s2 as string = VType.Field3
Thanks
-
Apr 3rd, 2021, 07:51 PM
#2
Re: Moving away from or extending Enum
You could use a second enum for extended information...
Code:
Enum vType
Field1
Field2
Field3
End Enum
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 3rd, 2021, 07:51 PM
#3
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 3rd, 2021, 07:56 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 3rd, 2021, 11:05 PM
#5
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.
-
Apr 6th, 2021, 08:56 AM
#6
Re: Moving away from or extending Enum
You may be looking for using custom attributes.
-
Apr 6th, 2021, 01:16 PM
#7
Thread Starter
Frenzied Member
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:
using cmd as New SqlCommand
..
..
cmd.Parameters.AddWithValue("VoucherType", Me.VType)
If Me.VType = EntryModes.Purchase Then
cmd.Parameters.AddWithValue("RequiresAudit", True)
cmd.Parameters.AddWithValue("DefaultAccount", "Acc1")
'some More fields hardcoded
ElseIf Me.VoucherType = EntryModes.Sales Then
cmd.Parameters.AddWithValue("RequiresAudit", False)
cmd.Parameters.AddWithValue("DefaultAccount", "Acc12")
'some More fields hardcoded
End If
....
....
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:
cmd.Parameters.AddWithValue("VoucherType", Me.VType)
cmd.Parameters.AddWithValue("RequiresAudit", Me.VType.RequiresAudit)
cmd.Parameters.AddWithValue("DefaultAccount", Me.VType.DefaultAccount)
'etc
-
Apr 6th, 2021, 07:53 PM
#8
Re: Moving away from or extending Enum
This is one option:
vb.net Code:
Public Enum VoucherType
VoucherType1
VoucherType2
VoucherType3
VoucherType4
VoucherType5
End Enum
Public Structure VoucherInfo
Public ReadOnly Property VoucherType As VoucherType
Public ReadOnly Property RequiresAudit As Boolean
Public ReadOnly Property DefaultAccount As String
Public Sub New(voucherType As VoucherType,
requiresAudit As Boolean,
defaultAccount As String)
Me.VoucherType = voucherType
Me.RequiresAudit = requiresAudit
Me.DefaultAccount = defaultAccount
End Sub
End Structure
vb.net Code:
Private voucherInfos As VoucherInfo() = {New VoucherInfo(VoucherType.VoucherType1, True, "Acc1"),
New VoucherInfo(VoucherType.VoucherType2, False, "Acc2")} 'Add more as required
vb.net Code:
Dim voucherInfo = voucherInfos.Single(Function(vi) vi.VoucherType = VType)
With cmd.Parameters
.AddWithValue("VoucherType", VType)
.AddWithValue("RequiresAudit", voucherInfo.RequiresAudit)
.AddWithValue("DefaultAccount", voucherInfo.DefaultAccount)
'Etc
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.
-
Apr 7th, 2021, 07:04 AM
#9
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
-
Apr 7th, 2021, 07:43 AM
#10
Re: Moving away from or extending Enum
Originally Posted by techgnome
^^^ This. ... (although in Java). ...We then create a "map" which would be a List(Of enum, class)
You mean a Dictionary(Of enum, class)???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 7th, 2021, 07:54 AM
#11
Re: Moving away from or extending Enum
Originally Posted by .paul.
You mean a Dictionary(Of enum, class)???
hehehe... yeah. That'd be the one.
-tg
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
|