Hi SRSDS,
In VB if you know for sure what the object is DiredtCast is the fastest, but casting is not the point.
In case you did not pick up on what gile and DinningPhilospher were pointing out, they were trying to show you how to avoid extra calls to GetObject.
I am repeating what gile and DiningPhilosopher have already pointed out but you have an Objectid and your intrested in the type of DbObject it points to in memory. From your original example it looks like that if it is the type your intrested in then you would like to use the object.
They mentioned TryCast because it is a clean way to test and to get the object.
The reason you are having problems getting the first example to work is there are 2 different GetTypes.
The GetType operator and the GetType method of the type Type.
The operator GetType takes a argument that is the Type name, and the GetType method is used with a object variable.
To get it to compile and look at it a little closer
Dim AttRef As AttributeReference
For Each SubEntId As ObjectId In btr
If trans.GetObject(SubEntId, OpenMode.ForRead, False, False).GetType().Equals(GetType(AttributeReference)) Then
AttRef = trans.GetObject(SubEntId, OpenMode.ForRead, False, True)
End If
NextI think the main point others were trying to make is notice how the result of GetObject is thrown away in testing for the type.
Perhaps he meant something along the lines of
Dim AttRef As AttributeReference
For Each SubEntId As ObjectId In btr
Dim dbo As DBObject = trans.GetObject(SubEntId, OpenMode.ForRead, False, True)
If dbo.GetType().Equals(GetType(AttributeReference)) Then
AttRef = dbo
End If
Next
That still does another check, but not as costly as calling GetObject 2 times for every object you want to know the type from its ObjectId.
Or even
Dim AttRef As AttributeReference
For Each SubEntId As ObjectId In btr
Dim dbo As DBObject = trans.GetObject(SubEntId, OpenMode.ForRead, False, True)
If TypeOf dbo Is AttributeReference Then
AttRef = dbo
End If
Next Now knock it all out at once.
Dim AttRef As AttributeReference
For Each SubEntId As ObjectId In btr
AttRef = TryCast(trans.GetObject(SubEntId, OpenMode.ForRead, False, True), AttributeReference)
If Not AttRef Is Nothing Then
''''''
End If
Next That was the point I think they were trying to make and were not trying to argue if TryCast is faster than CType which is still not as fast as DirectCast but TryCast it is much cleaner a quicker if you do not know the type. In a situation where you would use Ctype there is no need for any of this because already know type your dealing with.
No matter how you look it, it is still VB so it sucks but the TryCast is a little less sucky.
Since the post is this long I might as well finish it with a idiotic story to bring home the point.
There are 2 parcel companies(UPS and FedEx are examples) called Ctype ans TryCast delivery.(I still do not understand how Ctype got involved but going with it.) Now Ctype is faster than TryCast but TryCast is still fast.(DirectCast is still the fastest). So one day living in America you and 2 buddies need packages from Europe and you tell the Ctype guy I think i have this package in Europe could you go check and get it? So he flies to Europe comes back and is like "great news your package was there". Then you stand there for a moment waiting for the package and you ask "well can i have it?" and he is like "I do not have it I just checked for you." Now your standing there in a awkward moment staring at the guy thinking there is no way this tard flew all the way to Europe to check for a package I needed and flew all the way back to tell me it was there. Then he tells you if the package is not there and I try to get we have to start handling exceptions, and about that time running a little behind is the Trycast guy who your buddies used and tell one buddy your package was not there so I have nothing and the other it was and here is your package. This is really getting stupid.
You can also find your answers @
TheSwamp