Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

ArrayList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rn5a@rediffmail.com - 16 Oct 2007 02:11 GMT
What's wrong with the following code?

Dim arrColors(5) As ArrayList

arrColors(5) = New ArrayList
arrColors(0).Add("Red")
arrColors(1).Add("Blue")
arrColors(2).Add("Green")
arrColors(3).Add("Yellow")
arrColors(4).Add("Black")

Response.Write(arrColors(0).ToString & "<br>")
Response.Write(arrColors(1).ToString & "<br>")
Response.Write(arrColors(2).ToString & "<br>")
Response.Write(arrColors(3).ToString & "<br>")
Response.Write(arrColors(4).ToString & "<br>")

The above code generates the following error:

Object reference not set to an instance of an object.

pointing to

arrColors(0).Add("Red")

Thanks,

Ron
Peter Bromberg [C# MVP] - 16 Oct 2007 02:30 GMT
Dim arrColors(5) As ArrayList
declares an ArrayList type
BUT - it does not create an instance of the type.
Dim arrColors As New ArrayList()
declares the Arraylist arrColors AND creates an instance.
You do not need to specify the number of elements as the ArrayList type
automatically expands as you add elements.

Whenever you get in trouble like this its a good idea to look at the
documentation, like so:
http://www.google.com/search?source=ig&hl=en&q=Arraylist+class

Cheers.
Signature

Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> What's wrong with the following code?
>
[quoted text clipped - 24 lines]
>
> Ron
rn5a@rediffmail.com - 16 Oct 2007 03:02 GMT
On Oct 15, 8:30 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yohohhoandabottleofrum.com> wrote:
> Dim arrColors(5) As ArrayList
> declares an ArrayList type
[quoted text clipped - 45 lines]
>
> - Show quoted text -

Thanks, Peter, for your input but I am creating an instance. Please
have a look at the code I have shown in post #1. Note the line
immediately after I "Dim" the variable arrColors which is

arrColors(5) = New ArrayList

That line creates an instance, doesn't it?

Regards,

Ron
Aidy - 16 Oct 2007 09:26 GMT
> arrColors(0).Add("Red")

arrColours(0) is the object at position 0 in the array, however you have not
added anything to the array so this returns null (nothing) and you get your
error.

Do this instead;

arrColors.Add("Red")
arrColors.Add("Blue")
arrColors.Add("Green")
arrColors.Add("Yellow")
arrColors.Add("Black")
rn5a@rediffmail.com - 16 Oct 2007 14:47 GMT
> > arrColors(0).Add("Red")
>
[quoted text clipped - 9 lines]
> arrColors.Add("Yellow")
> arrColors.Add("Black")

Thanks, Aidy, for your suggestion. Why doesn't

arrColors(0).Add("Red"),
arrColors(1).Add("Blue")

etc. add anything to the array? I couldn't exactly understand this.
Could you please explain me?

Also is it possible to ReDim & Preserve with ArrayList?

Thanks once again,

Regards,

Ron
Aidy - 16 Oct 2007 15:01 GMT
> Thanks, Aidy, for your suggestion. Why doesn't
>
> arrColors(0).Add("Red"),
> arrColors(1).Add("Blue")
>
> etc. add anything to the array?

Basically an ArrayList is not an array, it is just a collection of objects.
With an array, if I create a 5 length array then I get a structure that can
hold 5 elements of my given type.  So I can then do;

arr[0] = "Red"
arr[1] = "Blue"

etc.

When you create an ArrayList, you merely get an object that is an empty
collection, it has no objects in it so you can't do

arr[0] =

as there is nothing at position 0.  For there to be something at position 0
you have to add it;

arr.Add("Red") -- this is the first thing added so will have position 0

You're just getting confused between arrays and the ArrayList object,
they're not the same.  This syntax;

arrColors(0).Add("Red")

is the same as this

Dim o as Object
o = arrColours(0)
o.Add("Red")

What you're doing is getting the object at position 0 then calling that
object's Add method.  As well as the problem above of there being no objects
in the array, if there *was* an object such as a string "Red" then you're
calling the Add method of that object which can give you errors if the
object doesn't support it.  You are *not* calling the Add method of the
ArrayList, you are calling the Add method of the object at that position.

> Also is it possible to ReDim & Preserve with ArrayList?

Again not really as they are just collections.  The ArrayList will grow and
shrink in size as you add/remove elements.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.