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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining and using enums

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
0
0
US
According to Microsoft:

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.


However, if I try placing several enums inside a namespace but before any classes are defined, I can't use the enum.

Here's an example of what doesn't work:
namespace CommonFunctions
{
public enum fldsJobSpecs
{
fldJOBSPECS_JobNumber,
fldJOBSPECS_Nuclear,
fldJOBSPECS_PlusMinus
};

public class CommFuncs
{
.
.
.


This seems to be the only thing that works:
namespace CommonFunctions
{
public class CommFuncs
{
public enum fldsJobSpecs
{
fldJOBSPECS_JobNumber,
fldJOBSPECS_Nuclear,
fldJOBSPECS_PlusMinus
};
.
.
.

Why?

Also, when I reference the enum, I have to fully qualify the value like:

dr[(int)CommFuncs.fldsJobSpecs.fldJOBSPECS_LastUpdated].ToString()

even though I have this declarative:

using CommonFunctions;


and this code:
public CommFuncs cf = new CommFuncs();

I can't reference the enum utilizing the 'cf'

Thanks in advance,



Jerry Scannell
 
You have to have the class to contain the enum.
if you make the class STATIC, you can reference it without a new.
You will need the class name regardless, so that there's no confusion which enum you wanted.

HTH,
Lodlaiden

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Thanks. Can you give me an example of what a static class would look like as it pertains to having enums in it?


Jerry Scannell
 
he's talking about the static keyword.
Code:
class Foo
{
   public enum Fu {a,b,c};
}

static class Bar
{
   public enum Bu {e,f,g};
}
Code:
Fu fus = new Foo().Fu;
Bu bus = Bar.Bu;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I did what you suggested. It works, however, there is compiler warning error that reads:
Warning 3 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. C:\Documents and Settings\cransca\My Documents\Visual Studio 2005\Projects\PSDS\PSDS\frmJobCopy.vb 38 31 PSDS

Here's what I did:
1. I created a separate .cs file that has
using System;
using System.Collections.Generic;
using System.Text;

public static class dbColumnEnums
{
public enum fldsJobSpecs : int
{
fldJOBSPECS_JobNumber,
fldJOBSPECS_Nuclear,
fldJOBSPECS_PlusMinus,
.
.
.
.

Referencing this class (which is part of a .dll) is like this:
Public dbJS As New dbColumnEnums.fldsJobSpecs()

Then using them is like this:
dim s as string = dr(dbJS.fldJOBSPECS_JobNumber).ToString()

That's where I get the warning error. Why?

Thanks in advance,



Jerry Scannell
 
I would avoid static references as much as possible. they make testing and debugging more difficult. there are few times you would need to nest an enum/class/struct within another class.

I would keep the classes as instance types and move the enum outside the scope of the class.
Code:
namespace ...
{
   public enum Fu
   {
       Bar,
       Baz
   }

   public class Foo
   {
      public void DosomethingWith(Fu f)
      {
          ....
      }
   }
}
Code:
var foo = new Foo();
foo.DosomethingWith(Fu.Bar);

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top