rifter1818 Posted February 14, 2004 Posted February 14, 2004 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. Quote
Rodenberg Posted February 15, 2004 Posted February 15, 2004 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 shouldn't be too drastically different in VB.Net, hope this helps. Quote
samsmithnz Posted February 15, 2004 Posted February 15, 2004 Hey this is an interesting bit of code. I didn't relieze you could do this in .NET. Is this the VB.NET equivalent??? For Each o As Object In meshlist If o.GetType Is o.clsItem Then 'Do stuff End If Next Quote Thanks Sam http://www.samsmith.co.nz
Administrators PlausiblyDamp Posted February 15, 2004 Administrators Posted February 15, 2004 Sam - spot on as long as your running VS.net 2003 / Framework 1.1 in version 1.0 it would have to be dim o as object For Each o In meshlist If o.GetType Is o.clsItem Then 'Do stuff End If Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
samsmithnz Posted February 15, 2004 Posted February 15, 2004 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 doesn't work either... For Each o As Object In meshlist If o.GetType Is clsItem Then 'Do stuff End If Next Quote Thanks Sam http://www.samsmith.co.nz
Administrators PlausiblyDamp Posted February 15, 2004 Administrators Posted February 15, 2004 try For Each o As Object In meshlist If typeof o Is clsItem Then 'Do stuff End If Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rifter1818 Posted February 15, 2004 Author Posted February 15, 2004 Thanks for your help How would i fill the array list with the object instances? would it be dim i as integer redim MeshList(whatever) for i = 0 to whatever MeshList(i) = new customvertex.something(elements) next Quote
samsmithnz Posted February 15, 2004 Posted February 15, 2004 Its an ArrayList, not a Array List, and we're trying to identify objects in the list, no add items to it. which would be: MeshList.add(customvertex.something(elements)) Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted February 15, 2004 Posted February 15, 2004 try 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 doesn't actually work :)) Thanks PlausiblyDamp Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted February 15, 2004 Posted February 15, 2004 How do I convert this into a Case Statement. I tryed: Select Case o Case TypeOf o Is clsItem End Select But it doesn't like it... Quote Thanks Sam http://www.samsmith.co.nz
Administrators PlausiblyDamp Posted February 15, 2004 Administrators Posted February 15, 2004 Can't think of an easy way to do that - I suppose you could use something like 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
samsmithnz Posted February 15, 2004 Posted February 15, 2004 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 doesn't look as good as a select... :( Quote Thanks Sam http://www.samsmith.co.nz
rifter1818 Posted February 16, 2004 Author Posted February 16, 2004 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 Quote
samsmithnz Posted February 17, 2004 Posted February 17, 2004 While rifter1818 seems to be finished with the thread, I'm going to bump it up to try and resolve my error, rather than start with a new thread altogether.. I'm 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. I'd 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: 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? I've trying scouring the net, but the keywords I have to search with are too generic... Quote Thanks Sam http://www.samsmith.co.nz
Rodenberg Posted February 17, 2004 Posted February 17, 2004 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 don't 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! Quote
samsmithnz Posted February 17, 2004 Posted February 17, 2004 So whats the equivalent to 'interfaces' in VB? meanwhile, I've found another function that does what I want (TypeName ): 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... Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.