Array of Classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've got an array of a class say dim mydogs(50) of dog

I create an instance of a dog mydogs(0) = new mydogs("milo")

That all works fine. However I want to search the array for a dog named
Fido. When I get to an array element that hasn't been created I get an
error. I understand why this happens, the object hasn't been created, just
declared.

Is there some way to say:

if mydogs(0)."has been created" then
if mydogs(0).name = "Fido" then found = true
end if

If there's a better way to accomplish the task I would be all happy to try it.

Thanks,
Ron
 
Ron,
Is there some way to say:

if mydogs(0)."has been created" then
if mydogs(0).name = "Fido" then found = true
end if

You could simply check to see if the array object at the given index is not
null. This way, you can check to see if the object is indeed created, and
after that you can check its name.

I think the Visual Basic syntax would be something like:

If Not IsNothing(mydogs(0)) Then ...

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
sure... don't use an array. Use an arraylist instead.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Jani,

Didn't work but thanks for the try.

Ron

Jani Järvinen said:
Ron,


You could simply check to see if the array object at the given index is not
null. This way, you can check to see if the object is indeed created, and
after that you can check its name.

I think the Visual Basic syntax would be something like:

If Not IsNothing(mydogs(0)) Then ...

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top