Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a class with an enum as a property?

Status
Not open for further replies.

jwarmuth

IS-IT--Management
Sep 22, 2001
151
CA
I'm trying to create a new base class with an enumerator as a property; that is to say, I have a property I'd like to limit to only a select list of options.

The only specific example I can relate to is the manner in which an SQLcommand specifies it's CommandType.

Code:
scmdMyCommand.CommandType = CommandType.[one of three options displayed here]

How can I replicate this method of creating a class property?

Jeff W.
MCSE, CNE
 
You mean, other than creating an enum type, and declaring the property to have that name and be of that type?
Code:
public enum BalloonColor
{
  Red = 0,
  Blue,
  Green
}

public class clown
{
  private BalloonColor _color;

  public BalloonColor BalloonColor
  {
     get { return _color; }
     set { _color = value; }
  }
}
Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Awesome, that worked perfectly. Thanks!!!

Jeff W.
MCSE, CNE
 
Just another thing to consider,

I am purely cautionary here but are you intending to then have a Switch statement to determine what to do with each enum? If so, an enum is not really what you want.
if you are using it like the example above with Color, all is cool.

if you are using the enum for something like type of data, and then have a plan for

psuedocode:

switch (PetType)

Case (dog)
Save dog code

Case (cat)
Save cat code

Case (rat)
Save rat code


then you really need to make a pet object interface with a Save Method.

just trying to save you some headaches later when you end up in switch hell

HTH

Bassguy
 
The purpose of this 'application' is to simply provide a simplified method for handling and reporting Exceptions during the writing, debugging, and testing processess. I quoted application because it's not a full application but a simple code source file I'm writing for my own utility purposes that I will import into future projects.

I've found over time (not a lot, I'm an inexperienced programmer) that in all applications I code, I tend to handle Exceptions the same way depending on the phase of development.

What I've decided to do is provide myself with a new way to get the same function with fewer lines of code and fewer changes during phase transitions.

The new class works something like this (quick demo);

Code:
//At the beginning of application, create ExceptionManager object

ExceptionManagement.ExceptionManager emDumpToEmail = new ExceptionManagement.ExceptionManager;
emDumpToEmail.Destination = Destination.Email;
//specify any unique email attributes
emDumpToEmail.EmailTo = "someguy@microsuft.com";

try
{
     //bad stuff goes down here
}
catch (Exception Ex)
{
     emDumpToEmail(Ex);
}

In that example, it would be for a beta testing program aiding me in gathering debug information.

I was going to create likewise functions for writing to the console, a web service, a local text file, or into a database.

The idea is to only have to change one block of code for each phase of the development (the initial configuration of the object upon creation) and to be able to create multiple Exception handling objects configured for different uses.

Jeff W.
MCSE, CNE
 
Someone wrote a while ago about capturing a screenshot for debugging purposes. If you use this exception handler on a GUI project you may want to consider making the screenshot attach to the email.

Just a though.
 
You could always use multiple catch blocks, so that you may see which problems you are encountering, then you could specialize your code. (I think that it is best practices to do so anyway...)

But set up your try{} catches{} like:

Code:
try{ //Do something
}
catch (OracleException oex){
}
catch (IOException ioex){
}
catch ( .... ..ex){
}
catch (Exception ex){
}

If nothing else, this will help you specialize the way you handle it, depending on the error type. This will be especially helpful in production environment.

I searched for the thread JurkMonkey referred to, here is the thread ID: thread732-1045366 I don't know if this will be exactly right for your purposes, but I use it in production, and it works for me.

Good Luck!

-Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top