|
-
Apr 19th, 2009, 04:20 AM
#1
Thread Starter
Addicted Member
An array of objects and calling their methods, how?!
Hello,
Can someone help me with my issue? I've been wasting so much time on this. Basically I want to have a list of objects and I want to call some specific methods of some specific objects.
E.g.
Code:
MsgBox(Instances(0).Position(0).ToString)
It should alert a number that presents the X position, but it is not working... the box does not even appear and no errors.
This is how I create the array of objects:
Code:
Public Instances As Object()
Any ideas how to get this to work?
-
Apr 19th, 2009, 04:38 AM
#2
Re: An array of objects and calling their methods, how?!
Since you have an array of Objects, you are only going to be able to pull the methods available to that object that belong to Object, which, in this case is .tostring. In order to have multiple object types in an array, and then call their methods, you will need to know what classes you are going to being throwing INTO this array, and then CType according when reading from the array.
If you are just going to put 1 object type into this array, why not use:
Code:
Dim MyArray() as ObjectType
-
Apr 19th, 2009, 04:43 AM
#3
Thread Starter
Addicted Member
Re: An array of objects and calling their methods, how?!
Hi. The object type is a custom object. Basically a new Public Class obj that I create and want to insert into the array for further use.
Code:
Public Class Test
Public TestVar As Integer = 123
End Class
Dim Instances() As Object
Instances(0) = New Test
msgbox(Instances(0).TestVar.ToString)
I need to make that to work. Got a solution for it? Do I need CType here, I am not sure how I am going to use it...
-
Apr 19th, 2009, 04:52 AM
#4
Re: An array of objects and calling their methods, how?!
Okay, what you want to do is replace the word "object" with the name of your class. So, in your case, it'll look this this:
Code:
Dim Instances() as Test
Of course, Instead of an array, I would highly reccommend you use a List( of Test).
Last edited by Campion; Apr 19th, 2009 at 04:56 AM.
-
Apr 19th, 2009, 04:54 AM
#5
Thread Starter
Addicted Member
Re: An array of objects and calling their methods, how?!
Oh I see... well that makes sense actually, but what are the pros of using a List?
-
Apr 19th, 2009, 05:00 AM
#6
Re: An array of objects and calling their methods, how?!
The main pro of using a List(of T) is that you don't have empty items, like you would an array. You can remove items, insert items, and overall have a generally better time using them. Now, I'm not saying that Arrays don't have their place, because they do, but with .NET, they simply are old, outdated, and should be used sparingly (like when splitting strings, etc.)
-
Apr 19th, 2009, 05:45 AM
#7
Thread Starter
Addicted Member
Re: An array of objects and calling their methods, how?!
Code:
MsgBox("yea")
test.Add(New testcls())
MsgBox("yea")
Whenever I run the code, I will only get "yea" popup once.. I am not sure why that code is not proper? I run this in the class:
Code:
Public test As List(Of testcls)
-
Apr 19th, 2009, 06:28 AM
#8
Re: An array of objects and calling their methods, how?!
Before you use any object, you must initialize it: (The List(of T) is an object.)
Code:
Instances = new List(of test)
Dim T as new Test
Instances.Add(T)
T = nothing
Objects 101.
-
Apr 19th, 2009, 06:41 AM
#9
Thread Starter
Addicted Member
Re: An array of objects and calling their methods, how?!
Oh thanks! I was missing the keyword "New"
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
|