Click to See Complete Forum and Search --> : Help with classes


Waseem Sadiq
January 10th, 2000, 09:09 AM
I have a classfile called cBot. So I declare vars like
dim Anarki as new cBot
dim Sarge as new cBot
dim Xaero as new cBot


In my program I let the user choose any of these three (actually 32, but let's keep it simple) bots (computer-controlled players for the game quake3arena) and afterwards I want to load a text-file wich contains settings like name, speed and stuff. So i created a Function like this public Function ParseFile(filename as string, Botname as cBot)


I have done this because my parsing routine will be able to load the settings from the file and put them in Botname.name

and Botname.speed

, regardless if Botname is Anarki, Sarge or Xaero. But I keep getting a ByRef type mismatch. I think that's because when I call the ParseFile function I pass Botname as a string, obvious.

My Qustion is if there is anyway to do that thingie with the classfiles without getting errors.

And another question. What's the difference between the following code:
Dim Mybot as cBot
set Mybot = new cBot

and
Dim Mybot as new cBot

or is it both the same?
Thanks a lot.

Chris Eastwood
January 10th, 2000, 09:28 AM
>, regardless if Botname is Anarki, Sarge or Xaero. But I keep getting a
>ByRef type mismatch. I think that's because when I call the ParseFile
>function I pass Botname as a string, obvious.

>My Qustion is if there is anyway to do that thingie with the classfiles
>without getting errors.

You should pass the 'cBot' variable as just that :


Call ParseFile ("c:\a-bot.file", Sarge)




You can then access all the properties of that bot object within the ParseFile routine.


>
>And another question. What's the difference between the following code:
>
>Dim Mybot as cBot
>set Mybot = new cBot
>
>and
>
>Dim Mybot as new cBot
>or is it both the same?

No it's not the same - in fact, it's a bad habit to get into.

Basically, when you declare an object as a 'new type', eg


Dim MyBot as new cBot




Visual Basic won't actually create that object until it comes across a reference to it in your code, eg :


Dim MyBot as new cBot

MyBot.Name = "Fred" ' <- Object is created at this point




- you might think, So What ? Let's expand the code a little further :


Dim MyBot as new cBot

MyBot.Name = "Fred" ' <- Object is created at this point
'
' Do some stuff.....
'
<snip>
'
set MyBot = nothing ' <- Kill off object
'
' More Stuff
'
If MyBot is nothing then ' <- Just checking will recreate the object !
MsgBox "MyBot successfully killed off"
End If
'




Visual Basic wraps the code of any object declared 'as new xxx' with checks to see if the object has been created yet. This add's overhead to your final program, and causes 'gotchas' like the one shown above (try it out and you'll see that the message box will never be displayed as long as you declare 'as new').



Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb