which way is better?

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

This is where my OO skills aren't quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character's class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type class?

Which is better? Also, if I did the first choice, how would this affect
the methods that determine whether a character is of a certain class,
assuming that the subclasses themselves are specific for each class?
E.g., wizard class, fighter class, etc. This would mean I wouldn't need
those particular methods?

I'm still trying to wrap my mind around how to organize this kind of stuff!
 
John Salerno said:
This is where my OO skills aren't quite honed yet. Some of you might
remember the little game program I was working on for fun. I had several
methods written to determine a character's class, race, etc. My question
is, should I put all those questions in a Character base class (so that
all subclasses inherit them and I can call them straight from the
objects), or should I put them as static methods in a Validator type
class?

Well, how do the methods work? Are they something like IsType(Class class,
Race race); or are you talking about class properties?

Subclasses and class properties should be enough, I would think. Could you
explain your situation a little more?
 
Daniel said:
Well, how do the methods work? Are they something like IsType(Class class,
Race race); or are you talking about class properties?

Subclasses and class properties should be enough, I would think. Could you
explain your situation a little more?

Certainly. I have a whole mess of methods like this:

public bool GetIsWizard()
{
if (//code that shows he's a wizard)
return true;
else
return false;
}

Now, the code in the if statement is checking whether a radio button is
checked, so perhaps I should change this to a property like you said?
 
John Salerno said:
Certainly. I have a whole mess of methods like this:

public bool GetIsWizard()
{
if (//code that shows he's a wizard)
return true;
else
return false;
}

Now, the code in the if statement is checking whether a radio button is
checked, so perhaps I should change this to a property like you said?

Well, you wouldn't want a bunch of properties like "Wizard, Fighter, Chicken
Tamer", but two properties using an enum of classes and an enum of races
might be appropriate. The other option is to use subclasses and then the
'is' statement to determien the type.

However, since you are writing a game, and I would assume its possilbe for a
character to change classes, I would go with properties since it allows you
to change things. Using subclasses would be limiting since the class would
have to change every time a class changed and you'd end up with alot of
classes(HumanWIzard, HumanFighter, LizardWizard, etc). Still, without using
subclasses you end up with complicated code logic(if different classes\races
require different logic). This might be a situation where composition is the
best way to do things, gotta think about that one a bit.

Still, I would use a property here, with an enum type like I mentioned
above.
 
Daniel said:
However, since you are writing a game, and I would assume its possilbe for a
character to change classes

Good point. It's also possible to have multiple classes at once.
 
John Salerno said:
Good point. It's also possible to have multiple classes at once.

Then a property with a flags enum is definatly the way I'd go, although I
might consider writing a generic utility method to do comparisons:

public static bool IsClass(Unit u, Class cls)
{
//check against units class flags
//to see if the one(s) in cls are set
}
 
Daniel said:
Daniel O'Connell [C# MVP] wrote:

However, since you are writing a game, and I would assume its possilbe
for a character to change classes

Good point. It's also possible to have multiple classes at once.


Then a property with a flags enum is definatly the way I'd go, although I
might consider writing a generic utility method to do comparisons:

public static bool IsClass(Unit u, Class cls)
{
//check against units class flags
//to see if the one(s) in cls are set
}

Thanks for the responses. I'll keep working on it and see how it turns
out with properties.
 
Daniel said:
Daniel O'Connell [C# MVP] wrote:

However, since you are writing a game, and I would assume its possilbe
for a character to change classes

Good point. It's also possible to have multiple classes at once.


Then a property with a flags enum is definatly the way I'd go, although I
might consider writing a generic utility method to do comparisons:

public static bool IsClass(Unit u, Class cls)
{
//check against units class flags
//to see if the one(s) in cls are set
}

Oh wait, what exactly is a 'flags enum'?
 
John Salerno said:
Daniel said:
Daniel O'Connell [C# MVP] wrote:


However, since you are writing a game, and I would assume its possilbe
for a character to change classes

Good point. It's also possible to have multiple classes at once.


Then a property with a flags enum is definatly the way I'd go, although I
might consider writing a generic utility method to do comparisons:

public static bool IsClass(Unit u, Class cls)
{
//check against units class flags
//to see if the one(s) in cls are set
}

Oh wait, what exactly is a 'flags enum'?

Its an enum using bit logic to set flags. I callthem a flags enum becase you
set the FlagsAttribute

[Flags]
enum Class
{
Wizard = 1, //0000 0001 bit set
Fighter = 2, //0000 0010 bit set
Knight = 4, //0000 0100 bit set
}

and so on. You can use the bitwise operators (& and |) to do the standard
bitwise operations on them. If you don't know the bitwise logic stuff do a
google search to pick up the theory.

The "Did you know" section of this blog gives an example of using a flags
enum: http://msmvps.com/kevinmcneish/archive/2004/12/07/22768.aspx
 
Daniel said:
Its an enum using bit logic to set flags. I callthem a flags enum becase you
set the FlagsAttribute

[Flags]
enum Class
{
Wizard = 1, //0000 0001 bit set
Fighter = 2, //0000 0010 bit set
Knight = 4, //0000 0100 bit set
}

and so on. You can use the bitwise operators (& and |) to do the standard
bitwise operations on them. If you don't know the bitwise logic stuff do a
google search to pick up the theory.

Ah, luckily I do know what this is. I read about bit flags a while ago,
and just a few days ago I read how to implement it in an enum.
 
Back
Top