473,231 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

return object of an unknown type

hi,
is it possible to return an object of an unknown (but not really unknown)
type with an method?

i have the following situation:

- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the object in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)

it sounds very simple, if you hard-code it, but it should be a little more
dynamic.

here something in c#-pseudocode:
object myobject = ...
Type typeofobject = ...

public typeofobject GetTheObject()
{
return (typeofobject) myobject;
}
Could you help me please?

Thanks in advance,
Alex

Jan 25 '07 #1
9 3924
Yes you can, but you should really have a very good reason for not
knowing in advance what the object type is.
Critically you need to understand a little about inheritence.
Everything in dotnet is derived from object, so a variable of type
object can hold anything. There are several ways of casting from one
type to another.

Here's some code.

object of;
object ot;
Form f = new Form();
TextBox t = new TextBox();

of=f;
ot=t;

f=null;
//cast example
f=(Form)of;

//this will throw an exception so commented out
//f=(form)ot;

//safe way of doing it.
f = of as Form;
if(null != f)
{
Console.WriteLine("of is a form");
}
//f should be null after this as ot points at a textbox
f = ot as Form;
if(null == f)
{
Console.WriteLine("ot is not a form");
}

On 25 Jan, 10:57, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
hi,
is it possible to return an object of an unknown (but not really unknown)
type with an method?

i have the following situation:

- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the object in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)

it sounds very simple, if you hard-code it, but it should be a little more
dynamic.

here something in c#-pseudocode:

object myobject = ...
Type typeofobject = ...

public typeofobject GetTheObject()
{
return (typeofobject) myobject;

}Could you help me please?

Thanks in advance,
Alex
Jan 25 '07 #2
thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return an
other object as this type.

perhaps i have to specifize the situation:

i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass

in MyMainClass there is an Method like this

public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);
}

privateObject is a variable of the type "object" and the method returns it
casted to "OtherType".
in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't want
to rewrite the method completely for every child-class of MyMainClass...

has somebody an idea?

Alex

"DeveloperX" <nn*****@operamail.comschrieb im Newsbeitrag
news:11**********************@j27g2000cwj.googlegr oups.com...
Yes you can, but you should really have a very good reason for not
knowing in advance what the object type is.
Critically you need to understand a little about inheritence.
Everything in dotnet is derived from object, so a variable of type
object can hold anything. There are several ways of casting from one
type to another.

Here's some code.

object of;
object ot;
Form f = new Form();
TextBox t = new TextBox();

of=f;
ot=t;

f=null;
//cast example
f=(Form)of;

//this will throw an exception so commented out
//f=(form)ot;

//safe way of doing it.
f = of as Form;
if(null != f)
{
Console.WriteLine("of is a form");
}
//f should be null after this as ot points at a textbox
f = ot as Form;
if(null == f)
{
Console.WriteLine("ot is not a form");
}

On 25 Jan, 10:57, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
>hi,
is it possible to return an object of an unknown (but not really unknown)
type with an method?

i have the following situation:

- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the object
in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)

it sounds very simple, if you hard-code it, but it should be a little
more
dynamic.

here something in c#-pseudocode:

object myobject = ...
Type typeofobject = ...

public typeofobject GetTheObject()
{
return (typeofobject) myobject;

}Could you help me please?

Thanks in advance,
Alex

Jan 25 '07 #3
Ok, so you have something like this? I'm using Form and TextBox just as
examples.

public class MyMainClass
{
public MyMainClass(){}
public virtual Control GetData()
{
object o = new Form();
return (Form)o;
}
}
public class MyClass2 : MyMainClass
{
public MyClass2(){}
public override Control GetData()
{
object o = base.GetData ();
//will break as base returns a form but its for example purposes
return (TextBox)o;
}
}

If you can do a little example like that which compiles and a comment
for the bit you don't understand it should be a little clearer.
On 25 Jan, 12:07, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return an
other object as this type.

perhaps i have to specifize the situation:

i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass

in MyMainClass there is an Method like this

public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);

}privateObject is a variable of the type "object" and the method returns it
casted to "OtherType".

in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't want
to rewrite the method completely for every child-class of MyMainClass...

has somebody an idea?

Alex

"DeveloperX" <nntp...@operamail.comschrieb im Newsbeitragnews:11**********************@j27g2000c wj.googlegroups.com...
Yes you can, but you should really have a very good reason for not
knowing in advance what the object type is.
Critically you need to understand a little about inheritence.
Everything in dotnet is derived from object, so a variable of type
object can hold anything. There are several ways of casting from one
type to another.
Here's some code.
object of;
object ot;
Form f = new Form();
TextBox t = new TextBox();
of=f;
ot=t;
f=null;
//cast example
f=(Form)of;
//this will throw an exception so commented out
//f=(form)ot;
//safe way of doing it.
f = of as Form;
if(null != f)
{
Console.WriteLine("of is a form");
}
//f should be null after this as ot points at a textbox
f = ot as Form;
if(null == f)
{
Console.WriteLine("ot is not a form");
}
On 25 Jan, 10:57, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
hi,
is it possible to return an object of an unknown (but not really unknown)
type with an method?
i have the following situation:
- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the object
in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)
it sounds very simple, if you hard-code it, but it should be a little
more
dynamic.
here something in c#-pseudocode:
object myobject = ...
Type typeofobject = ...
public typeofobject GetTheObject()
{
return (typeofobject) myobject;
}Could you help me please?
Thanks in advance,
Alex- Hide quoted text -- Show quoted text -
Jan 25 '07 #4

"Alexander Widera" <aw**@removethispartplease-hrz.tu-chemnitz.dewrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return
an other object as this type.

perhaps i have to specifize the situation:

i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass

in MyMainClass there is an Method like this

public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);
}

privateObject is a variable of the type "object" and the method returns it
casted to "OtherType".
in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't want
to rewrite the method completely for every child-class of MyMainClass...
Are you using C# 2.0? If so you could use a generic method:

public virtual T GetData<T>()
{
// .. a lot work to do...
return (T)privateObject;
}

And call GetData<OtherType>() or GetData<AnotherType>()

Kind Regards,
Allan Ebdrup
Jan 25 '07 #5
ok ... i try it:

public class MyMainClass
{
protected virtual object o = new TextBox();

public MyMainClass(){}

public virtual TextBox GetData()
{
// do something..
// only placeholer
// .....

// and return
return (TextBox)this.o;
}
}
public class MyClass2 : MyMainClass
{

// something like this
protected override object o = new Form();

public MyClass2(){}

// and something like this...
public override Form GetData()
{
// do something..
// only placeholer
// .....

// and return
return (Form)this.o;
}
}
the both classes are nearly the same... only the difference, that the Method
GetData() should return different types. is there a way to manage this? (and
not to write the method completely again?)

alex

"DeveloperX" <nn*****@operamail.comschrieb im Newsbeitrag
news:11**********************@k78g2000cwa.googlegr oups.com...
Ok, so you have something like this? I'm using Form and TextBox just as
examples.

public class MyMainClass
{
public MyMainClass(){}
public virtual Control GetData()
{
object o = new Form();
return (Form)o;
}
}
public class MyClass2 : MyMainClass
{
public MyClass2(){}
public override Control GetData()
{
object o = base.GetData ();
//will break as base returns a form but its for example purposes
return (TextBox)o;
}
}

If you can do a little example like that which compiles and a comment
for the bit you don't understand it should be a little clearer.
On 25 Jan, 12:07, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
>thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return
an
other object as this type.

perhaps i have to specifize the situation:

i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass

in MyMainClass there is an Method like this

public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);

}privateObject is a variable of the type "object" and the method returns
it
casted to "OtherType".

in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't
want
to rewrite the method completely for every child-class of MyMainClass...

has somebody an idea?

Alex

"DeveloperX" <nntp...@operamail.comschrieb im
Newsbeitragnews:11**********************@j27g2000 cwj.googlegroups.com...
Yes you can, but you should really have a very good reason for not
knowing in advance what the object type is.
Critically you need to understand a little about inheritence.
Everything in dotnet is derived from object, so a variable of type
object can hold anything. There are several ways of casting from one
type to another.
Here's some code.
object of;
object ot;
Form f = new Form();
TextBox t = new TextBox();
of=f;
ot=t;
f=null;
//cast example
f=(Form)of;
//this will throw an exception so commented out
//f=(form)ot;
//safe way of doing it.
f = of as Form;
if(null != f)
{
Console.WriteLine("of is a form");
}
//f should be null after this as ot points at a textbox
f = ot as Form;
if(null == f)
{
Console.WriteLine("ot is not a form");
}
On 25 Jan, 10:57, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
hi,
is it possible to return an object of an unknown (but not really
unknown)
type with an method?
>i have the following situation:
>- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the
object
in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)
>it sounds very simple, if you hard-code it, but it should be a little
more
dynamic.
>here something in c#-pseudocode:
>object myobject = ...
Type typeofobject = ...
>public typeofobject GetTheObject()
{
return (typeofobject) myobject;
>}Could you help me please?
>Thanks in advance,
Alex- Hide quoted text -- Show quoted text -

Jan 25 '07 #6
What about just putting the shared code in a seperate protected method

public class MyMainClass
{
protected virtual object o = new TextBox();

public MyMainClass(){}

protected void DoSomething(){
// do something..
// only placeholer
// .....
}
public virtual TextBox GetData()
{
this.DoSomething();
// and return
return (TextBox)this.o;
}
}
public class MyClass2 : MyMainClass
{

// something like this
protected override object o = new Form();

public MyClass2(){}

// and something like this...
public override Form GetData()
{
this.DoSomething()
// and return
return (Form)this.o;
}
}
or perhaps a generic class:

public class MyMainClass<T: where T new()
{
protected T o = new T();

public MyMainClass(){}

protected void DoSomething(){
// do something..
// only placeholer
// .....
}
public virtual T GetData()
{
this.DoSomething();
// and return
return this.o;
}
}

and use MyMainClass<TextBoxand MyMainClass<Formwitch repalces your two
previous classes.

Kind Regards,
Allan Ebdrup
Jan 25 '07 #7

I understand now, no :)
You can overload methods but they have to have different signatures and
return type is not part of the signature.

So the following is legal, but a version where getdata takes no
parameters in both is not.
I think Allan's ideas are best.

public class MyMainClass
{
/// <summary>
/// test
/// </summary>
public MyMainClass(){}
/// <summary>
/// test2
/// </summary>
/// <returns></returns>
public virtual Control GetData()
{
object o = new Form();
return (Form)o;
}
public virtual Form GetData(Form pForm)
{
object o = new Form();
return (Form)o;
}
}
public class MyClass2 : MyMainClass
{
public MyClass2(){}
public override Control GetData()
{
MyMainClass a = new MyMainClass();
a.GetData();
object o = base.GetData ();
//will break as base returns a form but its for example purposes
return (TextBox)o;
}

public override Form GetData(Form pForm)
{
return base.GetData (pForm);
}
}

On 25 Jan, 13:27, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
ok ... i try it:

public class MyMainClass
{
protected virtual object o = new TextBox();

public MyMainClass(){}

public virtual TextBox GetData()
{
// do something..
// only placeholer
// .....

// and return
return (TextBox)this.o;
}

}public class MyClass2 : MyMainClass
{

// something like this
protected override object o = new Form();

public MyClass2(){}

// and something like this...
public override Form GetData()
{
// do something..
// only placeholer
// .....

// and return
return (Form)this.o;
}

}the both classes are nearly the same... only the difference, that the Method
GetData() should return different types. is there a way to manage this? (and
not to write the method completely again?)

alex

"DeveloperX" <nntp...@operamail.comschrieb im Newsbeitragnews:11**********************@k78g2000c wa.googlegroups.com...
Ok, so you have something like this? I'm using Form and TextBox just as
examples.
public class MyMainClass
{
public MyMainClass(){}
public virtual Control GetData()
{
object o = new Form();
return (Form)o;
}
}
public class MyClass2 : MyMainClass
{
public MyClass2(){}
public override Control GetData()
{
object o = base.GetData ();
//will break as base returns a form but its for example purposes
return (TextBox)o;
}
}
If you can do a little example like that which compiles and a comment
for the bit you don't understand it should be a little clearer.
On 25 Jan, 12:07, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return
an
other object as this type.
perhaps i have to specifize the situation:
i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass
in MyMainClass there is an Method like this
public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);
}privateObject is a variable of the type "object" and the method returns
it
casted to "OtherType".
in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't
want
to rewrite the method completely for every child-class of MyMainClass...
has somebody an idea?
Alex
"DeveloperX" <nntp...@operamail.comschrieb im
Newsbeitragnews:11**********************@j27g2000c wj.googlegroups.com...
Yes you can, but you should really have a very good reason for not
knowing in advance what the object type is.
Critically you need to understand a little about inheritence.
Everything in dotnet is derived from object, so a variable of type
object can hold anything. There are several ways of casting from one
type to another.
Here's some code.
object of;
object ot;
Form f = new Form();
TextBox t = new TextBox();
of=f;
ot=t;
f=null;
//cast example
f=(Form)of;
//this will throw an exception so commented out
//f=(form)ot;
//safe way of doing it.
f = of as Form;
if(null != f)
{
Console.WriteLine("of is a form");
}
//f should be null after this as ot points at a textbox
f = ot as Form;
if(null == f)
{
Console.WriteLine("ot is not a form");
}
On 25 Jan, 10:57, "Alexander Widera"
<a...@removethispartplease-hrz.tu-chemnitz.dewrote:
hi,
is it possible to return an object of an unknown (but not really
unknown)
type with an method?
i have the following situation:
- a variable (A) of the type "object" which contains the object
- a variable (B) of the type "Type" which contains the type of the
object
in
(A) (A should be of the type B)
- a method which should return the object (A) as type (B)
it sounds very simple, if you hard-code it, but it should be a little
more
dynamic.
here something in c#-pseudocode:
object myobject = ...
Type typeofobject = ...
public typeofobject GetTheObject()
{
return (typeofobject) myobject;
}Could you help me please?
Thanks in advance,
Alex- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Jan 25 '07 #8
On 25 Jan, 13:24, "Allan Ebdrup" <ebd...@noemail.noemailwrote:
Are you using C# 2.0? If so you could use a generic method:

public virtual T GetData<T>()
{
// .. a lot work to do...
return (T)privateObject;
}

And call GetData<OtherType>() or GetData<AnotherType>()
To expand on Allan's suggestion, I would add the assertion that the
object of type 'T' must inherit from your base class, by using the
following syntax:

public virtual T GetData<T>() where T: MyBaseClass
{
return (T)privateObject;
}

Call it like this:

MyDerivedClass derived = GetData<MyDerivedClass>();

Jan 25 '07 #9
yes! that's it! thank you...
"Allan Ebdrup" <eb****@noemail.noemailschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP06.phx.gbl...
What about just putting the shared code in a seperate protected method

public class MyMainClass
{
protected virtual object o = new TextBox();

public MyMainClass(){}

protected void DoSomething(){
// do something..
// only placeholer
// .....
}
public virtual TextBox GetData()
{
this.DoSomething();
// and return
return (TextBox)this.o;
}
}
public class MyClass2 : MyMainClass
{

// something like this
protected override object o = new Form();

public MyClass2(){}

// and something like this...
public override Form GetData()
{
this.DoSomething()
// and return
return (Form)this.o;
}
}
or perhaps a generic class:

public class MyMainClass<T: where T new()
{
protected T o = new T();

public MyMainClass(){}

protected void DoSomething(){
// do something..
// only placeholer
// .....
}
public virtual T GetData()
{
this.DoSomething();
// and return
return this.o;
}
}

and use MyMainClass<TextBoxand MyMainClass<Formwitch repalces your two
previous classes.

Kind Regards,
Allan Ebdrup

Jan 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stefano Masini | last post by:
Hi, I'm scripting Adobe InDesign CS with python, through COM interface. This is how I get the application object: win32com.client.Dispatch('InDesign.Application') I'm not a guru of com...
2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
30
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
0
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
2
by: Damon | last post by:
Hi, Created the template function below but the g++ compiler keeps complaining "no matching function for call to....". Is there a way to make it work? Thank you. ==========snip part of...
6
by: George | last post by:
Hi All, I thought this would not compile because no return value is specified. But it does compile and run (aix and xlc v7.0.) Could someone kindly please point me to where in the spec this...
5
by: Jeroen Ceuppens | last post by:
How can you set that a function returns a type, but you don't know it from before? voidunknow Function() ? unknow Function()? Thx
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.