p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Instantiate Integer array in a stucture (http://p2p.wrox.com/showthread.php?t=14386)

hashtlishnii June 18th, 2004 10:28 AM

Instantiate Integer array in a stucture
Hello all,
I'm working with VB.NET 2002, 1.0 Framework. Here's my problem: I'm trying to create a STRUCTURE with an array element. The STRUCTURE looks like this:

Private Structure StructOfLabs
    Dim LabGroup As String
    Dim Hours() As Integer
End Structure

My big problem comes when I try to put stuff into the array. I can get to the LabGroup ok, but the compiler doesn't like me trying to access Hours() until I instantiate it. It tells me:
"An unhandeled exception of type 'System.NullReferenceException' occured in UpdateCalendar.exe

Additional information: Object reference not set to an instance of an object."

myLabs(0).LabGroup = ""
myLabs(0).Hours(0) = 0

I've searched & searched and I get tantalizing hints of how to go about it, but nothing materalizes. I would sure appreciate a bit of help. Thanks, all. -RK

jaucourt June 18th, 2004 11:26 AM

I think your problem might be that you've not dimensioned the array - i.e. you need to either do


Private Structure StructOfLabs
    Dim LabGroup As String
    Dim Hours(0) As Integer
End Structure

OR, where you are using the array, first do


Redim StructOfLabs.Hours(0)

hashtlishnii June 18th, 2004 11:56 AM

VB won't let me do the first: "Arrays declared as structure members cannot be declared with an initial size." The second suggestion did work, though. I didn't even think of trying the Redim. Thanks! -RK

All times are GMT -4. The time now is 12:37 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.