[Gtk-sharp-list] Object madness.

JPerrin testosteles@shaw.ca
Tue, 04 Mar 2003 21:37:27 -0700


-----Original Message-----
From: gtk-sharp-list-admin@lists.ximian.com
[mailto:gtk-sharp-list-admin@lists.ximian.com] On Behalf Of George
Farris
Sent: Tuesday, March 04, 2003 8:34 PM
To: gtk-sharp-list
Subject: Re: [Gtk-sharp-list] Object madness.


> Hmm in my "Teach yourself C# in 21 days" it does this:
>=20
> Object x =3D new PI();
> Console.WriteLine("Type: {0}", x.GetType());
>=20
> which returns "Type: PI"
> so..if type is PI why wouldn't it then have access to PI's methods?

This is pretty basic object-oriented stuff.  You can have an object (as
in System.Object) that is of a type: whatever (in C#, everything is an
object), but in order to access the methods of that type you'd have to
cast it.

So if you know that an object is actually also a PI, you can cast it
like so:

PI pi =3D (PI)x;

So the answer to your question... Yes, the object has access to PI's
methods, but only if you tell the compiler that the object *is* a PI. =20

JP




On Tue, 2003-03-04 at 14:54, =D8yvind Hvamstad wrote:
> Hi.
>=20
> This will not work! o is an Object and no method 'method()' exists=20
> there. It is not supposed to work either.
>=20
> The below example does something, that perhaps is what you want? This=20
> can also be done by interfacing.
>=20
> using System;
>=20
> public class Class1
> {
>   public virtual void foo() {
>     Console.WriteLine("Class1");
>   }
> }
>=20
> public class Class2 : Class1
> {
>   public override void foo() {
>     Console.WriteLine("Class2");
>   }
> }
>=20
>=20
> public class Myclass
> {
>   public Myclass () {
>     Class1 c =3D new Class2();
>     c.foo();
>   }
>=20
>   public static void Main() {
>     new Myclass();
>   }
> }
>=20
> On Tue, 2003-03-04 at 22:40, George Farris wrote:
> > I want to be able to do the following but it doesn't really seem to=20
> > work.  From what I have read it should.  Any ideas?
> >=20
> > public class InhClass
> > {
> > }
> >=20
> > public class Class1 : InhClass
> > {
> > 	methods here
> > }
> >=20
> > public class Class2 : InhClass
> > {
> > 	methods here
> > }
> >=20
> >=20
> > public class myclass
> > {
> > 	Object o =3D new Object();
> > =09
> > 	public myclass ()
> > 	{
> > 		if (preference  =3D=3D "pref" )
> > 		{
> > 			Class1 c =3D new Class1();
> > 			o =3D c;
> >=20
> > 			now access methods in Class1 via o.method()
> > 		}
> > }
> > The compiler says System.Object does not contain a reference for
> > o.method()
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list