Cast Oject to String Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U0FM?=

    Cast Oject to String Array

    Is it possible to Cast an Object to a String Array the way I'm doing it in
    the code below? I'm trying to reuse code I have without creating new code if
    I can.

    Dim asNames() As String
    Dim asValues() As String
    Dim sKeyPairValues As String = String.Empty

    Dim oNames Object
    Dim oValues Object

    Dim asDelims() As Char = {","}

    asNames = oNames.ToString .Split(asDelims )
    asValues = oValues.ToStrin g.Split(asDelim s)

    For i = 0 To asNames.Length - 1
    Try
    sKeyPairValues += asNames(i).ToUp per & "=" & asValues(i) &
    "; "
    Catch ex As Exception
    TraceUtil.Write Error(ex)
    End Try
    Next

    Thanks
  • Herfried K. Wagner [MVP]

    #2
    Re: Cast Oject to String Array

    "SAL" <SAL@discussion s.microsoft.com schrieb:
    Is it possible to Cast an Object to a String Array the way I'm doing it in
    the code below? I'm trying to reuse code I have without creating new code
    if
    I can.
    >
    Dim asNames() As String
    Dim asValues() As String
    Dim sKeyPairValues As String = String.Empty
    >
    Dim oNames Object
    Dim oValues Object
    Note that you never assign a value to the two variables above.
    Dim asDelims() As Char = {","}
    >
    asNames = oNames.ToString .Split(asDelims )
    asValues = oValues.ToStrin g.Split(asDelim s)
    >
    For i = 0 To asNames.Length - 1
    Try
    sKeyPairValues += asNames(i).ToUp per & "=" & asValues(i) &
    "; "
    Catch ex As Exception
    TraceUtil.Write Error(ex)
    End Try
    Next
    I do not see any casting code.

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

    Comment

    • Armin Zingler

      #3
      Re: Cast Oject to String Array

      "SAL" <SAL@discussion s.microsoft.com schrieb
      Is it possible to Cast an Object to a String Array the way I'm doing
      it in the code below?
      I don't see any cast in your code. Where do you fill oNames and oValues?
      I'm trying to reuse code I have without
      creating new code if I can.
      >
      Dim asNames() As String
      Dim asValues() As String
      Dim sKeyPairValues As String = String.Empty
      >
      Dim oNames Object
      Dim oValues Object
      >
      Dim asDelims() As Char = {","}
      >
      asNames = oNames.ToString .Split(asDelims )
      asValues = oValues.ToStrin g.Split(asDelim s)
      >
      For i = 0 To asNames.Length - 1
      Try
      sKeyPairValues += asNames(i).ToUp per & "=" &
      asValues(i) & "; "
      Catch ex As Exception
      TraceUtil.Write Error(ex)
      End Try
      Next

      Armin

      Comment

      • =?Utf-8?B?U0FM?=

        #4
        Re: Cast Oject to String Array

        I was only showing the code as an example. My actual code passes oNames,
        oValues into my function instead of of DIM it.

        "Armin Zingler" wrote:
        "SAL" <SAL@discussion s.microsoft.com schrieb
        Is it possible to Cast an Object to a String Array the way I'm doing
        it in the code below?
        >
        I don't see any cast in your code. Where do you fill oNames and oValues?
        >
        I'm trying to reuse code I have without
        creating new code if I can.

        Dim asNames() As String
        Dim asValues() As String
        Dim sKeyPairValues As String = String.Empty

        Dim oNames Object
        Dim oValues Object

        Dim asDelims() As Char = {","}

        asNames = oNames.ToString .Split(asDelims )
        asValues = oValues.ToStrin g.Split(asDelim s)

        For i = 0 To asNames.Length - 1
        Try
        sKeyPairValues += asNames(i).ToUp per & "=" &
        asValues(i) & "; "
        Catch ex As Exception
        TraceUtil.Write Error(ex)
        End Try
        Next
        >
        >
        Armin
        >
        >

        Comment

        • Armin Zingler

          #5
          Re: Cast Oject to String Array

          "SAL" <SAL@discussion s.microsoft.com schrieb
          I was only showing the code as an example. My actual code passes
          oNames, oValues into my function instead of of DIM it.
          What will oNames/oValues contain?
          Strings =oNames.ToStrin g
          Array of Strings =directcast(oNa mes, string())


          Armin

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Cast Oject to String Array

            "Armin Zingler" <az.nospam@free net.deschrieb:
            >I was only showing the code as an example. My actual code passes
            >oNames, oValues into my function instead of of DIM it.
            >
            What will oNames/oValues contain?
            Strings =oNames.ToStrin g
            ='DirectCast(oN ames, String)'.
            Array of Strings =directcast(oNa mes, string())
            Other objects:

            ='oNames.ToStri ng()'.


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

            Comment

            • =?Utf-8?B?U0FM?=

              #7
              Re: Cast Oject to String Array

              Thanks guys. I got it fixed.

              "Herfried K. Wagner [MVP]" wrote:
              "Armin Zingler" <az.nospam@free net.deschrieb:
              I was only showing the code as an example. My actual code passes
              oNames, oValues into my function instead of of DIM it.
              What will oNames/oValues contain?
              Strings =oNames.ToStrin g
              >
              ='DirectCast(oN ames, String)'.
              >
              Array of Strings =directcast(oNa mes, string())
              >
              Other objects:
              >
              ='oNames.ToStri ng()'.
              >
              >
              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
              >

              Comment

              • Armin Zingler

                #8
                Re: Cast Oject to String Array

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr ieb
                "Armin Zingler" <az.nospam@free net.deschrieb:
                >>I was only showing the code as an example. My actual code passes
                >>oNames, oValues into my function instead of of DIM it.
                >>
                >What will oNames/oValues contain? Strings =oNames.ToStrin g
                >
                ='DirectCast(oN ames, String)'.
                ToString is faster because it just returns "Me" (inside ToString). No
                casting/internal type checking necessary.
                >Array of Strings =directcast(oNa mes, string())
                >
                Other objects:
                >
                ='oNames.ToStri ng()'.
                You mean, other than Object? (I ask because oNames is 'Object')


                Armin

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Cast Oject to String Array

                  "Armin Zingler" <az.nospam@free net.deschrieb:
                  >>>I was only showing the code as an example. My actual code passes
                  >>>oNames, oValues into my function instead of of DIM it.
                  >>>
                  >>What will oNames/oValues contain? Strings =oNames.ToStrin g
                  >>
                  >='DirectCast(o Names, String)'.
                  >
                  ToString is faster because it just returns "Me" (inside ToString). No
                  casting/internal type checking necessary.
                  'DirectCast' won't call any method whereas calling 'ToString' involves a
                  virutal method call. Well, I just think that 'DirectCast' is semantically
                  more correct because the 'Object' variable already contains a 'String'
                  object.
                  >>Array of Strings =directcast(oNa mes, string())
                  >>
                  >Other objects:
                  >>
                  >='oNames.ToStr ing()'.
                  >
                  You mean, other than Object? (I ask because oNames is 'Object')
                  Objects whose type is neither 'String' nor 'String()'.

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

                  Comment

                  • Armin Zingler

                    #10
                    Re: Cast Oject to String Array

                    "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr ieb
                    "Armin Zingler" <az.nospam@free net.deschrieb:
                    I was only showing the code as an example. My actual code
                    passes oNames, oValues into my function instead of of DIM
                    it.

                    What will oNames/oValues contain? Strings =oNames.ToStrin g
                    >
                    ='DirectCast(oN ames, String)'.
                    ToString is faster because it just returns "Me" (inside ToString).
                    No casting/internal type checking necessary.
                    >
                    'DirectCast' won't call any method whereas calling 'ToString'
                    involves a virutal method call. Well, I just think that
                    'DirectCast' is semantically more correct because the 'Object'
                    variable already contains a 'String' object.
                    Yes, but with ToString no casting has to be done. Ok, both is
                    possible. :-) I currently can't test it again (IIRC ToString was
                    faster) but I will ASAP.


                    Armin

                    Comment

                    Working...