How to cast object to primitive array?

A

antonyliu2002

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 item with the session key
"AllLastNames", how do I cast it back to an array of primitive Strings?


I tried below, but did not work:

Dim arrLNames(10) As String
arrLNames = CType(Session.Contents("AllLastNames"), String())

The compiler says,

System.InvalidCastException: Specified cast is not valid.

Thanks.
 
M

MS

Hi
You can use

Dim arrLNames(10) As String
arrLNames = (string [] )(Session.Contents("AllLastNames"), String())
it is syntax in C#

might be im bit wrong for Vb
 
A

antonyliu2002

Hi, thank you for sharing your idea. But unfortunately, that will not
work for my VB code. I was thinking hard in order to find the
equivalent format in VB (like the one your offered), but could not get
it. And I googled hard, but could not find a good answer.
Hi
You can use

Dim arrLNames(10) As String
arrLNames = (string [] )(Session.Contents("AllLastNames"), String())
it is syntax in C#

might be im bit wrong for Vb

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 item with the session key
"AllLastNames", how do I cast it back to an array of primitive Strings?


I tried below, but did not work:

Dim arrLNames(10) As String
arrLNames = CType(Session.Contents("AllLastNames"), String())

The compiler says,

System.InvalidCastException: Specified cast is not valid.

Thanks.
 
M

MSDN

This works for me,

Dim arrLNames(10) As String

Dim oArray(10) As String

Dim oArr As String()

arrLNames(0) = "Smith"

arrLNames(1) = "Jones"

Session.Add("AllLastNames", arrLNames)

arrLNames = CType(Session.Contents("AllLastNames"), String())

oArray = CType(Session.Contents("AllLastNames"), String())

oArr = CType(Session.Contents("AllLastNames"), String())

Label1.Text = oArray(0).ToString() & ", " & oArray(1).ToString()

Label2.Text = oArr(0).ToString() & ", " & oArr(1).ToString()



SA

Hi, thank you for sharing your idea. But unfortunately, that will not
work for my VB code. I was thinking hard in order to find the
equivalent format in VB (like the one your offered), but could not get
it. And I googled hard, but could not find a good answer.
Hi
You can use

Dim arrLNames(10) As String
arrLNames = (string [] )(Session.Contents("AllLastNames"), String())
it is syntax in C#

might be im bit wrong for Vb

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 item with the session key
"AllLastNames", how do I cast it back to an array of primitive Strings?


I tried below, but did not work:

Dim arrLNames(10) As String
arrLNames = CType(Session.Contents("AllLastNames"), String())

The compiler says,

System.InvalidCastException: Specified cast is not valid.

Thanks.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top