Cast Oject to String Array

S

SAL

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.ToString.Split(asDelims)

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

Thanks
 
H

Herfried K. Wagner [MVP]

SAL said:
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.ToString.Split(asDelims)

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

I do not see any casting code.
 
A

Armin Zingler

SAL said:
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.ToString.Split(asDelims)

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


Armin
 
S

SAL

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

Armin Zingler

SAL said:
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.ToString
Array of Strings => directcast(oNames, string())


Armin
 
A

Armin Zingler

Herfried K. Wagner said:
=> 'DirectCast(oNames, String)'.

ToString is faster because it just returns "Me" (inside ToString). No
casting/internal type checking necessary.
Other objects:

=> 'oNames.ToString()'.

You mean, other than Object? (I ask because oNames is 'Object')


Armin
 
H

Herfried K. Wagner [MVP]

Armin Zingler said:
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.
You mean, other than Object? (I ask because oNames is 'Object')

Objects whose type is neither 'String' nor 'String()'.
 
A

Armin Zingler

Herfried K. Wagner said:
'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top