By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

Passing an Enumeration Variable by Ref

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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****************@TK2MSFTNGP15.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

P: n/a
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******************@news2.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****************@TK2MSFTNGP15.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

P: n/a
<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.WriteLine (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.WriteLine (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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Okay. Thanks. I was hoping there was a prettier way to do it.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<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.WriteLine (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.WriteLine (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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5

P: n/a
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*************@tk2msftngp13.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******************@news2.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****************@TK2MSFTNGP15.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

P: n/a
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**************@tk2msftngp13.phx.gbl...
Okay. Thanks. I was hoping there was a prettier way to do it.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<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.WriteLine (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.WriteLine (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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.