Interfaces, implements etc. Tutorial :)

Posted on 2003-02-20
Medium Priority
Last Modified: 2010-05-01
Hello to all.
I need a "tutorial" about interfaces, derived classes, and so on.
This is not a true question, so at the end of this little discussion i will create some fake questions to give points to the ones that helps me about that.

I will try to explain what i need to build, i hope you can understand my terrible english.

On the description, i will use CAPITALIZED words to evidentiate the classes i think i will need.

I need to build a game, simple and not graphical at all.
The games it's about two wizards, that casts spells to kill the enemy.

A SPELL can be what you want (a fireball, a shield, etc..) but the most important is that a SPELL can be a MONSTER (the WIZARD summons a Dragon, for example).
A SPELL have always a target, that can be a WIZARD, a MONSTER or an other SPELL.

A CREATURE is a live body. It have a certain number of LifePoints (0=dead), and a number of DamagePoints (how much LifePoints the creature can subtract at his target).
A CREATURE can also have a SPELL on it (it can be protected by a Shield, or diseased by a Poison Spell, etc.). All this "properties" are commons for WIZARDS and for MONSTERS, so a WIZARD is a CREATURE, and a MONSTER is also a CREATURE.
CREATURES can own the method to manage damages and LifePoints.

So these can be the basic properties for that classes:
  Caster as WIZARD
  Target as ... ?????? (can be anything... help)
  DamagePoints as Integer

  LifePoints as Integer
  DamagePoints as Integer
  ActiveSpells() as SPELL  
  Sub TakeDamage(HowMuch as integer)

  (same properties of the CREATURE)
  CurrentCastingSpell as SPELL

  (same properties of the CREATURE class)
  (same properties of the SPELL class but Target is limited to CREATURES)

Now the questions...

1) How can i "descend" the WIZARD and MONSTER classes from the CREATURE class?
If i can do that, i can use "Wizard1.LifePoints=10" even if "LifePoints" is a property of the CREATURE class.

2) A MONSTER is a CREATURE and a SPELL in the same time... how can i do that?

3) How can i manage the SPELL.Target propertiy?

I've read the VB HELP about interfaces (the one about Animals, Dinosaurs and so on...) but it's a bit confusing...

A bit of "PseudoCode" will be appreciated. :)

Question by:fcp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

webJose earned 1200 total points
ID: 7989124
The spell class can be like:

    Caster as WIZARD
    Target as CREATURE
    DamagePoints as Integer

Then you create a CREATURE class.  This class would look similar to the following.  Note it has no code in it; this is because you won't actually create intances of CREATURE, but only instances of WIZARD and MONSTER.  This means that the CREATURE class is only a definitory class, meaning it just defines an interface.

Option Explicit

Public Property Get LifePoints() As Integer
End Property

Public Property Let LifePoints(ByVal vNewValue As Integer)
End Property

Public Property Get DamagePoints() As Integer
End Property

Public Property Let DamagePoints(ByVal vNewValue As Integer)
End Property

Public Property Get ActiveSpells(ByVal lIndex As Long) As spell
End Property

Public Property Let ActiveSpells(ByVal lIndex As Long, ByVal vNewValue As spell)
End Property

Public Property Get ActiveSpellCount() As Long
End Property

Public Function AddActiveSpell(ByVal oSpell As spell)
End Function

Public Sub TakeDamage(ByVal iHowMuch As Integer)
End Sub

Now, you add a new class module and name it Wizard.  In the general declarations, you add:

Implements CREATURE

Now also add the properties and methods specific to the Wizard class.

Now, add another class module and name it Monster.  Then do the same as for Wizard.

After doing all this, you can instantiate Monsters and Wizards and assign them to CREATURE variables, like this:

Dim oCreature as CREATURE

    set oCreature = new Wizard
    oCreature.TakeDamage 100
    set oCreature = new Monster
    oCreature.AddActiveSpell oMySpell

Hope it helps.

Author Comment

ID: 7992814
mm... ok. Some questions:
A SPELL can target another SPELL... but you declare the Target as CREATURE. What we can do about that?

A MONSTER is a CREATURE *AND* a SPELL... again, how to solve this?

In your code, CREATURE is just an interface of Wizards and Monsters.
So the code to manage LifePoints, DamagePoints, ActiveSpells, etc need to be wrote in the WIZARD and in the MONSTER classes...
Please note that this code is the same for all...there's a way to write this code in one place only?

If i need to know what Wizard is the one set in the oCreature, how can i do that?


Expert Comment

ID: 7995901
Well, you have to tell me how can a spell affect another spell so you can redefine CREATURE to fit all categories.  Maybe even create a more base class, like TARGET, then Creature is a special kind of target, and so on.

The example I gave you should give you the idea of how to use the Implements keyword to solve your programming need.  All you have to do is to carefully define each "object" and then write the classes.

As for your last question, if for example the TakeDamage function is the same for both Wizards and Monsters, then you have to duplicate the code in both classes as VB does not allow inheritance of implementation.  That is a .NET treat, a C++ treat, or some other language treat, but not a VB5 or VB6 treat.

Author Comment

ID: 8107605
Well, apart our following comments, you're focused the problem... even if i'm already stick in the "circular reference" between SPELLS and MONSTER.
Thanks for your attention. :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question