I'm dumb... Visual Basic Help...

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Disclaimer: object oriented programming scares me.

I've got a number of objects in a collection. However, I can't access their properties unless I loop through the collection in the following way:

for each inst as obj in collection
inst.property = 5
next

I'd like to be able to access objects based on their index number, such as in the following:

for each inst as obj in collection
if inst.property = 5 then
inst(12).property = "success"
end if
next

Is this possible?

THANKS!
 

Specialk92282

Member
Nov 15, 2004
143
0
0
You're probably looking for something more like a list.

That is a List(Of <object type> )

this way you can traverse the list with indexes and still have access to each objects properties without using a DirectCast()
 

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Thanks. I appreciate the pointer in the right direction. I guess the collections approach just doesn't allow this at all?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I'm not a VB person, but in any other language, what you're trying to do seems simple enough

This is logically what you want to do, right?

for(object in collection){
if (object.property == 5) collection[12].property = true;
}

It looks really straightfoward to me.
 

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Yeah, that's the thing... it seems so darn easy.

I just noticed that somehow -- and this is where object oriented programing scares me -- that I've been able to write a few hundred lines of code refering to variables that exist for each object but aren't explicitly properties. (e.g. I've defined them as variables (with a "public" statement) but not ("public property"). I assume that this has something to do with my issue?

This is VB.Net by the way...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: AtlantaBob
Yeah, that's the thing... it seems so darn easy.

I just noticed that somehow -- and this is where object oriented programing scares me -- that I've been able to write a few hundred lines of code refering to variables that exist for each object but aren't explicitly properties. (e.g. I've defined them as variables (with a "public" statement) but not ("public property"). I assume that this has something to do with my issue?

This is VB.Net by the way...

You're not allowed to do this in a lot of languages. I couldn't tell you exactly whether or not it's a problem because Java and C++ and C# wont let you do it, so I've never tried.
 

AtlantaBob

Golden Member
Jun 16, 2004
1,034
0
0
Thanks for the info, notfred.

Just for those who are reading this, after converting some of these variables to properties, I'm now able to access them in the way that I want -- well, I am, with a simple boolean test variable. We'll see how it works when it comes to arrays.

Interestingly, though, VB doesn't show the properties in the autocomplete as it normally would.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
This works for me. But no, it won't autocomplete the property 'prop' after x(2), probably because it would be too slow to do that. You can still address it however. In my case, x(2) always returns 6, which makes perfect sense.

Edit: removed commented-out line