|
-
Sep 24th, 2006, 01:06 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2.0] Declaring as Object then typecast?
I have an bunch of classes that will have alot of the same methods, some pretty much identical. So, I have an interface that for example has a save method. Now, as a parameter I want to be able to pass different objects.
The way I came up with is:
save(Object obj)
then when I implement it, I typecast obj to whatever object it'll be using in that class. Does this seem logical or am I going about it the wrong way?
Any guidance is appreciated.
-
Sep 24th, 2006, 01:50 PM
#2
Banned
Re: [2.0] Declaring as Object then typecast?
It would be more appropriate to take all of the common functionality that those classes have and put it all in one class. Then for each of those original classes, have them inherit from the common functionality class.
Then instead of Object you would just use the name of the common class, then you wouldn't have to do any type-casting.
If your common methods need to be altered slightly for each type of class then you can override the common class's methods inside each of the derived classes.
The techniques I describe here are called "Inheritance" and "Polymorphism" and are important OOP ideas. There are many books and online tutorials about these topics around so its definitely worth spending a week or three learning about them, you've identified a part of your program that woul bnefit very well from these technologies. Check it out 
As a quick illustration, say your classes that you already have are called: Cat, Dog and Lizard. They are all animals, they all eat and drink, but they do differ in some ways. Cats and dogs are warm blooded lizards are not, you can train dogs but not cats or lizards...
The sensible thing to do is create a BASE class called Animal. This class would contain methods like "Eat", "Drink" and "Climb".
Then go back to your original classes and alter them to inherit from Animal...
Code:
public class Animal //BASE CLASS
{
public void Save()
{}
publc void Eat()
{}
public void Drink()
{}
public void Climb()
{}
}
public class Cat : Animal
{
...
}
public class Dog : Animal
{
...
}
public class Lizard : Animal
{
...
}
Now comes the clever bit. If you have a method in the Animal class called Save(), you can get that method to save the current object. That way you don't need a separate Save(Object ....) method. You could call MyCat.Save() and that would be all you'd need to do to save the cat object.
This sort of thing is tough to explain and I would not expect you to grasp it immediately from what I've written but you should have a scout around for a few tutorials or a good book.
Last edited by monoptic; Sep 24th, 2006 at 02:01 PM.
-
Sep 24th, 2006, 01:52 PM
#3
Re: [2.0] Declaring as Object then typecast?
You are doing it right, it's just like the "sender" in event Handlers.
except for one thing, you shouldn't extract an interface for similar classes, you should create a super class if you want it to hold methods
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 24th, 2006, 01:58 PM
#4
Re: [2.0] Declaring as Object then typecast?
You're using C# 2.0, so you can actually create your class/functions with generic types, you don't have to specify it as an object.
-
Sep 24th, 2006, 02:13 PM
#5
Thread Starter
Lively Member
Re: [2.0] Declaring as Object then typecast?
 Originally Posted by mendhak
You're using C# 2.0, so you can actually create your class/functions with generic types, you don't have to specify it as an object.
That was perfect! Worked exactly like I needed.
Thanks for all the replies, very much appreciated.
-
Sep 24th, 2006, 02:44 PM
#6
Re: [RESOLVED] [2.0] Declaring as Object then typecast?
PS: You have the correct avatar. There is nothing greener on god's green earth than Mountain Dew. Amen.
-
Sep 24th, 2006, 02:53 PM
#7
Thread Starter
Lively Member
Re: [RESOLVED] [2.0] Declaring as Object then typecast?
 Originally Posted by mendhak
PS: You have the correct avatar. There is nothing greener on god's green earth than Mountain Dew. Amen.
This is what i've been trying to explain to my dentist!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|