array conversions

  • Thread starter Thread starter Sankar Nemani
  • Start date Start date
S

Sankar Nemani

Why does the following code throw an exception?

Dim o1() As Object = New Object() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

Following is a snippet from VB.NET language specification on MSDN.
 
Sankar,

Is the sample not this
Dim s() As String
Dim o1() As Object = _
New String() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

In my opinion are you trying to cast some arrays containing different
objects.
Not one object to a given type.

I hope this helps?

Cor
 
Sankar Nemani said:
Why does the following code throw an exception?

Dim o1() As Object = New Object() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())


This won't work. You'll have to cast each item in the array individually.

\\\
Dim o1() As Object = {"Sankar", "Sankar2"}
Dim s1(o1.Length - 1) As String
For i As Integer = 0 To o1.Length - 1
s1(i) = DirectCast(o1(i), String)
Next i
///
 

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

Back
Top