Passing an Enumeration Variable by Ref

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
enum MyEnumeratedSet { Value1, Value2 }

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

... MyFunction(ref MyEnumeratedSet value)
{
value = MyEnumeratedSet.Value2;
}
 
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'
 
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.
 
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.
 
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
Okay. Thanks. I was hoping there was a prettier way to do it.
 
Back
Top