Is there a way to set a variable in VB.NET in an IF statement. I would like to do the following:
If (TypeOf sender Is TabControl And TypeOf e.Control Is TabPage) Then 'tab control is adding a tab page
AddControl(CType(e.Control, TabPage))
ElseIf (TypeOf sender Is TabPage) Then 'tab page is adding some control
AddControl(CType(sender, TabPage), e.Control)
ElseIf (tabPageFound = FindTabPageOf(sender)) IsNot Nothing Then
tabPageFound.SomeAction()
End if
Is there some way I can get the last “ElseIf” to work? I know I can re-write it to get it to work but is there some syntax that will make it work the way it is?
I don’t use VB.NET (so mind the syntax errors). However, note that each “else if” can be trivially rewritten as an “else” with a nested “if”. Many languages, such as those based on the C-syntax, do not have a compound “else if” statement but have nicer support for “hanging if” statements.
In this case it might be rewritten as:
Just something to consider.
For the sake of completeness this shows removal of all
ElseIfusage and the resulting nesting: