Using array of user-classes in a FOR EACH...LOOP

Using user-defined classes in a FOR EACH...LOOP

Page 1 of 1

5 Replies - 8126 Views - Last Post: 18 June 2008 - 03:56 AM Rate Topic: -----

#1 kc_james  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 07

Using array of user-classes in a FOR EACH...LOOP

Posted 05 November 2007 - 02:45 PM

I'm trying to use a array of a user-defined class in a FOR EACH...LOOP but keep getting a NullReferenceException and when I add a break point and look at the code it doesn't seem to be placing anything in the reference variable. I reduced my code to a simple test program and am still getting the same results. The code I'm using is:

Program Code:
dim outputString as string
dim MyTitles(10) as Title

For Each x As Title In MyTitles
outputString = x.Name
Next x

Class Code:
Public Class Title
Private sName as string

Public ReadOnly Property Name() as String
Get
Return sName
End Get
End Property
End Class

Any help would be greatly appreciated.

Regards,
James.

This post has been edited by kc_james: 05 November 2007 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using array of user-classes in a FOR EACH...LOOP

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Using array of user-classes in a FOR EACH...LOOP

Posted 05 November 2007 - 04:14 PM

Ouch, OOP in VB.NET. I'm afraid I never bothered learning the VB syntax for this. However, I should be able to help.

You've initialized a 10 element array of Title? After you do that, you have 10 places to store Title instances, MyTitles(0), MyTitles(1), etc. However, right now they're all NULL.

That For Each returns 10 values, all NULL. The first call to x.Name, x is NULL, so x has no method Name, so fails in the usual way.

You fix this by either loading all 10 elements with New Title() or allowing for NULLs in your loop.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Using array of user-classes in a FOR EACH...LOOP

Posted 05 November 2007 - 05:36 PM

It may be helpful to create a custom collection class instead of using an array.

This post may be of some help to you with that.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Using array of user-classes in a FOR EACH...LOOP

Posted 05 November 2007 - 10:19 PM

Where is your constructor for your Title class?

You cannot instantiate a class object without a constructor.

Even though you are creating an array of class objects you need to use the 'new' keyword for each element in the array. Otherwise they will be null, hence the NullReferenceException.
Was This Post Helpful? 0
  • +
  • -

#5 kc_james  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 07

Re: Using array of user-classes in a FOR EACH...LOOP

Posted 07 November 2007 - 01:46 AM

Thanks that helped. I've got it working now.
Till next time.
Was This Post Helpful? 0
  • +
  • -

#6 sheril  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 2
  • Joined: 17-June 08

Re: Using array of user-classes in a FOR EACH...LOOP

Posted 18 June 2008 - 03:56 AM

The for each loop syntax is

For Each [Item] In [Group]
[loopBody]
Next [Item]


http://vb.net-inform...r_each_loop.htm

sher
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1