|
-
May 10th, 2004, 01:32 AM
#1
Thread Starter
Frenzied Member
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:
public sub WhenSpellcasted(byval spell as cspell, byval strength as double)
'The spell object affects the attributes of the object it was cast on
spell.applymagic(Me,strength)
end sub
Now here is the problem:
I define a class of magician
VB Code:
public class CMagician : inherits Entity
private m_Mana as double = 0
ppublic Sub CastSpell(byval target as Entity, byval strength as double)
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
-
May 10th, 2004, 02:05 AM
#2
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.
-
May 10th, 2004, 02:09 AM
#3
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.
-
May 10th, 2004, 02:51 AM
#4
Thread Starter
Frenzied Member
Im a C++ prgrammer and old habits you now 
Anyway... you are correct... the example should look like this
VB Code:
dim MrX as new CMan
dim Merlin as new CMagician
Merlin.CastSpell(MrX,5,firespell)
and the castspell (implemented by all things that can perform magic) looks like this
VB Code:
public Sub CastSpell(byval target as Entity, byval strength as double, byref spellobj as CSpell)
target.WhenSpellCasted(myfirespell,strength)
and the code for "WhenSpellCasted"
VB Code:
public sub WhenSpellcasted(byval spell as cspell, byval strength as double)
'The spell object affects the attributes of the object it was cast on
spell.applymagic(Me,strength)
end sub
and finally the code for the fire spell object
VB Code:
public sub Applymagic(byval sucker as Entity, byval strength as double)
sucker.Life - = 100 * strength)
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
-
May 10th, 2004, 02:55 AM
#5
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.
-
May 10th, 2004, 03:44 AM
#6
Thread Starter
Frenzied Member
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:
sub Main()
dim Zorro as new CMan
'should not be possible to do here
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
-
May 10th, 2004, 10:46 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|