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:
jmcilhinney said:
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".

Sorry, my mistake. I changed it back to "Showme" and I got an error below:

Class1.cs(46): The type or namespace name 'intShowme' could not be found (are you missing a using directive or an assembly reference?)
 
JM, would you please tell me what is wrong with my code below and how to fix the error "The left-hand side of an assignment must be a variable, property or indexer" when I assign a value to the line below. :confused:

Many thanks.

blumonde

*************************************************************
namespace NS1 {

public class C1 {
public enum Start: int { S1=0, S2=1, S3=2 };
private Start m_Start;

public Start theStart
{
get { return m_Start; }
set { m_Start = value; }
}
}

public class C2 {

NS1.C1.Start.S1 = "Signin"; // <--- error occurs here. Logically it should be okay.
}

}

***********************************************************************
 
I'm afraid not. That makes no logical sens at all. You're trying to assign a string value to an constant member of an enumeration. NS1.C1.Start is a an enumerated type with constant fields S1, S2 and S3. You can assign those values to variables of type Start but you can't assign anything to them. They are constants, and what's more, even if they weren't you couldn't assign a string to one of them because you've specifically said that they are integers. I'm not sure you fully understand what an enumeration is for based on that code.
 
jmcilhinney said:
I'm afraid not. That makes no logical sens at all. You're trying to assign a string value to an constant member of an enumeration. NS1.C1.Start is a an enumerated type with constant fields S1, S2 and S3. You can assign those values to variables of type Start but you can't assign anything to them. They are constants, and what's more, even if they weren't you couldn't assign a string to one of them because you've specifically said that they are integers. I'm not sure you fully understand what an enumeration is for based on that code.


JM, is there a way to modify my code, change the enum variables to not constant so that they will accept string values? What should it be like in order for it to become flexible and let me assign string values to it?

Please show me a way. Thanks.

blumonde
 
There is no way. Your question confirms that you don't know how to use enumerations properly. That's no crime, but it means that you probably need to do a bit of reading on the subject. The whole idea of an enumeration is that each member IS a constant. They are used as identifiers so that you can use meaningful names and enforce strong-typing rather than just using integers or strings to identify things. The advantage is that you can define exactly what values are permissable for that type. If you use integers as identifiers then you canot limit the values and the values aren't really too meaningful. If you use strings then the values become a little more meaningful as the string itself tells you what means, but you still cannot limit the acceptable values. By using an enumerated type you can clearly identify what the values mean by their name and you also explicitly limit the values that a variable of that type can contain. Take a look at some of the enumerated types that already exist in the .NET Framework. One of the most frequently used would be DialogResult, which has values OK, Cancel, Yes, No, etc. Would you try to assign a string to one of those?
 
jmcilhinney said:
There is no way. Your question confirms that you don't know how to use enumerations properly. That's no crime, but it means that you probably need to do a bit of reading on the subject. The whole idea of an enumeration is that each member IS a constant. They are used as identifiers so that you can use meaningful names and enforce strong-typing rather than just using integers or strings to identify things. The advantage is that you can define exactly what values are permissable for that type. If you use integers as identifiers then you canot limit the values and the values aren't really too meaningful. If you use strings then the values become a little more meaningful as the string itself tells you what means, but you still cannot limit the acceptable values. By using an enumerated type you can clearly identify what the values mean by their name and you also explicitly limit the values that a variable of that type can contain. Take a look at some of the enumerated types that already exist in the .NET Framework. One of the most frequently used would be DialogResult, which has values OK, Cancel, Yes, No, etc. Would you try to assign a string to one of those?

Thanks for the info, JM.

blumonde
 
No worries. I hope I wasn't too harsh previously. Current stresses are making my irritation at small things bubble over at the moment. My apologies if I was.

One more thing. While I certainly wouldn't want to discourage you from posting you VB.NET questions here, Neal himself has mentioned on a couple of occasions that this forum is VB.NET-specific. There is obviously a fair bit of overlap with C# because of the Framework and such, but you probably should find a C# forum for your C# questions. I'd suggest CodeGuru as the best place to start.
 
jmcilhinney said:
No worries. I hope I wasn't too harsh previously. Current stresses are making my irritation at small things bubble over at the moment. My apologies if I was.

One more thing. While I certainly wouldn't want to discourage you from posting you VB.NET questions here, Neal himself has mentioned on a couple of occasions that this forum is VB.NET-specific. There is obviously a fair bit of overlap with C# because of the Framework and such, but you probably should find a C# forum for your C# questions. I'd suggest CodeGuru as the best place to start.

You were doing great. don't worry about it. Thanks for the info again. I hope you are working for CodeGuru too.
 
Back
Top