|
Sure, simply cast them to the enumeration and it will work!
enum Numbers
{
One = 1,
Two = 2,
Three = 3
}
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
Numbers n = (Numbers) i;
Console.WriteLine(n.ToString());
}
}
}
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Martin# wrote: As enum is int Type it makes no sence to store it as a text I guess!
I think he's talking about the names of the enumeration values. For instance:
enum Numbers
{
One = 1,
Two = 2,
Three = 3
} He wants to store "One", "Two", and "Three" in the database and somehow use them to get 1, 2, and 3. I used something like this at my previous company, but I don't remember now.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Right. I want to store "One", "Two", "Three" and retrieve whatever values are required to correctly fill the enum variables in my program.
Sorry for the confusion.
|
|
|
|
|
I think I understood it.
Look at my posts.
All the best,
Martin
|
|
|
|
|
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Vikram A Punathambekar wrote:
I wanted to say, that I understood it, but recomended an other solution!
All the best,
Martin
|
|
|
|
|
Hello.
Here is what you were searching for.
Again I don't recomend it!
string actstring ="Dog";
FancyAnimal actAnimal = (FancyAnimal)Enum.Parse(typeof(FancyAnimal), actstring, true);
Be aware that it will throw an exception if it has no member of that fits!
All the best,
Martin
|
|
|
|
|
Thanks.
A man's gotta' do what a man's gotta' do. Even if it's not recommendable.
|
|
|
|
|
blackjack2150 wrote: A man's gotta' do what a man's gotta' do. Even if it's not recommendable.
But safety first, my friend.
You could validate the string, if you get all the members of the Enum and store it:
string[] members = Enum.GetNames(typof(FancyAnimal));
System.Collections.Specialized.StringCollection memberscollection = new System.Collections.Specialized.StringCollection();
memberscollection.AddRange(members);
Than just make the check:
string act ="dog";
if(memberscollection.Contains(act))
{
Hope it helps!
All the best,
Martin
|
|
|
|
|
Schweet.
Why do you not recommend it? Because of the exception it could throw, or because it's slow?
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Vikram A Punathambekar wrote: Why do you not recommend it? Because of the exception it could throw, or because it's slow?
No, just imagen that.
Software 1:
public enum FancyAnimal
{
dog= 0,
cat= 1,
squirel= 2,
}
text "squirel" is stored in db.
Than in Software 2:
public enum FancyAnimal
{
dog= 0,
cat= 1,
squirrel= 2,
}
No good!
All the best,
Martin
|
|
|
|
|
I assume you're trying to highlight the misspelling of Squirrel?
If the apps are unrelated, it doesn't make sense to make them share the same DB table for storage. Personally, I would go for a readable representation like Dog rather than 0 in the database.
I like your suggestion for a different reason - the code is simpler. I guess I could store the 0 along with Dog in the database and use that mapping.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hello,
Vikram A Punathambekar wrote: I assume you're trying to highlight the misspelling of Squirrel?
Exactelly!
Vikram A Punathambekar wrote: If the apps are unrelated, it doesn't make sense to make them share the same DB table for storage.
Sorry, for my bad example!
I off course ment:
YourProgramm Version 1:
and
YourProgramm Version 2:
Vikram A Punathambekar wrote: Personally, I would go for a readable representation like Dog rather than 0 in the database.
As a customer, I would be pretty pissed if I had to fill in my formulars again after a software update!
Only because there was a missspelling found in the code!
All the best,
Martin
|
|
|
|
|
Hi All,How can I create TreeView Control in WebForm.Im using ASP.Net1.1v.
Help me,please.Thank You in advance..
Hi,Im Razana
|
|
|
|
|
Hi Razana,
You can use the Internet Explorer Webcontrols of Microsoft for this. They add 3 more controls for you to use in ASP.Net 1.1 (I have blogged about the tabcontrol[^]).
You can download them Here[^].
Cu
Sven Cipido
|
|
|
|
|
can i save a graphich(ex rectangle) with DrawRectangle(bla...bla...) into some variabel ??
thx
Beginner C#
Yogyakarta - Indonesia
|
|
|
|
|
i think u can store the objects in form of binary file
rahul
|
|
|
|
|
i want a windowsservice which generates crystalreport in pdf automatically at regular duarations and mail has to send ..ex: i want to generate report automatically at 30 or 31st of everymonth and after generation it has to send mail itself automatically.can u please anybody help me out
rajesh m
|
|
|
|
|
thanks to all who help...
1)How do i read a 2 byte, 4 byte, or 8 byte value? I know how to read a one byte, but that only goes up to 255...
2)If any of you are familiar assembly, i need know now how to change (write) to an addresses hex dump, also known as Array of Byte (in most memory editing engines).
3) Reading/writing to a pointer I.E (0x1009624, offset: 0xb14), or any other pointer...
thanks in advanced
|
|
|
|
|
|
I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class?
for example
public class Patient
{
private string[] m_labValues
//this is what i am clueless about.
//how do i get down to elements of this array?
//i am sure that this implementation is wrong.
public string LabValues
{
get { return m_labValues; }
set { m_labValues = value; }
}
}
from my main class i desire an ability to directly manipulate the
elements...like:
private Patient patient = new Patient();
patient.LabValues=new String[6];
patient.LabValues[0]="B.P is 120,80";
patient.LabValues[1]="Blood Sugar is 160"
..
..
..
I'm totally clueless about implementing this.
I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it
directly with out using the get-set style
Can some one please help me?
dnahelix
|
|
|
|
|
You need to give your class an "indexer."
public class Patient
{
private string[] m_labValues;
public Patient(int labValueCount)
{
m_labValues = new string[labValueCount];
for(int i = 0; i < m_labValues.Length; i++)
{
m_labValues[i] = string.Empty;
}
}
public string this[int index]
{
get
{
return m_labValues[index];
}
set
{
m_labValues[index] = value;
}
}
}
private Patient patient = new Patient(6);
patient[0] = "B.P is 120,80";
patient[1] = "Blood Sugar is 160";
Error checking omitted for brevity's sake.
|
|
|
|
|
You could simply do the following:
public string[] LabValues
{
get { return m_labValues; }
set { m_labValues = value; }
}
}
This will work but is not considdered good coding. You should rather use a generic list or a public method to set the array.
public void SetLabValues(string[] ar)
{
m_labValues=ar;
}
|
|
|
|
|
try to read on "Indexers" in c#
leslie sanford gave an example of it also
|
|
|
|
|
I thank you all for your kind help. Leslie Sanford's example looks wonderful. I shall implement this and see. Thanks again
dnahelix
|
|
|
|