Hopefully This one will actually be easy...

rifter1818

Well-known member
Joined
Sep 11, 2003
Messages
255
Location
A cold dark place
Is there any way (im using VB.net) for me to create an array of (i guess objects initially) and then latter deffine them,.. The use ive run into so far is creating an animated Mesh class i want to keep an array for each keyframe of verticies (i know it costs memory, but itll increase framerate) but the thing is i dont want to limit my class to just one type of mesh so i need to in the initialize function or new sub depending on how im fealing when you all come up for an answer here define this array as a specific vertexformat.... say as me.vertexbuffer.description.vertexformat or some such thing. Thanks in advance for your help.
 
Not sure about VB.Net specific syntax, but you should store each object in an array (or arraylist) of generic objects and determine the type as you iterate through the list.

ArrayList meshList = new ArrayList();
[.........FILL ARRAYLIST WITH VARIOUS OBJECT INSTANCES............]

when you are ready to use the objects you do the following:
[C#]
foreach(object o in meshList){
if(object is <ObjectType>)
///IF OBJECT IS THE TYPE YOU NEED, DO SOMETHING WITH IT
}

This is how I would do it in C#, the equivalent shouldnt be too drastically different in VB.Net, hope this helps.
 
Hey this is an interesting bit of code. I didnt relieze you could do this in .NET.

Is this the VB.NET equivalent???

Code:
        For Each o As Object In meshlist
            If o.GetType Is o.clsItem Then
                 Do stuff
            End If
        Next
 
Sam - spot on as long as your running VS.net 2003 / Framework 1.1 in version 1.0 it would have to be
Code:
dim o as object
For Each o In meshlist
            If o.GetType Is o.clsItem Then
                 Do stuff
            End If
        Next
 
PlausiblyDamp - neither of these actually work (I have VS2003 Enterprise)...

Whats wrong with them, I assume they must be close...

I was thinking it would be something like this... but this doesnt work either...
Code:
        For Each o As Object In meshlist
            If o.GetType Is clsItem Then
                 Do stuff
            End If
        Next
 
Thanks for your help

How would i fill the array list with the object instances? would it be
Code:
dim i as integer
redim MeshList(whatever)
for i = 0 to whatever
MeshList(i) = new customvertex.something(elements)
next
 
Its an ArrayList, not a Array List, and were trying to identify objects in the list, no add items to it. which would be:
Code:
MeshList.add(customvertex.something(elements))
 
Originally posted by PlausiblyDamp
try
Code:
For Each o As Object In meshlist
            If typeof o Is clsItem Then
                 Do stuff
            End If
        Next

Sorry, I had a typo in my code, this is definetly the way to do it, in 2002 and 2003 (my way doesnt actually work :))

Thanks PlausiblyDamp
 
How do I convert this into a Case Statement. I tryed:
Code:
                Select Case o
                    Case TypeOf o Is clsItem
                End Select

But it doesnt like it...
 
Cant think of an easy way to do that - I suppose you could use something like
Code:
select case o.GetType().ToString()
     case "System.Integer"
      etc

but that looks pretty horrible - what kind of classes are you looking at using and needing to select between - there may be a better alternative.
 
I have a number of custom classes, clsItem, clsfacility, clsCraft, and I was thinking it would be useful to combine them into one object (in this case an arraylist). I guess I could just use a giant IF statement, but it doesnt look as good as a select... :(
 
Thanks for your help

What i was looking for i guess was how to add items to an array list. I guess that about raps it up for my D3D Class just lighting to go so if anyof you are so kind im sure ill be adding yet more posts to the directx section, hopefully a few of them will be answered.. Thanks for all your help folks
 
While rifter1818 seems to be finished with the thread, Im going to bump it up to try and resolve my error, rather than start with a new thread altogether..

Im trying to create a generic collection of objects. All of them will have certain generic properties (such as a unique Key and Name), and several class specific properties. Id like to add them all in a collection/arraylist/etc.

So far we discussed adding a custom class to a arraylist collection, then later identifing what sort of class it was using:
Code:
        For Each o As Object In objMyCollection
            If TypeOf o Is clsItem Then
                 Do stuff
            ElseIf TypeOf o Is clsOtherItem Then
                 Do other stuff
            End If
        Next

The problem is that the first comparison in the IF is always true (even when there are all sorts of different objects in the arraylist). Can anyone give me anymore pointers, or articles, or examples, or anything really that could help me? Ive trying scouring the net, but the keywords I have to search with are too generic...
 
Use interfaces for the objects you would like to group together, make two checks. For example,
****************************************
interface ISimilarObject{

public int PrimaryKey{
get;
set;
}
public string Name{
get;
set;
}

}

****NOW IMPLEMENT THIS INTERFACE WITH THE OBJECTS YOU WANT TO GROUP TOGETHER****
public class ObjectOne: ISimilarObject{
private string name;
private int primaryKey;
public Object(){}
///other methods, properties, etc.///

****IMPLEMENT METHODS/PROPERTIES/ETC. REQUIRED BY THE INTERFACE*********
public int PrimaryKey{
get{ return this.primaryKey; }
set{ this.primaryKey = value; }
}
public string Name{
get{ return this.name; }
set{ this.name = value; }
}

}

public class ObjectTwo: ISimilarObject{
private string name;
private int primaryKey;
public Object(){}
///other methods, properties, etc.///

****IMPLEMENT METHODS/PROPERTIES/ETC. REQUIRED BY THE INTERFACE*********
public int PrimaryKey{
get{ return this.primaryKey; }
set{ this.primaryKey = value; }
}
public string Name{
get{ return this.name; }
set{ this.name = value; }
}

}

******NOW CHECk FOR THE INTERFACE INSTEAD OF AN ACTUAL OBJECT TYPE********

foreach(ISimilarObject similar in collection){

}

I dont know the VB specific syntax, but this should be enough. You will need to implement interfaces in order to do what you would like, otherwise you will waste a lot of time and sacrifice performance.

Good luck!
 
So whats the equivalent to interfaces in VB?

meanwhile, Ive found another function that does what I want (TypeName ):
Code:
        For Each o As Object In objCollection
            Select Case TypeName(o)
                Case "clsItem"
                    MsgBox("Here")
                Case "clsItem2"
                    MsgBox("There")
            End Select
        Next

Now I want to know the advantages and disadvantages to using the interface rather than the object type to loop through my collection...
 
Back
Top