Connecting Tech Pros Worldwide Forums | Help | Site Map

function return type based on input parameter.

mathieu
Guest
 
Posts: n/a
#1: Nov 6 '08
Hi there,

I discovered C# only last week, I need to learn at least the basis
to wrap the interface of my C++ lib in C#. To offer a nice API to my
C# user I am looking to implement something like this:

Convertor convertor = new Convertor();
int a = convertor.Convert( some_int_blob );
double b = convertor.Convert( some_double_blob );

What this is means is that only on input (eg. some_int_blob) the
convertor should be able to determine whether or not the input was
indeed of type int, and return the proper int, otherwise should throw
some exeption...

I was able to do it in Python, since everything derive from a
toplevel object. I was able to do it in C++, at compile time:

Convertor<Key1conv;
Key1::Type value = conv.Convert( some_int_blob ); // type is deduced
from a key

What I am looking now is to do it in C#. Should I go the Python way
and have the Convert function return an 'object', then this is up to
the application programmer to convert this object back to int/float/
string ...

Thanks
-Mathieu

Hans Kesting
Guest
 
Posts: n/a
#2: Nov 6 '08

re: function return type based on input parameter.


mathieu pretended :
Quote:
Hi there,
>
I discovered C# only last week, I need to learn at least the basis
to wrap the interface of my C++ lib in C#. To offer a nice API to my
C# user I am looking to implement something like this:
>
Convertor convertor = new Convertor();
int a = convertor.Convert( some_int_blob );
double b = convertor.Convert( some_double_blob );
>
What this is means is that only on input (eg. some_int_blob) the
convertor should be able to determine whether or not the input was
indeed of type int, and return the proper int, otherwise should throw
some exeption...
>
I was able to do it in Python, since everything derive from a
toplevel object. I was able to do it in C++, at compile time:
>
Convertor<Key1conv;
Key1::Type value = conv.Convert( some_int_blob ); // type is deduced
from a key
>
What I am looking now is to do it in C#. Should I go the Python way
and have the Convert function return an 'object', then this is up to
the application programmer to convert this object back to int/float/
string ...
>
Thanks
-Mathieu
*If* those "some_int_blob" and "some_double_blob" have different types,
you could use overloading:

int Convert(MyIntBlobType intBlob) {}
double Convert(MyDoubleBlobType doubleBlob) { }

Hans Kesting


mathieu
Guest
 
Posts: n/a
#3: Nov 6 '08

re: function return type based on input parameter.


On Nov 6, 11:10 am, Hans Kesting <news.han...@spamgourmet.comwrote:
Quote:
mathieu pretended :
>
>
>
Quote:
Hi there,
>
Quote:
I discovered C# only last week, I need to learn at least the basis
to wrap the interface of my C++ lib in C#. To offer a nice API to my
C# user I am looking to implement something like this:
>
Quote:
Convertor convertor = new Convertor();
int a = convertor.Convert( some_int_blob );
double b = convertor.Convert( some_double_blob );
>
Quote:
What this is means is that only on input (eg. some_int_blob) the
convertor should be able to determine whether or not the input was
indeed of type int, and return the proper int, otherwise should throw
some exeption...
>
Quote:
I was able to do it in Python, since everything derive from a
toplevel object. I was able to do it in C++, at compile time:
>
Quote:
Convertor<Key1conv;
Key1::Type value = conv.Convert( some_int_blob ); // type is deduced
from a key
>
Quote:
What I am looking now is to do it in C#. Should I go the Python way
and have the Convert function return an 'object', then this is up to
the application programmer to convert this object back to int/float/
string ...
>
Quote:
Thanks
-Mathieu
>
*If* those "some_int_blob" and "some_double_blob" have different types,
you could use overloading:
>
int Convert(MyIntBlobType intBlob) {}
double Convert(MyDoubleBlobType doubleBlob) { }
Ooops sorry, I forgot to mention that "some_int_blob" and
"some_double_blob" have exactly the same type.

Thanks
Hans Kesting
Guest
 
Posts: n/a
#4: Nov 6 '08

re: function return type based on input parameter.


>*If* those "some_int_blob" and "some_double_blob" have different types,
Quote:
Quote:
>you could use overloading:
>>
>int Convert(MyIntBlobType intBlob) {}
>double Convert(MyDoubleBlobType doubleBlob) { }
>
Ooops sorry, I forgot to mention that "some_int_blob" and
"some_double_blob" have exactly the same type.
>
Thanks
Then you would have to return "object" and have the consumer cast to
the appropriate type.
Or maybe you can change the Convert method to generic:
T Convert<T>(blob) {}

and use it like:
Convertor convertor = new Convertor();
int a = convertor.Convert<int>( some_int_blob );
double b = convertor.Convert<double>( some_double_blob );

Hans Kesting


Closed Thread