I'm making an Add-in that needs to loop through all the codeElements and
their children and add them to a listview. I have a big if
typeof(codeElement) is blah then ... elseif typeof(codeElement) is blah
then... conditional. I'm having a problem on the elseif typeof(codeElement)
is CodeFunction then block. It falls into the if block as if the condition
is true, but if I set a break point on the first line after the elseif ...
CodeFunction Then line and check the condition, it's false. If I check
TypeOf(codeElement) Is CodeElement, I get false as well. If I check
codeElement.Name, I get the name of the function, so it SHOULD be a
CodeFunction, and it DEFINATELY should be a CodeElement, since that's what
the variable codeElement was declared as. Here's my code:
Private Sub AddCodeElement(ByVal baseNode As TreeNode, ByVal codeElement
As CodeElement)
Try
Dim elementNode As TreeNode
Dim childElement As CodeElement
If TypeOf (codeElement) Is CodeNamespace Then
elementNode = baseNode.Nodes.Add("Namespace: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeNamespace).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeStruct Then
elementNode = baseNode.Nodes.Add("Struct: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeStruct).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeInterface Then
elementNode = baseNode.Nodes.Add("Interface: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeInterface).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeClass Then
elementNode = baseNode.Nodes.Add("Class: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeClass).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeEnum Then
elementNode = baseNode.Nodes.Add("Enum: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeEnum).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeVariable Then
elementNode = baseNode.Nodes.Add("Variable: " &
codeElement.Name)
' CodeVariable doesn't have a members collection.
ElseIf TypeOf (codeElement) Is CodeDelegate Then
elementNode = baseNode.Nodes.Add("Delegate: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeDelegate).Members
AddCodeElement(elementNode, childElement)
Next
ElseIf TypeOf (codeElement) Is CodeProperty Then
elementNode = baseNode.Nodes.Add("Property: " &
codeElement.Name)
' CodePropety doesn't have a members collection.
ElseIf TypeOf (codeElement) Is CodeFunction Then
elementNode = baseNode.Nodes.Add("Function: " &
codeElement.Name)
For Each childElement In CType(codeElement,
CodeFunction).Parameters
AddCodeElement(elementNode, childElement)
Next
End If
Catch ex As Exception
MsgBox(String.Format("The following error occurred while " & _
"adding a codeElement node to the tree.{0}{0}{1}", _
ControlChars.CrLf, ex.Message), _
MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
If I set a break point on the
elementNode = baseNode.Nodes.Add("Function: " & codeElement.Name)
line and when it breaks check the condition TypeOf (codeElement) Is
CodeFunction, it will be False even though it must be true for it to have
gotten to that line. If I continue, it hits an exception on the next line
where I try to cast codeElement as a CodeFunction.
Help!!! :)
-----
Lee
Carlos J. Quintero [MVP] - 31 May 2004 14:24 GMT
Hi,
I have seen the effect that you are describing too. I don?t know why, but
you can not trust the debugger (or at least the Immediate Window) to perform
tests of the kind
If TypeOf(x) Is CodeXXXX
The compiled code works fine, however. I think is something related with COM
interop, since TypeOf (codeElement) returns a __ComObject which is not
directly a CodeClass, etc, buy a wrapper and theerfore the debugger returns
False for those kinds of checks.

Signature
Carlos J. Quintero (Visual Developer - .NET MVP)
FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)
> I'm making an Add-in that needs to loop through all the codeElements and
> their children and add them to a listview. I have a big if
[quoted text clipped - 90 lines]
> -----
> Lee
Lee - 31 May 2004 17:43 GMT
Thanks for your reply. The problem I was having went beyond the debuger
being quirky though. In the actual running code it was throwing an
exception on the line where I tried to CType(codeElement, CodeFunction) even
though it was inside the block for the ElseIf TypeOf(codeElement) Is
CodeFunction Then ... End If.
I worked around the problem by moving the ElseIf for the CodeFunction up one
or two ElseIf's so it wasn't the last one. Sounds like I might have hit a
combination of a couple different bugs. :)
Does anyone out there from Microsoft have any comments they can add to this?
Is this a known issue?
-----
Lee
> Hi,
>
[quoted text clipped - 106 lines]
> > -----
> > Lee