How to check wether CType() will throw InvalidCastException?

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

    How to check wether CType() will throw InvalidCastException?

    Hello -

    I was wondering that the "cleanest" way is to determine whether a
    CType() will throw an InvalidCastExce ption? I have data I receive as
    an Object and I want to convert it to a String whenever possible using
    CType(lObjectDu mmy, String) otherwise I will just ignore it.

    I could put a Try/Catch As System.InvalidC astException around it but I
    was wondering if that is the best solution. I once heard that
    exceptions should not be used as a tool but rather as a means to
    intercept problems during run-time.

    Thanks,
    Joe
  • Tom Shelton

    #2
    Re: How to check wether CType() will throw InvalidCastExce ption?

    On 2008-11-06, Joe HM <unixverse@yaho o.comwrote:
    Hello -
    >
    I was wondering that the "cleanest" way is to determine whether a
    CType() will throw an InvalidCastExce ption? I have data I receive as
    an Object and I want to convert it to a String whenever possible using
    CType(lObjectDu mmy, String) otherwise I will just ignore it.
    >
    I could put a Try/Catch As System.InvalidC astException around it but I
    was wondering if that is the best solution. I once heard that
    exceptions should not be used as a tool but rather as a means to
    intercept problems during run-time.
    >
    Thanks,
    Joe
    Dim o As SomeObject = TryCast(obj, SomeObject)
    If Not o Is Nothing Then
    ' Do Cool Stuff
    End If

    --
    Tom Shelton

    Comment

    • Joe HM

      #3
      Re: How to check wether CType() will throw InvalidCastExce ption?

      On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
      wrote:
      On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
      >
      Hello -
      >
      I was wondering that the "cleanest" way is to determine whether a
      CType() will throw an InvalidCastExce ption?  I have data I receive as
      an Object and I want to convert it to a String whenever possible using
      CType(lObjectDu mmy, String) otherwise I will just ignore it.
      >
      I could put a Try/Catch As System.InvalidC astException around it but I
      was wondering if that is the best solution.  I once heard that
      exceptions should not be used as a tool but rather as a means to
      intercept problems during run-time.
      >
      Thanks,
      Joe
      >
      Dim o As SomeObject = TryCast(obj, SomeObject)
      If Not o Is Nothing Then
              ' Do Cool Stuff
      End If
      >
      --
      Tom Shelton
      Hello -

      Sorry ... I did not specify that I am using .NET 2003 and I just
      checked but there (unfortunately) is no TryCast.

      Thanks,
      Joe

      Comment

      • kimiraikkonen

        #4
        Re: How to check wether CType() will throw InvalidCastExce ption?

        On Nov 6, 7:07 pm, Joe HM <unixve...@yaho o.comwrote:
        On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
        wrote:
        >
        >
        >
        >
        >
        On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
        >
        Hello -
        >
        I was wondering that the "cleanest" way is to determine whether a
        CType() will throw an InvalidCastExce ption?  I have data I receive as
        an Object and I want to convert it to a String whenever possible using
        CType(lObjectDu mmy, String) otherwise I will just ignore it.
        >
        I could put a Try/Catch As System.InvalidC astException around it but I
        was wondering if that is the best solution.  I once heard that
        exceptions should not be used as a tool but rather as a means to
        intercept problems during run-time.
        >
        Thanks,
        Joe
        >
        Dim o As SomeObject = TryCast(obj, SomeObject)
        If Not o Is Nothing Then
                ' Do Cool Stuff
        End If
        >
        --
        Tom Shelton
        >
        Hello -
        >
        Sorry ... I did not specify that I am using .NET 2003 and I just
        checked but there (unfortunately) is no TryCast.
        >
        Thanks,
        Joe- Hide quoted text -
        >
        - Show quoted text -
        Hi,
        As you said and as you have v1.1 (i suppose), you can use a Try-Catch
        block and leave Catch block empty. As you know, doing this may be
        harmful in future because you won't be noticed if exception fails in
        Catch block.

        So, in addition to Tom,

        '------------------------
        Try

        Dim o As SomeObject = TryCast(obj, SomeObject)
        'Further processing here whether
        'casting is successful

        Catch
        'Leave empty intentionally

        End Try
        '----------------------

        HTH,

        Onur Guzel

        Comment

        • kimiraikkonen

          #5
          Re: How to check wether CType() will throw InvalidCastExce ption?

          On Nov 6, 8:21 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
          On Nov 6, 7:07 pm, Joe HM <unixve...@yaho o.comwrote:
          >
          >
          >
          >
          >
          On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
          wrote:
          >
          On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
          >
          Hello -
          >
          I was wondering that the "cleanest" way is to determine whether a
          CType() will throw an InvalidCastExce ption?  I have data I receive as
          an Object and I want to convert it to a String whenever possible using
          CType(lObjectDu mmy, String) otherwise I will just ignore it.
          >
          I could put a Try/Catch As System.InvalidC astException around it but I
          was wondering if that is the best solution.  I once heard that
          exceptions should not be used as a tool but rather as a means to
          intercept problems during run-time.
          >
          Thanks,
          Joe
          >
          Dim o As SomeObject = TryCast(obj, SomeObject)
          If Not o Is Nothing Then
                  ' Do Cool Stuff
          End If
          >
          --
          Tom Shelton
          >
          Hello -
          >
          Sorry ... I did not specify that I am using .NET 2003 and I just
          checked but there (unfortunately) is no TryCast.
          >
          Thanks,
          Joe- Hide quoted text -
          >
          - Show quoted text -
          >
          Hi,
          As you said and as you have v1.1 (i suppose), you can use a Try-Catch
          block and leave Catch block empty. As you know, doing this may be
          harmful in future because you won't be noticed if exception fails in
          Catch block.
          >
          So, in addition to Tom,
          >
          '------------------------
          Try
          >
          Dim o As SomeObject = TryCast(obj, SomeObject)
          'Further processing here whether
          'casting is successful
          >
          Catch
          'Leave empty intentionally
          >
          End Try
          '----------------------
          >
          HTH,
          >
          Onur Guzel- Hide quoted text -
          >
          - Show quoted text -
          Opps, correcting, of course change TryCast with Ctype or Directcast as
          follows:

          '------------------------
          Try


          Dim o As SomeObject = DirectCast(obj, SomeObject)
          'Further processing here whether
          'casting is successful


          Catch
          'Leave empty intentionally


          End Try
          '----------------------


          HTH,


          Onur Guzel


          Comment

          • Patrice

            #6
            Re: How to check wether CType() will throw InvalidCastExce ption?

            Or you can use TypeOf :

            If TypeOf obj Is String Then
            etc...




            "Joe HM" <unixverse@yaho o.coma écrit dans le message de groupe de
            discussion :
            11489d66-4a55-4083-86d7-e75c52e7c0be...leg roups.com...
            On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
            wrote:
            >On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
            >>
            Hello -
            >>
            I was wondering that the "cleanest" way is to determine whether a
            CType() will throw an InvalidCastExce ption? I have data I receive as
            an Object and I want to convert it to a String whenever possible using
            CType(lObjectDu mmy, String) otherwise I will just ignore it.
            >>
            I could put a Try/Catch As System.InvalidC astException around it but I
            was wondering if that is the best solution. I once heard that
            exceptions should not be used as a tool but rather as a means to
            intercept problems during run-time.
            >>
            Thanks,
            Joe
            >>
            >Dim o As SomeObject = TryCast(obj, SomeObject)
            >If Not o Is Nothing Then
            >' Do Cool Stuff
            >End If
            >>
            >--
            >Tom Shelton
            >
            Hello -
            >
            Sorry ... I did not specify that I am using .NET 2003 and I just
            checked but there (unfortunately) is no TryCast.
            >
            Thanks,
            Joe

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: How to check wether CType() will throw InvalidCastExce ption?

              Joe HM

              Almost the code as Tom wrote for Net 1.1

              \\\
              Try
              CType(obj, SomeType)
              ' Do Cool Stuff
              Catch
              'decide yourself if you want to handle this, I would do it for sure,
              while Tom made it just simple as example in my idea.
              End Catch
              ///

              Cor



              "Joe HM" <unixverse@yaho o.comwrote in message
              news:11489d66-4a55-4083-86d7-e75c52e7c0be@1g 2000prd.googleg roups.com...
              On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
              wrote:
              On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
              >
              Hello -
              >
              I was wondering that the "cleanest" way is to determine whether a
              CType() will throw an InvalidCastExce ption? I have data I receive as
              an Object and I want to convert it to a String whenever possible using
              CType(lObjectDu mmy, String) otherwise I will just ignore it.
              >
              I could put a Try/Catch As System.InvalidC astException around it but I
              was wondering if that is the best solution. I once heard that
              exceptions should not be used as a tool but rather as a means to
              intercept problems during run-time.
              >
              Thanks,
              Joe
              >
              Dim o As SomeObject = TryCast(obj, SomeObject)
              If Not o Is Nothing Then
              ' Do Cool Stuff
              End If
              >
              --
              Tom Shelton
              Hello -

              Sorry ... I did not specify that I am using .NET 2003 and I just
              checked but there (unfortunately) is no TryCast.

              Thanks,
              Joe

              Comment

              • Joe HM

                #8
                Re: How to check wether CType() will throw InvalidCastExce ption?

                On Nov 6, 1:40 pm, "Cor Ligthert[MVP]" <Notmyfirstn... @planet.nl>
                wrote:
                Joe HM
                >
                Almost the code as Tom wrote for Net 1.1
                >
                \\\
                Try
                    CType(obj, SomeType)
                   ' Do Cool Stuff
                Catch
                    'decide yourself if you want to handle this, I would do it for sure,
                while Tom made it just simple as example in my idea.
                End Catch
                ///
                >
                Cor
                >
                "Joe HM" <unixve...@yaho o.comwrote in message
                >
                news:11489d66-4a55-4083-86d7-e75c52e7c0be@1g 2000prd.googleg roups.com...
                On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
                wrote:
                >
                >
                >
                >
                >
                On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
                >
                Hello -
                >
                I was wondering that the "cleanest" way is to determine whether a
                CType() will throw an InvalidCastExce ption? I have data I receive as
                an Object and I want to convert it to a String whenever possible using
                CType(lObjectDu mmy, String) otherwise I will just ignore it.
                >
                I could put a Try/Catch As System.InvalidC astException around it but I
                was wondering if that is the best solution. I once heard that
                exceptions should not be used as a tool but rather as a means to
                intercept problems during run-time.
                >
                Thanks,
                Joe
                >
                Dim o As SomeObject = TryCast(obj, SomeObject)
                If Not o Is Nothing Then
                ' Do Cool Stuff
                End If
                >
                --
                Tom Shelton
                >
                Hello -
                >
                Sorry ... I did not specify that I am using .NET 2003 and I just
                checked but there (unfortunately) is no TryCast.
                >
                Thanks,
                Joe- Hide quoted text -
                >
                - Show quoted text -
                Hello -

                Thanks a lot for all the feedback. I ended up doing it as follows:
                Try
                lStringDummy = CType(lObjectDu mmy, String)
                Catch lException As System.InvalidC astException
                lStringDummy = "CType() FAILED"
                End Try

                That way I will not intercept any other exceptions.

                I was thinking about doing what Patrice suggested (If TypeOf obj Is
                String Then ...) but I also want to cast types other than Strings
                (e.g. Integers). I guess I could have Or'ed something together but
                using the Try/Catch seems to be more flexible.

                Thanks again!
                Joe

                Comment

                • Cor Ligthert[MVP]

                  #9
                  Re: How to check wether CType() will throw InvalidCastExce ption?

                  Joe,

                  I was only looking at the solution from Tom.

                  As ToString is a method of Object, and therefore inherited by every class
                  that exist. The most easiest way to convert any object to a string is.

                  Whatever.toStri ng

                  Cor

                  "Joe HM" <unixverse@yaho o.comschreef in bericht
                  news:c316a752-faac-4246-9962-d795e3c59a84@w1 g2000prk.google groups.com...
                  On Nov 6, 1:40 pm, "Cor Ligthert[MVP]" <Notmyfirstn... @planet.nl>
                  wrote:
                  Joe HM
                  >
                  Almost the code as Tom wrote for Net 1.1
                  >
                  \\\
                  Try
                  CType(obj, SomeType)
                  ' Do Cool Stuff
                  Catch
                  'decide yourself if you want to handle this, I would do it for sure,
                  while Tom made it just simple as example in my idea.
                  End Catch
                  ///
                  >
                  Cor
                  >
                  "Joe HM" <unixve...@yaho o.comwrote in message
                  >
                  news:11489d66-4a55-4083-86d7-e75c52e7c0be@1g 2000prd.googleg roups.com...
                  On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
                  wrote:
                  >
                  >
                  >
                  >
                  >
                  On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
                  >
                  Hello -
                  >
                  I was wondering that the "cleanest" way is to determine whether a
                  CType() will throw an InvalidCastExce ption? I have data I receive as
                  an Object and I want to convert it to a String whenever possible using
                  CType(lObjectDu mmy, String) otherwise I will just ignore it.
                  >
                  I could put a Try/Catch As System.InvalidC astException around it but I
                  was wondering if that is the best solution. I once heard that
                  exceptions should not be used as a tool but rather as a means to
                  intercept problems during run-time.
                  >
                  Thanks,
                  Joe
                  >
                  Dim o As SomeObject = TryCast(obj, SomeObject)
                  If Not o Is Nothing Then
                  ' Do Cool Stuff
                  End If
                  >
                  --
                  Tom Shelton
                  >
                  Hello -
                  >
                  Sorry ... I did not specify that I am using .NET 2003 and I just
                  checked but there (unfortunately) is no TryCast.
                  >
                  Thanks,
                  Joe- Hide quoted text -
                  >
                  - Show quoted text -
                  Hello -

                  Thanks a lot for all the feedback. I ended up doing it as follows:
                  Try
                  lStringDummy = CType(lObjectDu mmy, String)
                  Catch lException As System.InvalidC astException
                  lStringDummy = "CType() FAILED"
                  End Try

                  That way I will not intercept any other exceptions.

                  I was thinking about doing what Patrice suggested (If TypeOf obj Is
                  String Then ...) but I also want to cast types other than Strings
                  (e.g. Integers). I guess I could have Or'ed something together but
                  using the Try/Catch seems to be more flexible.

                  Thanks again!
                  Joe


                  Comment

                  Working...