473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

enumerations

namespace mynamespace
{
internal enum ReturnCode
{
Fail = -1,
Success = 0,
...
}
}
If I have to use the enumeration from a class that is in
the same namespace, do I still have to type,
ReturnCode.Fail, ReturnCode.Success ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated.
Nov 15 '05 #1
4 2535
C# beginner <an*******@discussions.microsoft.com> wrote:
namespace mynamespace
{
internal enum ReturnCode
{
Fail = -1,
Success = 0,
...
}
}
If I have to use the enumeration from a class that is in
the same namespace, do I still have to type,
ReturnCode.Fail, ReturnCode.Success ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated.


No - and that's a *good* thing, IMO, because it makes it explicit what
kind of enumeration you're talking about.

(Side note: rather than using return codes, most of the time you're
better off using exceptions to signal that a method has failed.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
No.

Enumerations are nothing more than classes (well structures) with constant
fields defined (that is basically how the CLR sees them). Consider the
following.

class OuterClass{
class InnerClass{
public void MyMethod();
}
}

If you wanted OuterClass to access MyMethod, you would have to explicitly
reference InnerClass.

The value of an enumeration does not belong to the class but rather to the
enumeration, which is its own entity.

"C# beginner" <an*******@discussions.microsoft.com> wrote in message
news:2e****************************@phx.gbl...
namespace mynamespace
{
internal enum ReturnCode
{
Fail = -1,
Success = 0,
...
}
}
If I have to use the enumeration from a class that is in
the same namespace, do I still have to type,
ReturnCode.Fail, ReturnCode.Success ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated.

Nov 15 '05 #3
The problem you have, is when two declarations of an enumeration have an
intersection in the set of named constants. As far as I am aware, you
cannot scope to the current enumeration. You cannot say, within the scope
of enumeration Day, do the following other statements, which could include
references to Mon, Tues, Wes, without the prefixed typename Day.

"C# beginner" <an*******@discussions.microsoft.com> wrote in message
news:2e****************************@phx.gbl...
namespace mynamespace
{
internal enum ReturnCode
{
Fail = -1,
Success = 0,
...
}
}
If I have to use the enumeration from a class that is in
the same namespace, do I still have to type,
ReturnCode.Fail, ReturnCode.Success ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated.

Nov 15 '05 #4
You have to write ReturnCode.Success/Fail, no way around it.

Rather than define a ReturnCode enum and pollute all your code with
ReturnCode.Success/Fail, you should consider two options:

* Returning a bool value (true/false)
* Not returning anything (void) and throwing exceptions when something goes
wrong.

Usually, the best approach is a mixture of the two above: you return a bool
value when the caller can do something about the failure case, and you throw
an exception when the event is really abnormal and the caller won't be able
to do anything about it (but somewhere higher in the call chain -- usually
an event loop or event handler --, there is a method that will catch the
exception, report it to the user and let the application go on).

Bruno.

"C# beginner" <an*******@discussions.microsoft.com> a écrit dans le message
de news:2e****************************@phx.gbl...
namespace mynamespace
{
internal enum ReturnCode
{
Fail = -1,
Success = 0,
...
}
}
If I have to use the enumeration from a class that is in
the same namespace, do I still have to type,
ReturnCode.Fail, ReturnCode.Success ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated.

Nov 15 '05 #5

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

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.