Casting in a generic function

V

Vincent Finn

Hi,

I am trying to write a generic function and it isn't behaving as expected.

I want to avoid having to write custom convert functions for my enums so
I want to convert to an int and cast to the enum, this wouldn't compile so
I tried a few variations.
The cast fails inside the generic function even though its valid.
The code below is the simplest example of the problem I could think of, even
casting int to int fails.

Is this a limitation or am I doing something wrong?

Thanks, Vin

// code
private void TestConvert()
{
string source = "1";
int destination = 0;
Parse(source, destination, new Converter<string, int>(Convert.ToInt32));
}

static protected void Parse<Destination_type, Intermediate_type>(string source,
Destination_type destination, Converter<string, Intermediate_type> converter)
{
Intermediate_type test = converter.Invoke(source);
// Error on next line: Cannot convert type 'Intermediate_type' to 'Destination_type'
destination = (Destination_type)test;
}
 
B

Barry Kelly

Vincent said:
I am trying to write a generic function and it isn't behaving as expected.

I want to avoid having to write custom convert functions for my enums so
I want to convert to an int and cast to the enum, this wouldn't compile so
I tried a few variations.
The cast fails inside the generic function even though its valid.
The code below is the simplest example of the problem I could think of, even
casting int to int fails.

Is this a limitation or am I doing something wrong?

Generics aren't like C++ templates. If an operation isn't explicitly
labeled as supported by a type argument via with a 'where' clause, it
will cause a compile-time error. Try casting to object first (to the
root of the inheritance hierarchy), then back down again (to avoid an
invalid cross-cast).

Your example doesn't make much sense. 'destination' is an argument
passed by value, so assigning to it has no effect. I'm trying to guess
what problem you're really trying to solve, and this is the best I could
come up with:

---8<---
using System;

class App
{
enum E
{
A, B, C
}

static void Main()
{
Console.WriteLine(Parse<E,int>("0", int.Parse));
Console.WriteLine(Parse<E,int>("1", int.Parse));
Console.WriteLine(Parse<E,int>("2", int.Parse));
}

static TEnum Parse<TEnum,TVia>(string text,
Converter<string,TVia> converter)
{
TVia value = converter(text);
return (TEnum) (object) value;
}
}
--->8---

-- Barry
 
V

Vincent Finn

Your example doesn't make much sense. 'destination' is an argument
passed by value, so assigning to it has no effect. I'm trying to guess
what problem you're really trying to solve, and this is the best I
could come up with:

Hi,
I know the example was a bit silly, but it was the simplest one I could get
the compile error on so I figured it'd be easier to display it that way.

Your solution works perfectly.

Thanks, Vin
 
M

Marc Gravell

I'm not entirely clear on what you are trying to do... but have you
perhaps looked into TypeConverter implementations?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top