select case Type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PJ6

    #1

    select case Type

    Select Case o.GetType
    Case = GetType(SomeRan domTypeName)
    '...
    End Select

    Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
    long list?

    Paul


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: select case Type

    Paul,
    | Why doesn't this work?
    Case requires "elementary data type". Type is not a "elementary ", but rather
    it is a class.

    http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx


    | Can I make it work or am I stuck with ElseIf for a
    | long list?
    When checking for specific types, I would use a ElseIf as it ensures that
    derived types are handled correctly.

    You can get a select case to work, by using the type names, however then you
    are limited to the type names & if you ever change the name of the type your
    select case will break.

    Something like:
    | Select Case o.GetType.ToStr ing
    | Case "SomeNameSpace. SomeRandomTypeN ame"
    | '...
    | End Select


    --
    Hope this helps
    Jay B. Harlow [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "PJ6" <noone@nowhere. net> wrote in message
    news:ulePazmgGH A.1320@TK2MSFTN GP04.phx.gbl...
    | Select Case o.GetType
    | Case = GetType(SomeRan domTypeName)
    | '...
    | End Select
    |
    | Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
    | long list?
    |
    | Paul
    |
    |


    Comment

    • PJ6

      #3
      Re: select case Type

      OK, but I don't see why they have that restriction, why the "Is" operator
      here cannot function the same as the "Is" operator elsewhere. It would seem
      to me an arbitrary and rather unnecessary break in convention.

      Paul

      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
      message news:uyRtuLngGH A.3456@TK2MSFTN GP05.phx.gbl...[color=blue]
      > Paul,
      > | Why doesn't this work?
      > Case requires "elementary data type". Type is not a "elementary ", but
      > rather
      > it is a class.
      >
      > http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx
      >
      >
      > | Can I make it work or am I stuck with ElseIf for a
      > | long list?
      > When checking for specific types, I would use a ElseIf as it ensures that
      > derived types are handled correctly.
      >
      > You can get a select case to work, by using the type names, however then
      > you
      > are limited to the type names & if you ever change the name of the type
      > your
      > select case will break.
      >
      > Something like:
      > | Select Case o.GetType.ToStr ing
      > | Case "SomeNameSpace. SomeRandomTypeN ame"
      > | '...
      > | End Select
      >
      >
      > --
      > Hope this helps
      > Jay B. Harlow [MVP - Outlook]
      > .NET Application Architect, Enthusiast, & Evangelist
      > T.S. Bradley - http://www.tsbradley.net
      >
      >
      > "PJ6" <noone@nowhere. net> wrote in message
      > news:ulePazmgGH A.1320@TK2MSFTN GP04.phx.gbl...
      > | Select Case o.GetType
      > | Case = GetType(SomeRan domTypeName)
      > | '...
      > | End Select
      > |
      > | Why doesn't this work? Can I make it work or am I stuck with ElseIf for
      > a
      > | long list?
      > |
      > | Paul
      > |
      > |
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: select case Type

        PJ6
        There is no "Is" operator in a Select Case per se!

        Notice that in a Select Case that "Is" is used to introduce a comparison
        operator, such as <, >, <=, >=, =, or <>. That is "[Is] comparisonopera tor".

        IMHO: using "Case Is < 10" reads better then "Case < 10", however that is
        largely personal preference.

        Its similar to the "Is" keyword in the "TypeOf Is" operator.
        --
        Hope this helps
        Jay B. Harlow [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        "PJ6" <noone@nowhere. net> wrote in message
        news:OhJm0kngGH A.1208@TK2MSFTN GP02.phx.gbl...
        | OK, but I don't see why they have that restriction, why the "Is" operator
        | here cannot function the same as the "Is" operator elsewhere. It would
        seem
        | to me an arbitrary and rather unnecessary break in convention.
        |
        | Paul
        |
        | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
        | message news:uyRtuLngGH A.3456@TK2MSFTN GP05.phx.gbl...
        | > Paul,
        | > | Why doesn't this work?
        | > Case requires "elementary data type". Type is not a "elementary ", but
        | > rather
        | > it is a class.
        | >
        | > http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx
        | >
        | >
        | > | Can I make it work or am I stuck with ElseIf for a
        | > | long list?
        | > When checking for specific types, I would use a ElseIf as it ensures
        that
        | > derived types are handled correctly.
        | >
        | > You can get a select case to work, by using the type names, however then
        | > you
        | > are limited to the type names & if you ever change the name of the type
        | > your
        | > select case will break.
        | >
        | > Something like:
        | > | Select Case o.GetType.ToStr ing
        | > | Case "SomeNameSpace. SomeRandomTypeN ame"
        | > | '...
        | > | End Select
        | >
        | >
        | > --
        | > Hope this helps
        | > Jay B. Harlow [MVP - Outlook]
        | > .NET Application Architect, Enthusiast, & Evangelist
        | > T.S. Bradley - http://www.tsbradley.net
        | >
        | >
        | > "PJ6" <noone@nowhere. net> wrote in message
        | > news:ulePazmgGH A.1320@TK2MSFTN GP04.phx.gbl...
        | > | Select Case o.GetType
        | > | Case = GetType(SomeRan domTypeName)
        | > | '...
        | > | End Select
        | > |
        | > | Why doesn't this work? Can I make it work or am I stuck with ElseIf
        for
        | > a
        | > | long list?
        | > |
        | > | Paul
        | > |
        | > |
        | >
        | >
        |
        |


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: select case Type

          "PJ6" <noone@nowhere. net> schrieb:[color=blue]
          > Select Case o.GetType
          > Case = GetType(SomeRan domTypeName)
          > '...
          > End Select
          >
          > Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
          > long list?[/color]


          \\\
          Dim t As Type = o.GetType()
          Select Case True
          Case t Is GetType(SomeRan domTypeName)
          ...
          Case t Is GetType(...)
          ...
          ...
          End Select
          ///

          - or -

          \\\
          Select Case True
          Case TypeOf t Is SomeRandomType
          ...
          Case TypeOf t Is ...
          ...
          ...
          End Select
          ///

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Roland

            #6
            Re: select case Type

            I am using

            Select case o.getType.name
            Case (SomerandomType Name)
            Case ...
            End Select

            works as a charm since it is "o.getType.name " is a string
            Roland

            "PJ6" <noone@nowhere. net> wrote in message
            news:ulePazmgGH A.1320@TK2MSFTN GP04.phx.gbl...[color=blue]
            > Select Case o.GetType
            > Case = GetType(SomeRan domTypeName)
            > '...
            > End Select
            >
            > Why doesn't this work? Can I make it work or am I stuck with ElseIf for a
            > long list?
            >
            > Paul
            >[/color]


            Comment

            Working...