Problems casting from Session() to a user-defined structure

Hi all and thanks in advance for your time.

I have defined a structure in my code:

Structure installArray
 Dim array ( , ) As String
 Dim arrayLimit As Integer
 Dim arrayColumns As Integer
 Dim arrayCurrentIndex As Integer
End Structure

I create an instance of this array and manipulate it.  Between life cycles of the page, I save the array to Session:
    Session("projArray") = projArray

When I try to retrieve the structure from Session, however, I cannot seem to cast it correctly.  The following code block gives 2 errors:

Error A: System.NullReferenceException: Object variable or With block variable not set.
Error B: System.InvalidCastException: Specified cast is not valid

Here's the full code snippet where both errors are occuring:
' initialize the array
Dim projArray As installArray
projArray.arrayColumns = 1

if (Session("projArray").arrayCurrentIndex <> Nothing) then '<-- !!! Error A !!!
    projArray = CType(Session("projArray"), installArray)  ' <--  !!! Error B !!!
    projArray.arrayCurrentIndex = 0
    redim projArray.array( projArray.arrayColumns, 10 )
    projArray.arrayLimit = 10
    Session("projArray") = projArray
end if

So, my two questions:

Error A: What is the proper way to check to see if a Session variable has been initialized to something?  ie: Tell if this is the first load of the page or not.

Error B: CType can apparently be used to cast to structures.  Why is my cast not working?  Is it because of how the structure is being stored in Session in the first place?  Or am I simply not casting it correctly?

Thanks so much!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Question 1.  
             If Not Session("projArray") Is Nothing

Question 2.
             Dont Cast
             projArray = Session("projArray")
In response..

1)  Correct, use this method

2)  No!  Always cast!
projArray = ctype(Session("projArray"), installArray)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thanks for the reply raterus,

I am pretty much self taught in ASP.Net, can you tell us why to always cast...

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

If you don't, you force .net to made an guess as to what the object is, and it doesn't always make the best decision.  If you were using C#, it wouldn't let you get away with this, everything has be be casted.

For programmers, I recommend "Option Strict On" at the top of every code file, which will force you to cast everything.  You can also configure this for the whole project in Visual Studio.  You will be a better programmer for it, trust me...
Thanks raterus, I will use this from now on... Great explanation...
might not want too once you see how many compile warnings you'll receive if you set "Option Strict On" in a big project :-)
KarnaxjsAuthor Commented:
Thanks guys, the answer to Error A worked like a charm.  I'm still quite new to

With regards to the second error, as can be seem in the code snippet, I believe I'm doing what was suggested... although after fixing the first error, the second one just went away.  So 'yay!', I suppose.

Thanks again for your time.
KarnaxjsAuthor Commented:
Ah, sorry sagacitysolutions.  I'm also new to this forum and didn't see the 'split' option.  :)

Thanks for the heads up, raterus!
KarnaxjsAuthor Commented:
Actually, funny enough Error B did just come back.  Anyone have any ideas?:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 342:
Line 343:If Not Session("projArray") Is Nothing
Line 344:    projArray = CType(Session("projArray"), installArray)  ' <-- Error: needs to be cast to an installArray type.
Line 345:else
Line 346:    projArray.arrayCurrentIndex = 0

Source File: c:\inetpub\wwwroot\installRequest\installTest.aspx    Line: 344

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   ASP.installTest_aspx.__Render__control1(HtmlTextWriter __output, Control parameterContainer) in c:\inetpub\wwwroot\installRequest\installTest.aspx:344
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +27
   System.Web.UI.Control.Render(HtmlTextWriter writer) +7
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
   System.Web.UI.Page.ProcessRequestMain() +1926

If it's truly an array, you may need to do this,
projArray = CType(Session("projArray"), installArray())
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.