Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,689 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,321 people online right now. Registration is fast and FREE... Join Now!




Another ArrayList question

2 Pages V  1 2 >  
Reply to this topicStart new topic

Another ArrayList question, UGH!

rgfirefly24
post 28 May, 2008 - 11:26 AM
Post #1


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


ok so i am using an arraylist

CODE

ArrayList name = new ArrayList();



let me set this up for you:

I have two list boxes

Box 1 (add/remove buttons) Box 2

the first list box contains 238 countries - the countries in list box 2. The second List box contains Countries that the user decided they did not want information about.

What needs to happen:

Upon click of the Remove button(removes country from List box 2)
i need to store the country name that was removed in an ArrayList

Problem:

name keeps getting reset to count = 0.

Code that i can show:
CODE

public partial class classname : Inherited
{
    SqlConnection conn = null;
    int count = 0;
    String country = "none";
    ArrayList name = new ArrayList(); //setup of the arraylist



CODE

protected void btnRemove_Click(object sender, EventArgs e)
    {
      
        foreach (int j in listbox2.GetSelectedIndices())
        {
            ListItem movecountry = new ListItem();
            movecountry.Value = listbox2.Items[j].Value;
            movecountry.Text = listbox2.Items[j].Text;
            listbox1.Items.Add(movecountry);
            
            name.Add(listbox2.Items[j].Text);
            listbox2.Items.Remove(movecountry);
            
        }


This post has been edited by rgfirefly24: 28 May, 2008 - 11:28 AM
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 28 May, 2008 - 11:35 AM
Post #2


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


What is this? Can we see its declaration?
CODE

name.Add(listbox2.Items[j].Text);


It seems sort of superfluous, and you hadn't mentioned it in your description of the issue.

This post has been edited by crcapps: 28 May, 2008 - 11:37 AM
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 28 May, 2008 - 11:37 AM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(crcapps @ 28 May, 2008 - 12:35 PM) *

What is this? Can we see its declaration?
CODE

name.Add(listbox2.Items[j].Text);



what do you mean exactly? are you talking about name? if so the declaration is shown twice in this post smile.gif otherwise specify exactly which part your curious about.
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 28 May, 2008 - 11:39 AM
Post #4


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


Oh, okay, I saw it up at the beginning.

So the text in name[j] gets literally set to "count=0"?

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:37 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:35 PM) *

What is this? Can we see its declaration?
CODE

name.Add(listbox2.Items[j].Text);



what do you mean exactly? are you talking about name? if so the declaration is shown twice in this post smile.gif otherwise specify exactly which part your curious about.

User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 28 May, 2008 - 11:43 AM
Post #5


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(crcapps @ 28 May, 2008 - 12:39 PM) *

Oh, okay, I saw it up at the beginning.

So the text in name[j] gets literally set to "count=0"?

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:37 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:35 PM) *

What is this? Can we see its declaration?
CODE

name.Add(listbox2.Items[j].Text);



what do you mean exactly? are you talking about name? if so the declaration is shown twice in this post smile.gif otherwise specify exactly which part your curious about.



no What happens is I click the remove button once. The text for item[j] is placed into the arraylist. The problem. When i click remove a 2nd time the ArrayList is empty (count = 0). The text that was previously stored in there is not there anymore.

and just incase you get confused. ArrayList has a .Count feature that tells you how many objects are in the the ArrayList. When i click the remove button a 2nd time i hover over ArrayList and it shows count = 0 what it should tell me is count = 1

This post has been edited by rgfirefly24: 28 May, 2008 - 11:46 AM
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 28 May, 2008 - 11:48 AM
Post #6


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


Not sure if it will fix exactly what you are experiencing, but instead of
CODE

name.Add(listbox2.Items[j].Text);


this is probably better all around
CODE

name.Add(movecountry.Text);
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 28 May, 2008 - 11:50 AM
Post #7


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(crcapps @ 28 May, 2008 - 12:48 PM) *

Not sure if it will fix exactly what you are experiencing, but instead of
CODE

name.Add(listbox2.Items[j].Text);


this is probably better all around
CODE

name.Add(movecountry.Text);



it might be but see it doesnt fix the issue. I think the issue has to do with Localization of variables vs globalization but i'm not 100% sure. I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 28 May, 2008 - 11:52 AM
Post #8


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


QUOTE(rgfirefly24 @ 28 May, 2008 - 12:50 PM) *

I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.


You are probably correct about that, but maybe make the ArrayList static, rather than an instance? That, or use this:

CODE

this.name.Add(movecountry.Text);


Because if you aren't using the this keyword, then you aren't necessarily referring to the class member, just a local variable.

This post has been edited by crcapps: 28 May, 2008 - 11:54 AM
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 28 May, 2008 - 11:54 AM
Post #9


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(crcapps @ 28 May, 2008 - 12:52 PM) *

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:50 PM) *

I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.


You are correct about that. Maybe make the ArrayList static, rather than an instance?



then it wouldn't be usable inside a non static method if i remember right?

CODE

Static ArrayList name;//would this be how you would set a static arraylist?
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 28 May, 2008 - 11:55 AM
Post #10


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


check my edits. You probably need to use this to refer to name as a class member.

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:54 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:52 PM) *

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:50 PM) *

I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.


You are correct about that. Maybe make the ArrayList static, rather than an instance?



then it wouldn't be usable inside a non static method if i remember right?

CODE

Static ArrayList name;//would this be how you would set a static arraylist?


User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 28 May, 2008 - 03:18 PM
Post #11


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


QUOTE(crcapps @ 28 May, 2008 - 12:55 PM) *

check my edits. You probably need to use this to refer to name as a class member.

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:54 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:52 PM) *

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:50 PM) *

I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.


You are correct about that. Maybe make the ArrayList static, rather than an instance?



then it wouldn't be usable inside a non static method if i remember right?

CODE

Static ArrayList name;//would this be how you would set a static arraylist?




you know your probably right. I've been so stressed with getting this application complete that i miss so many of the little things. They cut our deadline down by 2 weeks so we are scrambling to get this finished in time.
User is offlineProfile CardPM

Go to the top of the page

crcapps
post 29 May, 2008 - 07:31 AM
Post #12


D.I.C Head

**
Joined: 13 May, 2008
Posts: 53


My Contributions


Hope it helped!

Sorry about the stupid questions earlier, it's pretty bad here at work too, right now, so sometimes it takes me a couple readings to understand what's going on because of everything else I was working on at the time.

QUOTE(rgfirefly24 @ 28 May, 2008 - 04:18 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:55 PM) *

check my edits. You probably need to use this to refer to name as a class member.

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:54 PM) *

QUOTE(crcapps @ 28 May, 2008 - 12:52 PM) *

QUOTE(rgfirefly24 @ 28 May, 2008 - 12:50 PM) *

I would assume that anything Added to ArrayList wouldn't just be added for that instance of the Remove_Click but be there beyond that since the ArrayList is actually declared class level.


You are correct about that. Maybe make the ArrayList static, rather than an instance?



then it wouldn't be usable inside a non static method if i remember right?

CODE

Static ArrayList name;//would this be how you would set a static arraylist?




you know your probably right. I've been so stressed with getting this application complete that i miss so many of the little things. They cut our deadline down by 2 weeks so we are scrambling to get this finished in time.

User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:35AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month