Results 1 to 6 of 6

Thread: [RESOLVED, can't be done] Select Case with reference types

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved [RESOLVED, can't be done] Select Case with reference types

    I was under the impression that it would be possible to do a select case on reference types themselves (not just their properties.) The comparator for reference types is 'is', not '=', but Case Is GetType(Splitter) automatically inserts the '='. It doesn't like Case Is Is GetType(Splitter) either.

    Any ideas on how to do this, preferably with select case, as I will be adding more than just two types later on.

    vb Code:
    1. For Each current As Control In form.Controls
    2.     Select Case current.GetType
    3.         Case Is = GetType(Splitter)
    4.             'some code
    5.         Case Is = GetType(ColumnHeader)
    6.             'more code
    7.         Case Else
    8.             'nothing
    9.     End Select
    10. Next
    Last edited by agent; Sep 6th, 2007 at 09:28 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Select Case with reference types

    Select Case can only be use where the '=' opertaor is defined for the type of the expression and the type of the cases. You can do this:
    vb.net Code:
    1. Select Case True
    2.     Case TypeOf current Is Splitter
    3.  
    4.     Case TyoeOf current is ColumnHeader
    5.  
    6.     Case Else
    7.  
    8. End Select
    but that gains you nothing over using If...ElseIf statements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select Case with reference types

    Code:
    Select Case current.GetType().ToString()
      Case "WhateverNamespaceName.Splitter"
        ' ...
      Case "WhateverNamespaceName.ColumnHeader"
        ' ...
    End Select
    However, I recommend that you use the If...ElseIf method instead. Why? Refactoring won't change class names in strings, and you'll end up with code that builds but doesn't work.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Select Case with reference types

    Whoops, ColumnHeader isn't derrived from Control.

    This is what I'm using:
    vb Code:
    1. For Each current As Control In form.Controls
    2.  
    3.                 If current.GetType Is GetType(Splitter) Then
    4.                     'Some code
    5.                 ElseIf current.GetType Is GetType(ListView) Then
    6.                     Dim tmp As ListView = current
    7.  
    8.                     For Each clm As ColumnHeader In tmp.Columns
    9.                         'Some more code
    10.                     Next
    11.                 End If
    12.  
    13.             Next

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED, can't be done] Select Case with reference types

    You shouldn't be calling GetType more than once:
    vb.net Code:
    1. Dim currentType As Type
    2.  
    3. For Each current As Control In form.Controls
    4.     currentType = current.GetType()
    5.  
    6.     If currentType Is GetType(Splitter) Then
    7.  
    8.     ElseIf currentType Is GetType(ColumnHeader) Then
    9.  
    10.     End If
    11. Next current
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: [RESOLVED, can't be done] Select Case with reference types

    Thanks... didn't think about that one... I'll fix it in a few minutes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width