Napivo1972 Posted April 14, 2005 Posted April 14, 2005 I can hear some of you already laugh for this certainly is a Question in the easy cathegory. To cast an onbect to a known objecttype I just do: Dim fntfont as font = Directcast(Myobject,font) That�s easy enough. Now I have an object that can contain Fonts and Pens In vb6 you would use tyename to find the type of object and solve it that way but how do we do this now (I mean without the VB namespace) I would appreciate an example in VB if possible Quote
Administrators PlausiblyDamp Posted April 14, 2005 Administrators Posted April 14, 2005 You could always use the TypeOf operator e.g. Dim o As Object If TypeOf o Is Pen Then 'o is a Pen handle correctly here End If however you may be better off handling the whole idea differently - is there a particular reason why a single variable could contain two different classes of objects? Often it would be much better to use a different variable for each object type. This way you can strongly type things and let the compiler identify plenty sources of error. Any chance you could post more of the code to see if it could be improved somehow? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted April 14, 2005 Posted April 14, 2005 This documentation on the MSDN might be what you are looking for. The Object.GetType method. Also, PD has some good points. Quote
Napivo1972 Posted April 14, 2005 Author Posted April 14, 2005 I am glad you asked PD.For I know what I have done is pretty dirty but I don�t know how to solve it nicely. I would post more code but it would be too complex. What I am doing is writing a program similar to the explorer window. Only this is will be my personal Disk analyzer. At start it builds a tree of objects for each directory on a drive while calculating total drive consumption of all subdirectories. This takes a while of course but later on. Let�s call it DiectoryTreeEntries Later on I use this information to make various calculations and fill out a Listview. So far so good (Said the man that jumped from the 15th when he reached the 5th) but then it gets ugly. I used the TAG property of the listviewItems to store a pointer to the respective DiectoryTreeEntries. So that if I double-click one of the ListViewNodes I can automaticly jump to that node and refill my Listview with the data of the new current directory. (Ug hope that makes sense) Now of course there are not only Directories on a drive but also files :) Those are not stored in DirectoryTreeEntries but gathered for the current directory when I enter it. This is to save Memory as I don�t want to store all FileInformation of my drive in memory. The fileInfois also linked to the TAG property of the ListViewNode. I know this is BAD BAD practice but I didn�t know better. As I was typing this I realized something�.. can�t I inherit the ListViewItem Class and add a few properties� Of course I can I�ll play a bit with it tonight and let you know how this turns out. Quote
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.