473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Casting in a generic function

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_typeconverter)
{
Intermediate_type test = converter.Invoke(source);
// Error on next line: Cannot convert type 'Intermediate_type' to 'Destination_type'
destination = (Destination_type)test;
}
Mar 28 '07 #1
3 4982
Vincent Finn wrote:
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,TViaconverter)
{
TVia value = converter(text);
return (TEnum) (object) value;
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Mar 28 '07 #2
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
Mar 28 '07 #3
I'm not entirely clear on what you are trying to do... but have you
perhaps looked into TypeConverter implementations?

Marc
Mar 28 '07 #4

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

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
3
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return...
4
by: casul | last post by:
Hi All, Given the following code that just defines a dummy class "my_class" and a dummy wrapper "my_wrapper" with a main program : #include <iostream> template< typename _Tag > class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.