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.Succ ess ...etc. Is there
anyway I can avoid typing the name of the enumeration?
Any feedback is appreciated. 4 2564
C# beginner <an*******@disc ussions.microso ft.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.Succ ess ...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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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*******@disc ussions.microso ft.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.Succ ess ...etc. Is there anyway I can avoid typing the name of the enumeration? Any feedback is appreciated.
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*******@disc ussions.microso ft.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.Succ ess ...etc. Is there anyway I can avoid typing the name of the enumeration? Any feedback is appreciated.
You have to write ReturnCode.Succ ess/Fail, no way around it.
Rather than define a ReturnCode enum and pollute all your code with
ReturnCode.Succ ess/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*******@disc ussions.microso ft.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.Succ ess ...etc. Is there anyway I can avoid typing the name of the enumeration? Any feedback is appreciated.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 I've seen I can define both keys for description and code, but
my XMLs validate even if I choose a wrong pair:
Here are my both enumerations (abbreviated)
<xs:simpleType name="COUNTRYCODE">
<xs:restriction base="xs:int">
<xs:enumeration...
|
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 parameters, as well as
output Events.
Public Enum enmListOfValues
Value1
Value2
Value3
Value4
|
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 defined in foo.cpp needs to return typeNone. Is using
static_cast< bar >( 0 )
|
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 DLL I'd like to
access -- is this possible?
Thanks,
Joe
|
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 AllItems as Integer
'First Item' = 1;
'Second Item' = 2;
:
End Enum
| |
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 PropertyGrids displaying these
enumerations (one of which has a few hundred items in). I believe this is
because the dynamically generated enumerations are not being destroyed.
I have not been able to find a mechanism for deleting/destroying dynamically...
|
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 base="xs:string">
<xs:enumeration value="Apple" />
<xs:enumeration value="Orange" />
</xs:restriction>
|
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.
Should a single class be created that contains all of a solution's
enumerations? Or should enumerations be defined directly in the files that
contain the related classes? Are there any other design patterns I am
overlooking?
|
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 provide a "one obvious way" to
do enumerations in Python.
> >>> from enum import Enum
> >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
|
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 reStructuredText source as it is today. Please discuss it
here so I can see what issues people may have.
|
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,...
| |
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |