System.Object

G

Gene Vital

Just curious what anyone else thinks....



I would like all of my class libraries to share a common set of custom
properties and methods, however I don't want to have to recode all of
these properties and methods in each subclass. This is a feature that
MSFT obviously ackknowleges the benefit of since every class in the .NET
framework inherits from the System.Object yet fail to give us access to
a "Root" type object so all of our classes can inherit from
one place.
 
N

Nicholas Paldino [.NET/C# MVP]

Gene,

What's stopping you from creating your own base class and then deriving
all of your classes from that? They don't stop you from doing that in any
way.
 
J

Jon Skeet [C# MVP]

Gene Vital said:
Just curious what anyone else thinks....

I would like all of my class libraries to share a common set of custom
properties and methods, however I don't want to have to recode all of
these properties and methods in each subclass. This is a feature that
MSFT obviously ackknowleges the benefit of since every class in the .NET
framework inherits from the System.Object yet fail to give us access to
a "Root" type object so all of our classes can inherit from
one place.

You don't need Microsoft to give you a class to derive all your other
classes from - you can just create your own one.
 
G

Gene Vital

Jon said:
You don't need Microsoft to give you a class to derive all your other
classes from - you can just create your own one.

I can see how I can do this with non GUI classes but how about GUI
classes like a TextBox or even a Form for that matter??
 
N

Nicholas Paldino [.NET/C# MVP]

Gene,

An object is an object is an object. They are all objects, no one
object is more special than the other. You can derive from a form, or a
control, the same rules apply. You just have to make sure you set the
accessibility correctly (protected) for controls/fields/properties you want
a subclass to access, as well as virtual functions/properties which you want
to be able to be overridden.
 
J

Jon Skeet [C# MVP]

Gene Vital said:
I can see how I can do this with non GUI classes but how about GUI
classes like a TextBox or even a Form for that matter??

You can't do it at that stage. What you *can* do is have a common
interface which has a method to fetch an object which contains all the
properties you need. Then although you'll need a certain amount of code
in each class, it won't be a lot - just the method and a variable to
store the reference to the object it returns.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
An object is an object is an object. They are all objects, no one
object is more special than the other. You can derive from a form, or a
control, the same rules apply. You just have to make sure you set the
accessibility correctly (protected) for controls/fields/properties you want
a subclass to access, as well as virtual functions/properties which you want
to be able to be overridden.

I suspect that what Gene means is that if he's already deriving from
Form, or from TextBox, he can't *also* derive from another "root"
class.
 
G

Gene Vital

Nicholas said:
Gene,

An object is an object is an object. They are all objects, no one
object is more special than the other. You can derive from a form, or a
control, the same rules apply. You just have to make sure you set the
accessibility correctly (protected) for controls/fields/properties you want
a subclass to access, as well as virtual functions/properties which you want
to be able to be overridden.


I understand all of that, my point is that there is no single point
that I can create for ALL subclasses to inherit from.

If I want to add a property to a Form and a TextBox I have to add the
code to my Form subclass and my Textbox subclass repeating the same
identical code in both places. Or are you telling me that I can create a
Form subclass and a Textbox subclass that both inherit from the same
base class? If so then I am missing it somewhere.
 
N

Nicholas Paldino [.NET/C# MVP]

Gene,

I understand now, and unfortunately, you can't do that. MS can do it,
like you said, but then again, they own the code. However, I can't see them
making a change to something like object (at least, not the public face of
it), but something like Control, definitely.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top