473,806 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using toString with Enumeration

Hey, this is a very basic question for all you csharpers...

I have created a class:

public class Answer
{
public enum theAnswer { A, B, C }
}

I want to be able to do a toString as below on my enumeration... but
cant figure out the syntax to put in my class!!!

theAnswer myAnswer;
string myString;

ans = myAnswer.A;
mystring = ans.toString

Sep 26 '06 #1
2 1257
Hi,

FYI it's much better practice to declare public enums in a namesapce, not a class declaration, and to use Pascal casing as you would
a class or struct declaration:

namespace TopNamespace
{
public enum TheAnswer
{
A, B, C
}

public class Answer
{
public Answer() // instance constructor
{
TheAnswer myAnswer = TheAnswer.A;
string myString = myAnswer.ToStri ng();

// myString now contains "A"
}
}
}
--
Dave Sexton

"csharpers" <la*****@gmail. comwrote in message news:11******** **************@ e3g2000cwe.goog legroups.com...
Hey, this is a very basic question for all you csharpers...

I have created a class:

public class Answer
{
public enum theAnswer { A, B, C }
}

I want to be able to do a toString as below on my enumeration... but
cant figure out the syntax to put in my class!!!

theAnswer myAnswer;
string myString;

ans = myAnswer.A;
mystring = ans.toString

Sep 26 '06 #2
Thanks

Sep 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2936
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are defined in a different namespace from the element.
2
1915
by: Neil | last post by:
Is it possible to override the ToString() method when creating a simple enumeration? public enum myEnum { Undefined = 0, Unassigned = 1, Assigned = 2, Reallocated = 3 }
3
1313
by: raffelm | last post by:
Say I have an enum private enum ShippingMethods { smSnailMail, smEmail } If create var of type ShippingMethods when I call ToString() on it, I get the actual enumeration. I would like to make ToString() return a
7
6378
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos ) { Console.WriteLine("Name: {0}" fi.Name); Console.WriteLine("Value: {0} " fi.GetValue(null).ToString());
10
7141
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but how does one override the function to have some values return a different string? For example, suppose I have an enum with value for the types of DVD media like public enum DVD_Media {
1
2962
by: Wm. Scott Miller | last post by:
I have a Custom DTS Task for SQL Server and I've got it working fine, except for when I try to access its properties from an ActiveX script. I have added a new property called Length and a method called Generate that returns a string. Whenever I get the object in the ActiveX script, the only properties and methods I can use are for the CustomTask interface, not the extended properties I've added. Does anyone know why these properties...
8
1733
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small Medium Large End Enum
7
9840
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
1
1945
by: brucepickford001 | last post by:
Hi, I have a not simple problem. I want the following XML to be schema valid <PopulationDemographic> <PersonName>Joe Blogs</PersonName> <Age>2</Age> <Category>baby</Category> </PopulationDemographic>
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.