473,973 Members | 57,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing an Enumeration Variable by Ref

I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no way
to pass an Enum by reference.
Am I missing something painstakingly obvious or can Enum's not be passed by
reference?
Nov 16 '05 #1
6 16161
enum MyEnumeratedSet { Value1, Value2 }

...Main (..)
{
MyEnumeratedSet value = MyEnumeratedSet .Value1;
MyFunction(ref value);
}

... MyFunction(ref MyEnumeratedSet value)
{
value = MyEnumeratedSet .Value2;
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
<jo*****@driver .net> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no way to pass an Enum by reference.
Am I missing something painstakingly obvious or can Enum's not be passed by reference?

Nov 16 '05 #2
Obviously this was WAY Too simple ;) - so one should have moved onto the
obvious assumption.
That i NEEDED System.Enum in the parameters because i am going to have
multiple Enum'

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:IW******** **********@news 2.e.nsc.no...
enum MyEnumeratedSet { Value1, Value2 }

..Main (..)
{
MyEnumeratedSet value = MyEnumeratedSet .Value1;
MyFunction(ref value);
}

.. MyFunction(ref MyEnumeratedSet value)
{
value = MyEnumeratedSet .Value2;
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
<jo*****@driver .net> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no

way
to pass an Enum by reference.
Am I missing something painstakingly obvious or can Enum's not be passed

by
reference?


Nov 16 '05 #3
<jo*****@driver .net> wrote:
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no way
to pass an Enum by reference.
Yes there is. For example:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Change (ref z);
Console.WriteLi ne (z);
}

static void Change(ref Foo x)
{
x = Foo.Baz;
}
}
Am I missing something painstakingly obvious or can Enum's not be passed by
reference?


Parameters passed by reference have to match in type *exactly* - you
can't pass subtypes and just cast them, as that would break type safety
(consider passing a string variable by reference to something accepting
ref object - the method could assign it a value which wasn't a
reference to a string).

If you need it to work with *any* enum, it'll involve boxing and then
unboxing afterwards:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Enum tmp = z;
Change (ref tmp);
z = (Foo) tmp;
Console.WriteLi ne (z);
}

static void Change(ref Enum x)
{
x = Foo.Baz;
}
}

If you know the type in advance, however, it would be much better to
stick to code like the first example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Okay. Thanks. I was hoping there was a prettier way to do it.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<jo*****@driver .net> wrote:
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no
way
to pass an Enum by reference.


Yes there is. For example:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Change (ref z);
Console.WriteLi ne (z);
}

static void Change(ref Foo x)
{
x = Foo.Baz;
}
}
Am I missing something painstakingly obvious or can Enum's not be passed
by
reference?


Parameters passed by reference have to match in type *exactly* - you
can't pass subtypes and just cast them, as that would break type safety
(consider passing a string variable by reference to something accepting
ref object - the method could assign it a value which wasn't a
reference to a string).

If you need it to work with *any* enum, it'll involve boxing and then
unboxing afterwards:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Enum tmp = z;
Change (ref tmp);
z = (Foo) tmp;
Console.WriteLi ne (z);
}

static void Change(ref Enum x)
{
x = Foo.Baz;
}
}

If you know the type in advance, however, it would be much better to
stick to code like the first example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Well, then you have to declare it as a System.Enum, because implicit type
casts
cannot be performed when passing an instance by ref.

...Main (..)
{
Enum value = MyEnumeratedSet .Value1;
MyFunction(ref value);
}

... MyFunction(ref MyEnumeratedSet value)
{
if (value is MyEnumeratedSet )
value = MyEnumeratedSet .Value2;
}

Although i would recommed using overloaded methods instead,
one for each Enum type.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
<jo*****@driver .net> wrote in message
news:e8******** *****@tk2msftng p13.phx.gbl...
Obviously this was WAY Too simple ;) - so one should have moved onto the obvious assumption.
That i NEEDED System.Enum in the parameters because i am going to have
multiple Enum'

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:IW******** **********@news 2.e.nsc.no...
enum MyEnumeratedSet { Value1, Value2 }

..Main (..)
{
MyEnumeratedSet value = MyEnumeratedSet .Value1;
MyFunction(ref value);
}

.. MyFunction(ref MyEnumeratedSet value)
{
value = MyEnumeratedSet .Value2;
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
<jo*****@driver .net> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no

way
to pass an Enum by reference.
Am I missing something painstakingly obvious or can Enum's not be
passed by
reference?



Nov 16 '05 #6
If you ask me, it is by far the most prettiest way i can think of to do it.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
<jo*****@driver .net> wrote in message
news:ey******** ******@tk2msftn gp13.phx.gbl...
Okay. Thanks. I was hoping there was a prettier way to do it.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<jo*****@driver .net> wrote:
I have MyEnumeratedSet { ... some values go here ... }
I have a variable that is defined as
MyEnumeratedSet MyVariable;
I have a function that needs to be able to set a variable by Ref
MyFunction(ref System.Enum Parameter)
{ ..... Parameter has to be set here }

so I have tried calling it by
MyFunction(ref (Enum) MyVariable);
unfortunately this yields compiler errors and it seems like there is no
way
to pass an Enum by reference.


Yes there is. For example:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Change (ref z);
Console.WriteLi ne (z);
}

static void Change(ref Foo x)
{
x = Foo.Baz;
}
}
Am I missing something painstakingly obvious or can Enum's not be passed by
reference?


Parameters passed by reference have to match in type *exactly* - you
can't pass subtypes and just cast them, as that would break type safety
(consider passing a string variable by reference to something accepting
ref object - the method could assign it a value which wasn't a
reference to a string).

If you need it to work with *any* enum, it'll involve boxing and then
unboxing afterwards:

using System;

enum Foo
{
Bar=1,
Baz=2
}

class Test
{
static void Main()
{
Foo z = Foo.Bar;
Enum tmp = z;
Change (ref tmp);
z = (Foo) tmp;
Console.WriteLi ne (z);
}

static void Change(ref Enum x)
{
x = Foo.Baz;
}
}

If you know the type in advance, however, it would be much better to
stick to code like the first example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #7

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

Similar topics

6
2067
by: mheydman | last post by:
I posted this to the notes page of the COM PHP documentation... I will post an update there if I find a solution here. I have need for a commercial COM/DOTNET class library that requires a paramater to be passed when the object is created. It does not appear as though PHP allows for the instantiation of COM/DOTNET objects that require a parameter to be passed to their constructor... I am not sure if I am being clear, but suppose to...
40
3432
by: Sonia | last post by:
Hi, Please look at the following code. This is a simple program for a game of craps - very basic. I have declared the gameStatus as global variable. I was wondering if there is a better way of handling this (possibly via local variables) I am using that variable to keep track of the status of the game and pass it back to main() from craps() where I either increase or decrease a bank balance. Should this be handled with local parameters...
20
4883
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
4
1266
by: Caroline Middlebrook | last post by:
Hi, I have been reading through the Stroustrup book 3e (section 4.8) about enumerations and there is something I just don't understand... I thought that the idea of an enum was that you could define some type that had a set number of values defined by you eg enum days_of_week {mon, tue, wed, thu, fri, sat, sun};
4
5673
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly convenient, is also one of the most gross warping of language concepts to date... public IEnumerator...
7
9034
by: Don | last post by:
I need to get the type of an enumeration from an instance of a class. e.g. Public Class MyClass Public Enum MyEnum value1 = 1 End Enum End Class
1
1799
by: martin | last post by:
This code compiles (this is how it should work): Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); This code does not compile (passing an int instead of an AddressFamily is bad): Socket sock = new Socket(1, SocketType.Stream, ProtocolType.Tcp); But why on earth does not compile: Socket sock = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
10
2098
by: braratine | last post by:
Hello, I have to following issue. A type is declared based on an enum: " enum TpAddressPlan { P_ADDRESS_PLAN_NOT_PRESENT = 0, P_ADDRESS_PLAN_UNDEFINED = 1, P_ADDRESS_PLAN_IP = 2, P_ADDRESS_PLAN_MULTICAST = 3, P_ADDRESS_PLAN_UNICAST = 4,
4
1576
by: nsphelt | last post by:
Hi. I’ve noticed that in when setting a variable of type Color to something inVB.NET, that IntelliSense gives you a list of Color structures when you hit equals as though the structure were an enumeration. I am wondering if it is possible to duplicate this behaviour. I have created a structure that has shared properties that return instances of that structure to act as though it were an enumeration, but I would like to get IntelliSense to...
0
10347
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
11399
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
11558
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,...
0
10070
agi2029
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...
1
8453
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
7600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5146
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
2
4726
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.