473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.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
Jan 9 '08 #1
9 3876
"SAL" <SA*@discussions.microsoft.comschrieb:
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.

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

Jan 9 '08 #2
"SAL" <SA*@discussions.microsoft.comschrieb
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

Jan 9 '08 #3
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" <SA*@discussions.microsoft.comschrieb
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

Jan 9 '08 #4
"SAL" <SA*@discussions.microsoft.comschrieb
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
Jan 9 '08 #5
"Armin Zingler" <az*******@freenet.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.ToString
='DirectCast(oNames, String)'.
Array of Strings =directcast(oNames, string())
Other objects:

='oNames.ToString()'.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jan 9 '08 #6
Thanks guys. I got it fixed.

"Herfried K. Wagner [MVP]" wrote:
"Armin Zingler" <az*******@freenet.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.ToString

='DirectCast(oNames, String)'.
Array of Strings =directcast(oNames, string())

Other objects:

='oNames.ToString()'.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jan 9 '08 #7
"Herfried K. Wagner [MVP]" <hi***************@gmx.atschrieb
"Armin Zingler" <az*******@freenet.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.ToString

='DirectCast(oNames, String)'.
ToString is faster because it just returns "Me" (inside ToString). No
casting/internal type checking necessary.
>Array of Strings =directcast(oNames, string())

Other objects:

='oNames.ToString()'.
You mean, other than Object? (I ask because oNames is 'Object')
Armin

Jan 9 '08 #8
"Armin Zingler" <az*******@freenet.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.ToString

='DirectCast(oNames, 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(oNames, string())

Other objects:

='oNames.ToString()'.

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/>

Jan 9 '08 #9
"Herfried K. Wagner [MVP]" <hi***************@gmx.atschrieb
"Armin Zingler" <az*******@freenet.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.ToString
>
='DirectCast(oNames, 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
Jan 9 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
11
by: ssg31415926 | last post by:
I need to cast a string into an object and vice versa. At the moment, I'm using code like this: int numOfObjects = Values.Length; object objects = new object; for(int i = 0; i < numOfObjects;...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
4
by: stand__sure | last post by:
This has bugged me for some time. Is there any "behind the scenes" difference between the following two snippets? Dim obj1 As Object = New Oject() {} Dim obj2 As Object = New Object(-1) {}...
9
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array:...
3
by: antonyliu2002 | last post by:
For my web application, I store a primitive array of strings to session. Dim arrLastNames(10) As String blah blah Session("AllLastNames") = arrLastNames Now, when I retrieve this session...
5
by: Eric bouxirot | last post by:
hi, i'd like to do a "cast", who seem to be very complicated in .NET. in C standard it's so easy... i explain : i have made an app based on plugin architecture in VB.NET. all work fine.. ...
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.