Results 1 to 7 of 7

Thread: OO problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    OO problem

    Im writing a really simple class that is to be the foundation of an adventuregame in D3D... Just for me to learn DirectX

    Anyway I have defined an abstract baseclass "Entity" which can represent any given object in the world, rock, bridge, weapon etc.

    In this baseclass I have an abstract sub called "WhenSpellCasted" which has these parameters

    Spell as CSpell <-- the spell casted

    Strength as double <--how strong the spell was (depending on the magic capabilities of the caster

    Naturally I want any item to respond one way or another to a spell, perhaps reveal hidden items, or nothing happens...

    Then I create an object CMan : inherits Entity
    This class has lots of properties that a man has... and I also implement then sub "WhenSpellCasted" that looks kinda like this

    VB Code:
    1. public sub WhenSpellcasted(byval spell as cspell, byval strength as double)
    2.  
    3. 'The spell object affects the attributes of the object it was cast on
    4. spell.applymagic(Me,strength)
    5.  
    6. end sub


    Now here is the problem:

    I define a class of magician

    VB Code:
    1. public class CMagician : inherits Entity
    2. private m_Mana as double = 0
    3.  
    4. ppublic Sub CastSpell(byval target as Entity, byval strength as double)
    5.  
    6. target.WhenSpellCasted(myfirespell,10)

    Here I get an error saying that the entity object can't be implicitly converted to a CMan object...

    Why is that? The whole point of using base classes if for the purpose Im using it as...?



    Kidn regards
    Henrik

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Huh I don't see a reference to cMan in your code.

    Also prefixing c to the start of all class names is no longer the preferred method in .net, not that it really matters since it always comes down to personal preference anyway.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Whats the method signature for ApplyMagic?

    It should be:

    Public Sub ApplyMagic(target As Entity, strength as double)

    If it is:
    Public Sub ApplyMagic(target As cMan, strength as double)

    Then you can only pass cMan to it not all entity types.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Im a C++ prgrammer and old habits you now


    Anyway... you are correct... the example should look like this

    VB Code:
    1. dim MrX as new CMan
    2.  
    3.  
    4. dim Merlin as new CMagician
    5.  
    6.  
    7. Merlin.CastSpell(MrX,5,firespell)

    and the castspell (implemented by all things that can perform magic) looks like this

    VB Code:
    1. public Sub CastSpell(byval target as Entity, byval strength as double, byref spellobj as CSpell)
    2.  
    3. target.WhenSpellCasted(myfirespell,strength)


    and the code for "WhenSpellCasted"

    VB Code:
    1. public sub WhenSpellcasted(byval spell as cspell, byval strength as double)
    2.  
    3. 'The spell object affects the attributes of the object it was cast on
    4. spell.applymagic(Me,strength)
    5.  
    6. end sub


    and finally the code for the fire spell object

    VB Code:
    1. public sub Applymagic(byval sucker as Entity, byval strength as double)
    2.  
    3. sucker.Life - = 100 * strength)
    4.  
    5. end sub



    With this code I get an error on the line

    Merlin.CastSpell(MrX,5,firespell)

    and the precompiler complains that it can't cast from Entity to CMan



    any and all help is appreciated!!

    kind regards
    Henrik

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    And you are saying that cMan inherits from Entity? Do you have option strict on? If so just try casting it specifically to entity first.

    Merlin.CastSpell(directcast(MrX,entity),5,firespell)

    If it doesn't like that cast then make sure that cMan truely does inherit from Entity.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Yes, thats correct I always use Option Strict On

    But I thought this was a valid operation in terms of good OO programming and therefor I couldn't realize that the option strict didn't like it... I thought I had defined the base class incorrect or something.... thanks for the help anyway!


    Oh, I have another "wierd" question...

    Is it possible to define visibility to certain classes only? For example I want the sub WhenSpellCasted(in the entity class) to be visible only to classes that are of type Magician... so I shouldn't be able to write

    VB Code:
    1. sub Main()
    2.  
    3. dim Zorro as new CMan
    4.  
    5. 'should not be possible to do here
    6. Zorro.WhenSpellCasted(blabla)
    Is there a modifier that can help me here??

    this has nothing to do with functionality, but I think it looks better to implement proper visibility... but sometimes I lack the skills...

    kind regards
    Henrik

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Not really unless you moved cMagician to a different assembly and did some other funky stuff with it. Either that or you can make it a method of cMagician only. Or Maybe I don't understand what you are trying to do?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width