Use Object data type as return value or Dynamic return value:
After a long time Sql programmers are back to the form. We planned to provide new tips on weekly basics. Thinking about this one of the programmer talks about returning the dynamic data type in functions and it make the tip of the week.
Dynamic object data type
In a function we can return only one data type. If you need to return more data type dynamically, we can pass different parameter and return as object data types.
In asp.net, sometimes the function needs to send dynamic data type. So in this situation we will use object data type as return data type. After get the return value we will convert this to which data type we want.
In the following example GetData() function will return the object data type. If you pass 1 as a parameter, it will return string value “Name” and if you pass 2 as a parameter, it will return integer value “Age” in the same function.
protected void Page_Load(object sender, EventArgs e)
{
string name;
name = Convert.ToString(GetData(1));
Response.Write("Name: " + name + "");
int Age;
Age = Convert.ToInt16(GetData(2));
Response.Write("Age: " + Age + "");
}
protected object GetData(int Index)
{
object ReturnObj = null;
switch (Index)
{
case 1:
ReturnObj = "Robert";
break;
case 2:
ReturnObj = 24;
break;
default:
ReturnObj = null;
break;
}
return ReturnObj;
}