473,383 Members | 1,795 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,383 software developers and data experts.

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 1283
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@microsofts_free_email_service.com> wrote in
message news:%2****************@TK2MSFTNGP11.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@microsofts_free_email_service.com> a écrit dans
le message de news: %2****************@TK2MSFTNGP11.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******@club-internet.fr> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"thechaosengine" <sh856531@microsofts_free_email_service.com> a écrit dans
le message de news: %2****************@TK2MSFTNGP11.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*****@TAKETHISOUTkamath.com> a écrit dans le message de
news: O7****************@TK2MSFTNGP09.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******@club-internet.fr> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"thechaosengine" <sh856531@microsofts_free_email_service.com> a écrit
dans
le message de news: %2****************@TK2MSFTNGP11.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
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...
2
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
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...
3
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
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...
6
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
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...
6
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....
5
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,
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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.