Another obj-oriented question...

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
If I create a new class, there doesn't seem to be any easy way to get all of the instances of that class (say, for instance, in a collection).

I've tried to think of a few things, but the best I can come up with is this. Can someone let me know if there's a better way?

Solution (for the moment):

whenever an instance of the class is created in the main program, add it to a publicaly declared collection:


public collection as new microsoft.visualbasic.collection
dim inst as new widget
collection.add(inst)

However, this seems to have a downside, in that the collection doesn't reside within the definition of the class itself. Is there a nicer way to do this? it seems as if there should be...

Thanks again,
 

Giscardo

Senior member
May 31, 2000
724
0
0
Yes, you should create a static variable of some collection type as part of the class. In the constructor for the class, just add a reference to the new object instance into the collection. Here's an example in C#, not VB.NET, but you should get the picture. Let's say your class name is Foo:

public class Foo
{
private static System.Collections.ArrayList _fooInstances;

public static ArrayList FooInstances
{
get { return _fooInstances; }
}

// Public constructor:
public class Foo()
{
// do some constructor work here
_fooInstances.Add(this);
}
}

EDIT: sorry about the formatting, i used spaces but they all get wiped out by the browser. ANyways, the point is, if you use a static variable inside your class definition, it will be visible/accessible by all instances of Foo.
 

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Giscardo,

Thanks for the prompt reply. That's one of the things that I was trying to do, so I have a couple of things that I'm not sure about, and if you could explain 'em, I'd be grateful!

a.) for some reason, VB doesn't seem to like me trying to declare a collection as "static"; I get "Static is not valid on member variable declaration." Would declaring the collection as "shared" allow me to do essentially the same thing (that is, only have one copy laying around, as opposed to having a separate collection for every instance of the class?)

b.) The constructor part is what kills me here. The VB equivilent of the constructor is sub New(), correct? If I want to add an item to a collection, I understand I have to use collectionname.add(item). However, I'm not quite sure how to reference the item that I've just created within the class. I'll be creating the item in the main module (this is a console program), and I understand how to reference it there. But I'm not sure how I can reference it within the class definition itself. I guess what I'm asking is, in the code below, what replaces "XXX"?

Thanks again!

public class Foo

private shared col as microsoft.visualbasic.collection

sub new()
col.add(XXX)
end sub

End Class
 

Giscardo

Senior member
May 31, 2000
724
0
0
a) I've never coded in VB.NET, but yes, you're right, "shared" is the VB equivalent of the "static" keyword.

b) in C#, you would replace "XXX" with "this". I think you have to use "Me" for VB:

sub new()
col.add(Me)
end sub

try that and see if it works.
 

rsd

Platinum Member
Dec 30, 2003
2,293
0
76
You may want to pass your constructor your collection in case you are making separate classes? E.g. you have a Widgets class, and then on a separate page or another class instantiate your collection.
 

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Giscardo,

That's exactly what it was. I should have known that. This is what I get for staring at a computer screen for more than 8 hours. (Thankfully not doing VB all day) Thanks!

rsd,

Thanks for the suggestion as well.
 

rsd

Platinum Member
Dec 30, 2003
2,293
0
76
I do mostly VB.NET myself now, but before this had never done any VB (mostly C/C++). But after a little while you get the hang of it, and if anything C# looks so strange now

Feel free to ask me any questions by the way.
 

Giscardo

Senior member
May 31, 2000
724
0
0
rsd, if he does the static method like in the code sample I posted, the collection can be accessed with a public static (or public shared) member of his class, anywhere in his code. It depends on what he's trying to accomplish though, either way could work.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |