Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

COllections

Posted on 2005-05-01
3
Medium Priority
?
154 Views
Last Modified: 2010-04-16
I have a class called BaseClass.

The BaseClass and some of its variables are as follows:

***************************** BaseClass**************************************
public class BaseClass
      {
            ArrayList listOfClubMember;
      }


public void addMember(string name)
      {
            ClubMember clubMember = new ClubMember(name);
            listOfClubMember.Add(clubMember);
      }

public  ArrayList ListOfClubMember
            {
                  get{return listOfClubMember;}
            }      
*************************** END OF BASE CLASS ***********************************

as you can see I am calling a new class ClubMember which is being passed the string variable name.
I will give you the definition of ClubMember class below:

**********************************************************************  |
                                                     ClubMember                                                                       |
************************************************************************
public ClubMember(string name)
            {
                  AdminProfile adminProfile = new AdminProfile(name);
            }

Again from here the AdminProfile class is called. In the admin profile constructor name is passed and name is set in the class.
so basically the flow of control is as follows:

BaseClass ----> ClubMember ------>AdminProfile and all the way back again to the BaseClass.

now here is the problem starting :
back in BaseClass please notice the following:

             listOfClubMember.add(clubMember);

Now technically speaking every time this add method is called the listOfClubMember Arraylist should be incremented and if I do a count on the ArrayList I should get the size.

the size is 0 .... it just refuses to increase. this is because
a) either the list is not being added to
b) there is some logic error that I cant find
0
Comment
Question by:deersuper
3 Comments
 
LVL 21

Expert Comment

by:Yurich
ID: 13907040
well, i presume you have your array list declared somewhere outside of your constructor and in the constructor, you just need to initialize it:

listOfClubMember = new ArrayList;

let us know if you still have the same problem.

regards
0
 
LVL 4

Accepted Solution

by:
nachiket earned 1500 total points
ID: 13907668
Work fine in following test application

I can think of follownig mistakes u may be doing.

1. Are u using the same BaseClass Object every time ? If you add to one object and read from other oject it will be different every time!!
2. In your example  "ArrayList listOfClubMember" is a class level variable (i guess it will be like that only) if incase by mistake u are newing it in constructor make sure you access the class level variable not the local one in constructor.

in yor example if following is a constructor i guess u are using the local variable listOfClubMember and not class level listOfClubMember.

public class BaseClass
     {
          ArrayList listOfClubMember;
     }

if this is the mistake do following
public class BaseClass
     {
          this.listOfClubMember = new ArrayList();
     }


=====================
Class
=====================
public class BaseClass
      {
            private ArrayList listOfClubMember = new ArrayList();
      
            public void addMember(string name)
            {
                  ClubMember clubMember = new ClubMember(name);
                  listOfClubMember.Add(clubMember);
            }

            public  ArrayList ListOfClubMember
            {
                  get{return listOfClubMember;}
            }    
      }

      public class ClubMember
      {
            public ClubMember(string name)
            {
                  AdminProfile adminProfile = new AdminProfile(name);
            }
      }

      public class AdminProfile
      {
            private string profile = null;
            public AdminProfile(string name)
            {
                  profile = name;
            }
      }


=============================
Use
=============================

private BaseClass b = new BaseClass();
            private void add_Click(object sender, System.EventArgs e)
            {
                  b.addMember("One");
                  b.addMember("Two");
                  MessageBox.Show(b.ListOfClubMember.Count.ToString());
            }
0
 
LVL 2

Author Comment

by:deersuper
ID: 13909554
You were absolutely right ! but along with declaring the ArrayList OUTSIDE of the constructor I also added the word static to it because this arraylist belongs to a class and not to any instance of this class.


thanks guys .....
you guys are good !
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question