Help creating an enumeration

  • Thread starter Randel Bjorkquist
  • Start date
R

Randel Bjorkquist

Hers's my question. I want the ability to set a property of an object I've
created, from outside of the class code (ie... inside of "Main" or an
application event), with an enum. Something like this:

CODE IN "Main":
TItem MyItem = new TItem();
MyItem.Discount = TItem.eDisount.DISC_0;

I believe I should be able to do this. I just can't seem to find it. What
I want to imitate is the code for a "Key" event, like "KeyDown", "KeyPress"
or "KeyUp". In the "KeyDown" event, I can compare the
"System.Windows.Forms.KeyEventArgs e" parameter to what looks like a handful
of enums for the "Forms" class. I do a comparison like this:

CODE IN "KeyDown" EVENT
private void lstbox_FileNames_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e){
if(e.KeyCode == System.Windows.Forms.Keys.Delete){
//DO SOMETHING HERE
}//END OF "if(e.KeyCode == System.Windows.Forms.Keys.Delete)"
}//END OF "lstbox_FileNames_KeyDown" METHOD

What I don't get is how to get the list of enumerators to show up outside of
my class. I have pasted a quick example of my class code below and am
looking forwards to any help I can get.

Thanks in advance,

Randel Bjorkquist
**********************************************************
**********************************************************
using System;

namespace Assignment_8
{
/// <summary>
/// Summary description for TItem.
/// </summary>
public class TItem
{
//---------------------------------------------------------------------------
public enum eDiscount {DISC_0 = 0, DISC_3, DISC_5};

//---------------------------------------------------------------------------
private eDiscount FDiscount;

//---------------------------------------------------------------------------
public TItem(){
FDiscount = eDiscount.DISC_0;
}//END OF "TItem" DEFAULT CONSTRUCTOR -----------------------

//---------------------------------------------------------------------------
public decimal Discount{
get{
decimal TmpVal = 0;

switch(FDiscount){
case eDiscount.DISC_0: TmpVal = 1M; break;
case eDiscount.DISC_3: TmpVal = 2M; break;
case eDiscount.DISC_5: TmpVal = 3M; break;
}//END OF "" SWITCH-STATEMENT

return(TmpVal);
}//END OF "get" ACCESSOR

set{FDiscount = value;}//END OF "set" ACCESSOR
}//END OF "Discount" ACCESSOR
METHOD ----------------------------------------
}//END OF "TItem" CLASS
}//END OF "Assignment_8" NAMESPACE IN FILE "TItem.cs"
 
A

Angel J. Hernández M.

Hope this may help you...


using System;

namespace ConsoleApplication1 {
class Class1 {
[STAThread]
static void Main(string[] args) {
EnumTest o = new EnumTest();
o.MyProperty = EnumTest.Test.Blue; // Is this the way you wanna do it?
Console.WriteLine(o.MyProperty.ToString());
Console.ReadLine();
}
}

class EnumTest {
public enum Test: int {
Blue,
Red,
Yellow
}

private Test m_property;

public Test MyProperty {
get {return m_property;}
set {m_property = value;}
}
}
}

Regards,


--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
 
R

Randel Bjorkquist

Hey Angel,

Yes, that is what I'm trying to do. But I can not get my code to do that.
What I mean by that is that my enum is not showing up as part of my class in
main. I type the class name, then the "." and my public enum "eDiscount" is
no where to be seen. I have looked at your code and compared it to what I
have and I'm missing something, cause it looks like my code looks like
yours. My code was attached in my first news group post.

Do you have any ideas of what I'm missing?

Randel


Angel J. Hernández M. said:
Hope this may help you...


using System;

namespace ConsoleApplication1 {
class Class1 {
[STAThread]
static void Main(string[] args) {
EnumTest o = new EnumTest();
o.MyProperty = EnumTest.Test.Blue; // Is this the way you wanna do it?
Console.WriteLine(o.MyProperty.ToString());
Console.ReadLine();
}
}

class EnumTest {
public enum Test: int {
Blue,
Red,
Yellow
}

private Test m_property;

public Test MyProperty {
get {return m_property;}
set {m_property = value;}
}
}
}

Regards,


--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda





Randel Bjorkquist said:
Hers's my question. I want the ability to set a property of an object
I've created, from outside of the class code (ie... inside of "Main" or
an application event), with an enum. Something like this:

CODE IN "Main":
TItem MyItem = new TItem();
MyItem.Discount = TItem.eDisount.DISC_0;

I believe I should be able to do this. I just can't seem to find it.
What I want to imitate is the code for a "Key" event, like "KeyDown",
"KeyPress" or "KeyUp". In the "KeyDown" event, I can compare the
"System.Windows.Forms.KeyEventArgs e" parameter to what looks like a
handful of enums for the "Forms" class. I do a comparison like this:

CODE IN "KeyDown" EVENT
private void lstbox_FileNames_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e){
if(e.KeyCode == System.Windows.Forms.Keys.Delete){
//DO SOMETHING HERE
}//END OF "if(e.KeyCode == System.Windows.Forms.Keys.Delete)"
}//END OF "lstbox_FileNames_KeyDown" METHOD

What I don't get is how to get the list of enumerators to show up outside
of my class. I have pasted a quick example of my class code below and am
looking forwards to any help I can get.

Thanks in advance,

Randel Bjorkquist
**********************************************************
**********************************************************
using System;

namespace Assignment_8
{
/// <summary>
/// Summary description for TItem.
/// </summary>
public class TItem
{

//---------------------------------------------------------------------------
public enum eDiscount {DISC_0 = 0, DISC_3, DISC_5};


//---------------------------------------------------------------------------
private eDiscount FDiscount;


//---------------------------------------------------------------------------
public TItem(){
FDiscount = eDiscount.DISC_0;
}//END OF "TItem" DEFAULT CONSTRUCTOR -----------------------


//---------------------------------------------------------------------------
public decimal Discount{
get{
decimal TmpVal = 0;

switch(FDiscount){
case eDiscount.DISC_0: TmpVal = 1M; break;
case eDiscount.DISC_3: TmpVal = 2M; break;
case eDiscount.DISC_5: TmpVal = 3M; break;
}//END OF "" SWITCH-STATEMENT

return(TmpVal);
}//END OF "get" ACCESSOR

set{FDiscount = value;}//END OF "set" ACCESSOR
}//END OF "Discount" ACCESSOR
METHOD ----------------------------------------
}//END OF "TItem" CLASS
}//END OF "Assignment_8" NAMESPACE IN FILE "TItem.cs"
 
B

Bruce Wood

Intellisense sometimes loses its mind. Don't assume that because you
can't see TItem.eDiscount that it isn't there. You should type in
TItem.eDiscount.DISC_0 and it should compile.
 
R

Randel Bjorkquist

I see what I was doing wrong.

TItem TmpItem = new TItem();
TmpItem.Discount = TmpItem.EDisount.DISC_0;

//this is wrong because I'm trying to access the public enum through the
local variable and not the class. I get it to work if I do this:

TmpItem.Discount = TItem.EDiscount.DISC_0;

Thanks for the help guys.

Randel
 
R

Randel Bjorkquist

Part II

I also had to change my accessor method. I was returning a decimal and was
forcing the "value" to convert to "eDiscount". But I think I have it now.

Again, thanks for the help.

Randel Bjorkquist
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top