473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should I return an int or an enum?

Hi all,

I have a property called GeneralStatus that can return an int between -1 and
3.

These values are represented by an enumeration called StatusEnum.

My question is, should my property definition return a int (the underlying
type of the enum) or should I return the enum itself.

What would returning the enum achieve?

Many thanks

tce
Jul 21 '05 #1
4 1296
The only advantage is, you can easy readability of an enum. So, you can easy
check it for certain returns without knowing the int values.

myReturn == myEnum.Failed

However, using a enum won't guarantee you returning values outside of enum.
(for some reason c# does not provide this check)

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"thechaosengine " <sh856531@micro softs_free_emai l_service.com> wrote in
message news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi all,

I have a property called GeneralStatus that can return an int between -1 and 3.

These values are represented by an enumeration called StatusEnum.

My question is, should my property definition return a int (the underlying
type of the enum) or should I return the enum itself.

What would returning the enum achieve?

Many thanks

tce

Jul 21 '05 #2

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> a écrit dans
le message de news: %2************* ***@TK2MSFTNGP1 1.phx.gbl...
Hi all,

I have a property called GeneralStatus that can return an int between -1
and 3.

These values are represented by an enumeration called StatusEnum.

My question is, should my property definition return a int (the underlying
type of the enum) or should I return the enum itself.

What would returning the enum achieve?
Better type checking. The compiler will give you errors if you mess up and
assign or compare with a value that does not belong to the enum (a member of
another enum for example).

Many thanks

tce

Jul 21 '05 #3
Bruno,

Actually C# does not do "bounds" checking on enums. Try this:

public enum MyEnum
{
first = 1,
second = 2
}

MyEnum x = (MyEnum) 100;

and the code works! If anything, the enums are best when you want to checks
against a known set of numeric values. Also, better readability.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Bruno Jouhier [MVP]" <bj******@clu b-internet.fr> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> a écrit dans
le message de news: %2************* ***@TK2MSFTNGP1 1.phx.gbl...
Hi all,

I have a property called GeneralStatus that can return an int between -1
and 3.

These values are represented by an enumeration called StatusEnum.

My question is, should my property definition return a int (the underlying type of the enum) or should I return the enum itself.

What would returning the enum achieve?
Better type checking. The compiler will give you errors if you mess up and
assign or compare with a value that does not belong to the enum (a member

of another enum for example).

Many thanks

tce


Jul 21 '05 #4

"Manohar Kamath" <mk*****@TAKETH ISOUTkamath.com > a écrit dans le message de
news: O7************* ***@TK2MSFTNGP0 9.phx.gbl...
Bruno,

Actually C# does not do "bounds" checking on enums. Try this:

public enum MyEnum
{
first = 1,
second = 2
}

MyEnum x = (MyEnum) 100;

and the code works! If anything, the enums are best when you want to
checks
against a known set of numeric values. Also, better readability.
I know, but typing is not (at least in C#) about bounds checking, it is
about distinguishing what needs to be distinguished, and about increasing
the amount of verifications performed by the compiler. For example, an API
like:
MyEnum MyFunc()
is different from an API like:
int MyFunc()
because it will get a compile time error if you write something like:
MyOtherEnum val = MyFunc();

Of course, if you start casting, you are on your own!

Bruno.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Bruno Jouhier [MVP]" <bj******@clu b-internet.fr> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> a écrit
dans
le message de news: %2************* ***@TK2MSFTNGP1 1.phx.gbl...
> Hi all,
>
> I have a property called GeneralStatus that can return an int
> between -1
> and 3.
>
> These values are represented by an enumeration called StatusEnum.
>
> My question is, should my property definition return a int (the underlying > type of the enum) or should I return the enum itself.
>
> What would returning the enum achieve?


Better type checking. The compiler will give you errors if you mess up
and
assign or compare with a value that does not belong to the enum (a member

of
another enum for example).
>
> Many thanks
>
> tce
>



Jul 21 '05 #5

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

Similar topics

7
1925
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can use them obviously by placing their names in place of values, henc the ideal behind using Const. My question is... is there a way to get the actual Variable name for specific value that is returned to me?
2
1504
by: Lasse Edsvik | last post by:
Hello I was wondering if it's possible to return both a string and an enum value? both "I have a caddilac" and CADDILAC? as an example.. if so, could you guys show me? TIA /Lasse
4
1270
by: thechaosengine | last post by:
Hi all, I have a property called GeneralStatus that can return an int between -1 and 3. These values are represented by an enumeration called StatusEnum. My question is, should my property definition return a int (the underlying type of the enum) or should I return the enum itself.
3
13895
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
2
1813
by: tzvika.visman | last post by:
I write a MC++ wrapper for our company internal SDK that wrote in C++ native code for writing application with this SDK over C# and other .NET languages and most of my SDK API function return a status code that define as a enum type. Do I have other option then duplicate the enum from the c++ to the C# code and write a MC++ class that look something like this: public __gc class ManagedDokStatus {
6
4815
by: Bob | last post by:
It should be simple, but I'm stuck on this one... how do you return a value for an enum property type? TIA, Bob
5
1343
by: Tim Gallivan | last post by:
I have 2 functions, CheckThis and CheckThat. Each function can return some of the error codes in the Enum below (CheckThis can return 0, 1, 2 or 3 and CheckThat can return 0, 4, 5 or 6). I'm trying to evaluate the following: if CheckThis OR CheckThat then Select Case ErrCode ... obviously this won't work in the case of CheckThis returning a 2 and CheckThat returning a 6, I'll get an 8 ...
6
3673
by: Valmont | last post by:
Complete newbie at C#. Class below stripped for brevity. How do I return a private "Status" from a public method? I may need a set/get but I don't know how to do that on an enum. Thank you. public class SomeClass
5
11232
by: Felix Kater | last post by:
Hi, is it valid to declare the variable 'e' of type 'my_enum' to later hold the return value of function f2 -- or must e by of type int? Felix typedef enum{ a_my_enum, b_my_enum,
0
8036
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
7978
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
8461
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
8448
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...
1
8126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5987
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
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2454
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.