Can this be done in VB.NET?

  • Thread starter Thread starter Bishop
  • Start date Start date
B

Bishop

If this (below) is something that can be done in VB.NET can you provide an
example, link to page explaining, or at least a keyword I can use to search
Google?

What I want to do is define a group of constants so that when something
calls my function, instead of remembering what 1,2,3, and 4 is, I can call
it using the group I already defined. For instance:


Const Group Definitions:
CanTypes
1 - RegularCola
2 - DietCola
3 - CaffeneFreeCola
4 - CaffeneFreeDietCola

Example Function:
Function GetPopDetails(CanType as CanTypes) as String
if CanType = 1 then
'Do stuff
'Return info
end if
End Function

Calling Function:
dim myString as string = GetPopDetails(CanTypes.RegulaCola)
 
If this (below) is something that can be done in VB.NET can you provide an
example, link to page explaining, or at least a keyword I can use to search
Google?

What I want to do is define a group of constants so that when something
calls my function, instead of remembering what 1,2,3, and 4 is, I can call
it using the group I already defined. For instance:


Const Group Definitions:
CanTypes
1 - RegularCola
2 - DietCola
3 - CaffeneFreeCola
4 - CaffeneFreeDietCola

Example Function:
Function GetPopDetails(CanType as CanTypes) as String
if CanType = 1 then
'Do stuff
'Return info
end if
End Function

Calling Function:
dim myString as string = GetPopDetails(CanTypes.RegulaCola)

Public Enum CanType
RegularCola
DietCola
CaffeineFreeCola
CaffeineFreeDietCola
End Enum

Function GetPopDetails (ByVal can As CanType) As String
Select can
Case CanType.RegularCola
'Do Stuff
Return Info
....
End Select
End Function

dim canInfo As String = GetPopDetails(CanTypes.RegularCola)
 
Exactly what I was looking for, thanks!

Tom Shelton said:
Public Enum CanType
RegularCola
DietCola
CaffeineFreeCola
CaffeineFreeDietCola
End Enum

Function GetPopDetails (ByVal can As CanType) As String
Select can
Case CanType.RegularCola
'Do Stuff
Return Info
....
End Select
End Function

dim canInfo As String = GetPopDetails(CanTypes.RegularCola)
 
Michael D. Ober said:
Because of the way VB handles "Nothing" as an enumerated value, I
always make my first enumeration item "= 1"

Public Enum CanType
RegularCola = 1
DietCola
CaffieneFreeCola
CaffeineFreeDietCola
End Enum

Defining the Enum CanType this way allows you to test for
non-initialized enumerators by testing against Nothing.

Mike Ober.

As an Enum is a value type, there are no uninitialized values. Only if
you have a Nullable(Of) it makes sense.
I would never use the type with Nothing because that would support VB's
concept of handling Nothing with value types, which I don't do.

Instead, I'd add a "None" value in the Enum, if that's a valid state.


Armin
 
Michael D. Ober said:
Armin, that's what I thought also until I had a piece of code do the
following

if <enumvar> = Nothing then
do "not initialized"
elseif <enumvar> = <firstoption>
do "first option processing"
...
end if

The <firstoption> path in the code never ran, so I took a deeper
look at what was going on and discovered that an uninitialized enum
value is zero.


I'm not sure how this is meant because the only situation that makes the
first path never run is, if you declare the 1st enum value = 1 and use
this value as the initial value (dim x as bla = firstenum). If you don't
assign an initial value, the path would run in any case unless you
explicitly assign an initial value <> 1, but if you do that, there is
never a reason to check for Nothing.

What I meant:
Instead of using 1 as the value of the 1st enum member in order to be
able to check for Nothing, I'd add another Enum value, like "None", and
use it as the initializer. Then you can also see an "uninitialized"
state without having to use Nothing in conjunction with a value type.
You'd be able to write "If enumvar = myenum.none then". Or, if I need a
distinction between having a value and having none, a Nullable(Of
CanType) is also appropriate.
The problem is that Nothing always evaluates to zero,
either as a pointer for object types or as an number for value
types.

I know. :-) I read your statement that you propagate the usage of
Nothing with value types. If that's true, I don't agree and, instead,
I'd use..see above.


Armin
 
On Thu, 22 May 2008 14:54:36 +0200, "Armin Zingler"

---snip---
What I meant:
Instead of using 1 as the value of the 1st enum member in order to be
able to check for Nothing, I'd add another Enum value, like "None", and
use it as the initializer. Then you can also see an "uninitialized"
state without having to use Nothing in conjunction with a value type.
You'd be able to write "If enumvar = myenum.none then". Or, if I need a
distinction between having a value and having none, a Nullable(Of
CanType) is also appropriate. ---snip---
Armin

It is not just a good idea to add the "None" value: It is also a
recommended best practice.

Straight from the horse's mouth:
http://msdn.microsoft.com/en-us/library/ms229058.aspx

"Do provide a value of zero on simple enumerations.
If possible, name this value None. If None is not appropriate, assign
the value zero to the most commonly used value (the default).
"

Regards,

Joergen Bech
 
Here is a neat little trick I came accross...

Public Class DaysOfWeek
Public Enum DoW
Sun
Mon
Tue
Wed
Thu
Fri
Sat
End Enum

Private Shared Days As Type = GetType(DoW)
Public Shared ReadOnly Names() As String = [Enum].GetNames(Days)

Shared Sub New()
'change any of the names you want here
Names(DoW.Sun) = "Sunday"
Names(DoW.Sat) = "Saturday"
End Sub

Shared Property Item(ByVal Index As DoW) As String
Get
Return Names(Index)
End Get
Set(ByVal value As String)
Names(Index) = value
End Set
End Property

End Class
 
Back
Top