Search:
Namespaces
Discussions
.NET v1.1
Feedback
Passing an Enumeration Variable by Ref
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
johndoe@driver.net
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?
Reply to this message...
Dennis Myrén
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
<
Click here to reveal e-mail address
> wrote in message
news:%
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
johndoe@driver.net
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" <
Click here to reveal e-mail address
> wrote in message
news:IWU%c.8582$g%
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Dennis Myrén
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
<
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
> Obviously this was WAY Too simple ;) - so one should have moved onto
the
[Original message clipped]
Reply to this message...
Jon Skeet [C# MVP] (VIP)
<
Click here to reveal e-mail address
> wrote:
[Original message clipped]
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;
}
}
[Original message clipped]
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 - <
Click here to reveal e-mail address
>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
johndoe@driver.net
Okay. Thanks. I was hoping there was a prettier way to do it.
"Jon Skeet [C# MVP]" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Dennis Myrén
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
<
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
System.Console
System.Enum
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com