function return type based on input parameter.

M

mathieu

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<Key1> conv;
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
 
H

Hans Kesting

mathieu pretended :
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<Key1> conv;
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
 
M

mathieu

mathieu pretended :











*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
 
H

Hans Kesting

*If* those "some_int_blob" and "some_double_blob" have different types,
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
 

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