How do we create an accessor method

blumonde

Well-known member
Joined
Jul 26, 2005
Messages
68
Programming Experience
Beginner
Gentlemen,

How do we create an accessor method for enumeration variables?

A quick example, (I understand there are syntax errors below but that is not important)

**********************************
public enum Showme
s1=0
s2=0
s3=0
end enum

public enum Showme
get ?
set ?
end sub
***********************************

How do I setup those variables in my "get" and "set" ?

Thanks.

blumonde
 
Last edited:
i think it would be something along the lines of:
VB.NET:
Public Enum ShowMe
S1 = 0
S2 = 1
S3 = 2
End Enum

Private intShowMe As ShowMe

Public Property ShowMe as ShowMe
  Get
    Return intShowMe
  End Get
  Set (Value As ShowMe)
    intShowMe = Value
  End Set
End Property
but i'm not 100% sure what you're really asking for, enumerations are treated like integers btw
 
Moved to more appropriate forum. This question is not realted to Visual Studio.

Enumerations themselves have no properties or methods. If you want to create a property of an enumerated type in some class or structure then you would do it exactly as you would for a property of any other type, as JB has shown.
 
jmcilhinney said:
Moved to more appropriate forum. This question is not realted to Visual Studio.

Enumerations themselves have no properties or methods. If you want to create a property of an enumerated type in some class or structure then you would do it exactly as you would for a property of any other type, as JB has shown.


Hi JM,

Ltnt. I understand you are also an expert in C#. Could you please show me how I can create an accessor method for the enum below:

private enum m_Status { S1, S2, S3 };

Regards,

blumonde
 
If what JB posted was what you want in VB.NET then its basically the same thing in C#. I don't really see what this has to do with enumerated types specifically. If you want a class to expose an object of any type then you just declare a private variable and a public property, each of that type. There are converters for C# to VB.NET and VB.NET to C# here, so you can just run JB's code through there if you want C#.
 
jmcilhinney said:
If what JB posted was what you want in VB.NET then its basically the same thing in C#. I don't really see what this has to do with enumerated types specifically. If you want a class to expose an object of any type then you just declare a private variable and a public property, each of that type. There are converters for C# to VB.NET and VB.NET to C# here, so you can just run JB's code through there if you want C#.


Thanks for the converter. I tried to convert it myself but couldn't get it right.

Thanks again JM.

blumonde
 
jmcilhinney said:
Moved to more appropriate forum. This question is not realted to Visual Studio.

Enumerations themselves have no properties or methods. If you want to create a property of an enumerated type in some class or structure then you would do it exactly as you would for a property of any other type, as JB has shown.


JM, after converting JB's code from VB to C#, I got an error when trying to build the class. The C# version is below:

public enum ShowMe
{
S1 = 0,
S2 = 1,
S3 = 2
}
private ShowMe intShowMe;

public ShowMe ShowMe {
get {
return intShowMe;
}
set {
intShowMe = Value;
}
}


Error 1 The type 'myclass1' already contains a definition for 'ShowMe'
--- at the line "public ShowMe ShowMe" <--- this 2nd variable

Please advise.

blumonde
 
You've declared the enumerated type ShowMe inside the class definition, so you can't now declare a property with the name ShowMe as well. You should really be declaring the enumeration outside the class. Like I said, an enumeration is a type in itself, so there is no need to declare it inside another type. generally you would only do so if it was to be private. Nested types do exist but I can't think of a single nested enumeration in the .NET Framework.
 
jmcilhinney said:
You've declared the enumerated type ShowMe inside the class definition, so you can't now declare a property with the name ShowMe as well. You should really be declaring the enumeration outside the class. Like I said, an enumeration is a type in itself, so there is no need to declare it inside another type. generally you would only do so if it was to be private. Nested types do exist but I can't think of a single nested enumeration in the .NET Framework.

JM, Below is what I have. Could you please show me how to exclude the name? I tried but didn't get it right.

Thanks.


namespace n1 {
public class Let1 {

public enum intShowMe
{
S1 = 0,
S2 = 1,
S3 = 2
}
private ShowMe intShowMe;

public ShowMe ShowMe {
get {
return intShowMe;
}
set {
intShowMe = Value;
}
}


}
}


blumonde
 
Again, you have defined the ShowMe enumerated type INSIDE the class definition. You need to declare it OUTSIDE, otherwise you have two different things inside the class referred to by the name "ShowMe".
 
jmcilhinney said:
Again, you have defined the ShowMe enumerated type INSIDE the class definition. You need to declare it OUTSIDE, otherwise you have two different things inside the class referred to by the name "ShowMe".

I changed it as below but still couldn't get it right. What should it be like?

blumonde

namespace n1 {

public enum intShowMe
{
S1 = 0,
S2 = 1,
S3 = 2
}


public class Let1 {

private ShowMe intShowMe;

public ShowMe ShowMe {
get {
return intShowMe;
}
set {
intShowMe = Value;
}
}


}
}
 
What does the error message say now? Something like "Type 'ShowMe' not defined."? Where have you defined a type named ShowMe? You haven't because you went and changed its name. If you read my previous posts I kept saying move it outside. Nothing about changing the name.
 
jmcilhinney said:
What does the error message say now? Something like "Type 'ShowMe' not defined."? Where have you defined a type named ShowMe? You haven't because you went and changed its name. If you read my previous posts I kept saying move it outside. Nothing about changing the name.

JM, below is JB's original VB code. After I converted it to C#. I didn't change any name. I only moved the enum to outside of the class just as I posted before this one. Could you please show me a quick small sample code of how you would do it( with classes and stuff)? It is more clear to me that way. I am slow. Sorry.

Public Enum ShowMe
S1 = 0
S2 = 1
S3 = 2
End Enum

Private intShowMe As ShowMe

Public Property ShowMe as ShowMe
Get
Return intShowMe
End Get
Set (Value As ShowMe)
intShowMe = Value
End Set
End Property

***************

The error is "The class 'LProcess' already contains a definition for 'Showme'"



 
In post #8 you have defined "public enum ShowMe". In posts #10 and #12 you have defined "public enum intShowMe". Note the change of name. In your subsequent code you are declaring variables of type "ShowMe" but there is no type called "ShowMe" because YOU renamed it to "intShowMe". CHANGE THE NAME BACK TO "ShowMe".
 
Back
Top