Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Greetings:

 

The below code complies w/o error, however, upon running it the above error msg is generated (by second line in below code). Thing is, I'm not sure why as I'm defining the variable as a type that is in the collection. :confused: (The routine is for making all child panels on a main panel invisible upon initial load.)

 

Any and all guidance will be appreciated.

 

Private Sub HideAllPanels(ByVal ThePanel As Panel)

For Each ControlObj As Panel In ThePanel.Controls

If ControlObj.GetType.ToString() = "Panel" Then

ControlObj.Visible = False

If ControlObj.Controls.Count > 0 Then

HideAllPanels(ControlObj)

End If

End If

Next

End Sub

 

Ed in Tampa

  • Administrators
Posted

Try

   Private Sub HideAllPanels(ByVal ThePanel As Panel)
       For Each ControlObj As Control In ThePanel.Controls
           If TypeOf ControlObj Is Panel Then
               ControlObj.Visible = False
               If ControlObj.Controls.Count > 0 Then
                   HideAllPanels(ControlObj)
               End If
           End If
       Next
   End Sub

 

Because the panel can contain other controls as well as panels you need to use as Control in the for ... each loop.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

PlausiblyDamp...

 

I think you almost have it...however, I have the option Strict "On" and therefore need to cast ControlObj as a Panel when sending it to HideAllPanels in the embedded call (code will not compile unless this cast is done...I think). Problem for me is that I'm having trouble in VB.Net getting that syntax correct (I code in C# in my day job).

 

In that language, I'd simply go HideAllPanels((Panel)ControlObj) but that's not working here.

 

Can someone show me how to cast this more general Control obj as a Panel (and I think I'll be on my way)? :D

 

Many thanks!

 

Try

   Private Sub HideAllPanels(ByVal ThePanel As Panel)
       For Each ControlObj As Control In ThePanel.Controls
           If TypeOf ControlObj Is Panel Then
               ControlObj.Visible = False
               If ControlObj.Controls.Count > 0 Then
                   HideAllPanels(ControlObj)
               End If
           End If
       Next
   End Sub

 

Because the panel can contain other controls as well as panels you need to use as Control in the for ... each loop.

Edited by BenRosa
Posted

Ok...casting successfully

 

Ok, after poking around...I found a nice little function called CType which did the trick for this casting task.

 

Here's the final version...(with chgs in bold):

 

Private Sub HideAllPanels(ByVal ThePanel As Panel)

For Each ControlObj As Control In ThePanel.Controls

If TypeOf ControlObj Is Panel Then

ControlObj.Visible = False

If ControlObj.Controls.Count > 0 Then

Dim ChildControlObj As Panel

ChildControlObj = CType(ControlObj, Panel)

HideAllPanels(ChildControlObj)

End If

End If

Next

End Sub

 

I couldn't cast directly, so had to create another var to hold the new Panel obj.

 

Thanks PD! :)

PlausiblyDamp...

 

I think you almost have it...however, I have the option Strict "On" and therefore need to cast ControlObj as a Panel when sending it to HideAllPanels in the embedded call (code will not compile unless this cast is done...I think). Problem for me is that I'm having trouble in VB.Net getting that syntax correct (I code in C# in my day job).

 

In that language, I'd simply go HideAllPanels((Panel)ControlObj) but that's not working here.

 

Can someone show me how to cast this more general Control obj as a Panel (and I think I'll be on my way)? :D

 

Many thanks!

  • Administrators
Posted
   Private Sub HideAllPanels(ByVal ThePanel As Panel)
       For Each ControlObj As Control In ThePanel.Controls
           If TypeOf ControlObj Is Panel Then
               ControlObj.Visible = False
               If ControlObj.Controls.Count > 0 Then
                   HideAllPanels(DirectCast(ControlObj, Panel))
               End If
           End If
       Next
   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted
Not that you can't look this up in MSDN, but CType will perform a conversion if one is found, where as DirectCast will only perform a cast if the object being casted is an appropriate type. (i.e. an int can be CTyped to a string, but not DirectCasted. A Form can be DirectCasted to a control.)
[sIGPIC]e[/sIGPIC]
Posted
"CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type."
Posted (edited)
DirectCast will only perform a cast if the object being casted is an appropriate type...

 

True...and I'm thinking I'll be okay using DirectCast here (verses CType) as I am checking beforehand, if I've got a Panel control object. Besides, I'm all for the performance gain realized from DirectCast.

 

Great responses.

 

Thank you all. :)

Edited by BenRosa
  • Leaders
Posted

If you check the type before hand, DirectCast should always succeed. But just a note, the performance gain of CType is very minimal in most circumstances. You would have to do alot of casting to see any speed boost.

 

As a general practice, though, I do avoid CType because there is a little appearent ambiguity to it. When compiled, CType will be converted to another cast that you can perform on your own anyways. It could be a DirectCast, a conversion between numeric types (CInt, CLong, CDbl), to/from a string (ToString, type.Parse). I think it's just nice to be explicit.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...