I want to give a parameter to a method and i want my method to return data by looking the parameter. Data can be in type of boolean, string, int or etc. How can i return a variable type from a method? I don't want to return an object type and then cast it to another type. For example:
BlaBla VariableReturnExampleMethod(int a)
{
if (a == 1)
return "Demo";
else if (a == 2)
return 2;
else if (a == 3)
return True;
else
return null;
}
The reason why i want that is i have a method that reads a selected column of a row from the database. Types of columns are not same but i have to return every column's information.
It sounds like this might be a good case for generics. If you know what data type you're expecting when you call it, you can call that particular generic version of the function.
Well that's basically what you do have to do. Alternatively, if you're using C# 4 you could make the return type
dynamic
, which will allow the conversion to be implicit:Fundamentally, you specify types to let the compiler know what to expect. How can that work if the type is only known at execution time? The reason the
dynamic
version works is that it basically tells the compiler to defer its normal work until execution time - so you lose the normal safety which would let the second example fail at compile time.Use return type as
object
, then you are able to get any return type. you have to handle the return type ether through reflection or other method.check this:
Edit: I am in the favor of strongly typed objects and you can implement it easily on .net platform.
Use
dynamic
Keyword in place ofBlahBlah
if you are targeting.Net 4.0
but if lesser one thenobject
is your safest bet because it is the base class for every other class you can think of.Consider using something like Dapper-dot-net (written by Marc Gravell and Sam Saffron at our very own Stack Overflow) to pull things out of the DB. It handles the database to object mapping for you.
Furthermore, if you don't want to use a tool, and you're pulling from a Database, and you know the data types of the various columns at compile time (like it sounds you do), you should probably be working row-by-row rather than column-by-column.
Note: you could use object initializer syntax, and linq here but this is the most basic way I could think of demoing this without using a ton of extra stuff.
Also note, that here I'm assuming that you don't actually want to return "Demo", 2, and true, but values that use the row. That just means you'd change the hard coded values to:
row.GetStringType(stringColumnIdx)
or something similar.I look on your asks and one is better than second, but last i must rewritting to better understand solution. And this solution skiped long if else stack and replacing it by foreach on Types enum, where we can implement all types what we need. I more like using dynamic, but this is usable too.
Main function
GetValueByValidating
returned value if is type defined and possible, in other cases return false Look niranjan-kala this is your main function after rewriting.