Returning Object works, you'd have to cast the Object back to the type of the variable you're assigning. Something like: int ival = (int)myObject.getText(s, len, type);
A much cleaner way that doesn't require casting is to provide overloads of the getText method, one for each type you're handling. For example: class MyObject { public void getText(String s, out int value) { value = 1; } public void getText(string s, out double value) { value = 2; } }
int ival; double dval;
switch(str) { case "N": myObject.getText(s, out ival); // Do something with ival... break; case "D": myObject.getText(s, out dval); break; ... }
Nope, nobugz is referring to Reflection to retreive the returntype of a method you specified earlier, or one you get from an assembly. You would like to write a method without knowing the type you are returning, right
Maybe defining an interface which you can return will do the trick: define an interface for the basics, and let your objects imlpement it. That way you can return any object as long as it implements your interface.
An example (BTW: of course it is not best practice to define the interface and the implementing class and a calling class in the same file, but this is just a sample ):
public class InterfaceTest { /// <summary> /// Interface definition, containing a name /// </summary>
public interface ITest { string Name { get; set; } }
/// <summary> /// Class definition, implementing ITest and adding an Age property /// </summary> public class TestA : ITest { private string name; private int age;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } } }
/// <summary> /// Method with a returntype of 'ITest'. This can be an instance of TestA, r any other class implementing the ITest interface /// </summary> /// <returns>An object implementing ITest</returns> public ITest GetObject() { return new TestA(); } }
If "type" is an actual type, you can use generics for this as well:
public T getText<T>(string s, int len) { //return whatever you wish return default(T); }
switch(str){ case "N": len = str.length; myObject.getText<DateTime>(s, len); .....
Not sure if this is what you mean, though it requres that the calling code knows the return type in advance (although you'll need that to cast to if you're going to use it).
Thank you for you replies. What i want is that i dont want to fix the return type of the method at time of compilation. depending on certain conditions i would like to change it between int , string etc..
Any
possible return type always converts to an Object. As long as the
clients can figure it out, make the return type Object. It's the
.NET version of a Variant...
Returning an Object as nobugz suggests is a workable solution, although it involves "turning off" many features that C# provides for you. IF you could provided a more specific example of what you want to do, we could provide a more appropriate response.
Returntype of Methods
Handschuh
int ival = (int)myObject.getText(s, len, type);
A much cleaner way that doesn't require casting is to provide overloads of the getText method, one for each type you're handling. For example:
class MyObject
{
public void getText(String s, out int value) { value = 1; }
public void getText(string s, out double value) { value = 2; }
}
int ival;
double dval;
switch(str) {
case "N":
myObject.getText(s, out ival);
// Do something with ival...
break;
case "D":
myObject.getText(s, out dval);
break;
...
}
Lawrence Parker
thanks for ur reply. will check it out....
regards
swingme
Sculptor
thanks for ur reply. with objects i have tried out and it works.. but as u told may be overloading may be a much better solution.. will try it.
regards
swingme
rapidexposure
Maybe defining an interface which you can return will do the trick: define an interface for the basics, and let your objects imlpement it. That way you can return any object as long as it implements your interface.
An example (BTW: of course it is not best practice to define the interface and the implementing class and a calling class in the same file, but this is just a sample
public class InterfaceTest
{
/// <summary>
/// Interface definition, containing a name
/// </summary>
public interface ITest
{
string Name
{
get;
set;
}
}
/// <summary>
/// Class definition, implementing ITest and adding an Age property
/// </summary>
public class TestA : ITest
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
/// <summary>
/// Method with a returntype of 'ITest'. This can be an instance of TestA, r any other class implementing the ITest interface
/// </summary>
/// <returns>An object implementing ITest</returns>
public ITest GetObject()
{
return new TestA();
}
}
Jessica Alba
Thanks for ur reply. That means i can define a method like
private <reflection> Test()
{
.................
.................
return <reflection....>
}
ist it
regards
swingme
Franco Finstad
If "type" is an actual type, you can use generics for this as well:
public T getText<T>(string s, int len)
{
//return whatever you wish
return default(T);
}
switch(str){
case "N":
len = str.length;
myObject.getText<DateTime>(s, len);
.....
Not sure if this is what you mean, though it requres that the calling code knows the return type in advance (although you'll need that to cast to if you're going to use it).
JD Prasad
WinFXGuy
regards
swingme
boxelder
Joe Duanel
regards
swingme
GWeston
jerry_tseng
regards
swingme
NeilG
switch(str) {
case "N":
len = str.length;
myObject.getText( s, len,type);
break;
case "D":
len = str.length;
myObject.getText(s, len,type);
break;
.........................
........................
Ist it possible for GetText to return an object depending on "type" GetText may return date , string...etc
regards
swingme
sagittarian