473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum but with values?

UJ
I know how to define enums, and they work great.

But is there any way to define the same time of thing but give them actual
values?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatu s as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum

This way, I can then later do the folllowing command and it would be true:

dim junk as TMCContentStatu s
junk = TMCContentStatu s.PrevApproved
if ( junk = "P" ) then....

I know I could do this right now with enums but I need to have specific
values to save into a database.

Any ideas on how to do this?

TIA

Jeffrey.
Nov 21 '05 #1
4 1484
But is there any way to define the same time of thing but give them actual
values?
Are you saying that the numerical values you can have in an enum
aren't "actual values"?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatu s as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum


Public Class TMCContentStatu s
Public Const PrevApproved As String = "P"
.... and so on ...
End Module

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
"UJ" <UJ@nowhere.com > schrieb:
I know how to define enums, and they work great.

But is there any way to define the same time of thing but give them actual
values?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatu s as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum

This way, I can then later do the folllowing command and it would be true:


Creating "enumeratio ns" of any data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenum s&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
> I know I could do this right now with enums but I need to have specific
values to save into a database.


Save enumerations as string in the database. This makes the database easier
to read then storing it as a value. Then use the following function to
convert the string into the enumation value:

Public Shared Function StringToEnum(By Val strString As String, ByVal
enuDummy As [Enum]) As [Enum]
REM Search for an enumeration member with the name strString.
REM enuDummy is a member of the enumeration to be searched.
REM When strString is found, that enumeration member is returned.
REM When strString is invalid or not found, enuDummy is returned.
REM When enuDummy is Nothing, an Exception is thrown.

Dim colNames As Array
Dim colValues As Array
Dim i As Integer

REM Check parameters.
If enuDummy Is Nothing Then
Throw New Exception("The enumeration object is 'Nothing'. This
is not allowed.")
End If
If strString Is Nothing Then
Return enuDummy
End If
If strString Is "" Then
Return enuDummy
End If
REM A string array of the names of the constants in enumType. The
elements of the
REM array are sorted by the values of the enumerated constants.
colNames = enuDummy.GetNam es(enuDummy.Get Type)
REM An Array of the values of the constants in enumType. The
elements of the array
REM are sorted by the values of the enumeration constants.
colValues = enuDummy.GetVal ues(enuDummy.Ge tType)
REM Search for the name.
For i = 0 To colNames.GetLen gth(0) - 1
If colNames.GetVal ue(i) = strString Then
Return colValues.GetVa lue(i)
End If
Next
REM String not found.
Return enuDummy

End Function

Example:

Public Enum TMCContentStatu s as Integer
Undefined
PrevApproved
ToBeAdded
ToBeDeleted
AddDenied
DeleteDenied
Approved
End Enum

Dim junk as TMCContentStatu s
strTemp = ....get string that represents enumeration value from database.
junk = StringToEnum( strTemp, TMCContentStatu s.Undefined )

Turning an enumeration value into a string is easy, just add '.ToString' at
the end.
Nov 21 '05 #4
UJ
Duh.....This is exactly what I need and works perfectly.....

Brain fart.....

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
But is there any way to define the same time of thing but give them actual
values?


Are you saying that the numerical values you can have in an enum
aren't "actual values"?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatu s as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum


Public Class TMCContentStatu s
Public Const PrevApproved As String = "P"
... and so on ...
End Module

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4852
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
9
3141
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a conversion is undefined unless the value is within the range of the enumeration. For example: enum flag { x=1, y=2, z=4, e=8 }; // range 0:15 flag f1 = 5; // type error: 5 is not of type flag flag f2 = flag(5); // ok: flag(5) is of type flag and...
0
1746
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with consecutive values. The problem is that some functions should only accept some of the ranges. To store values of more than one range/enum in variable one has to use int as storage which imho compromises type safety because any function that wants to...
6
796
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs (this is why I started the TOKEN enum off at '1000' so I had plenty of space at the
3
16078
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
18
11371
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
0
1307
by: Ben Finney | last post by:
Howdy all, I've uploaded enum 0.3 to the Cheeseshop. <URL:http://cheeseshop.python.org/pypi/enum/> Enumerations are now sequences, iterable (as before) *and* indexable:: >>> from enum import Enum >>> Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
10
23617
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize FOO_STORE to be, say -10, I get a warning about unsigned comparison and I'm seeing an infinite loop. If I do initialize FOO_STORE = -10, I don't see any warnings. No infinite loop.
34
11205
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
6
10060
by: bsma1 | last post by:
I building a web service that has an enum I want the consuming application to be able to use. I have the enum declared in the web service as: public enum myEnum { ONE = 1, TWO = 2, };
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.