array list

M

Morten Wennevik

Hi Hrcko,

You need to add each item in turn to an array or an ArrayList

object[] o = new object[listBox1.Items.Count];
for(int i = 0; i < o.Length; i++)
o = listBox1.Items;

Or ...

ArrayList list = new ArrayList();
foreach(object o in listBox1.Items)
list.Add(o);
 
B

Bjorn Abelli

You need to add each item in turn to an array or an ArrayList

object[] o = new object[listBox1.Items.Count];
for(int i = 0; i < o.Length; i++)
o = listBox1.Items;

Or ...

ArrayList list = new ArrayList();
foreach(object o in listBox1.Items)
list.Add(o);



If you don't want to write that much code, just create the ArrayList...

ArrayList a = new ArrayList( this.listBox1.Items );

Or if you wanted it as an array instead of an ArrayList...

Object[] a = new ArrayList( this.listBox1.Items ).ToArray();


// Bjorn A
 
H

Hrcko

I have a problem with this code because I have a Guid in my listBox.

I can't convert System.Guid to Guid[].

Hrcko
 
M

Morten Wennevik

Ah, you are right. Had the impression that the overloaded constructor needed an object[], but all it needs is an ICollection.

Also, Hrcko, if you have a previously created ArrayList, you can use ArrayList.AddRange(ListBox.Items).

You need to add each item in turn to an array or an ArrayList

object[] o = new object[listBox1.Items.Count];
for(int i = 0; i < o.Length; i++)
o = listBox1.Items;

Or ...

ArrayList list = new ArrayList();
foreach(object o in listBox1.Items)
list.Add(o);



If you don't want to write that much code, just create the ArrayList...

ArrayList a = new ArrayList( this.listBox1.Items );

Or if you wanted it as an array instead of an ArrayList...

Object[] a = new ArrayList( this.listBox1.Items ).ToArray();


// Bjorn A
 
H

Hrcko

I have a problem with this code because I have a Guid in my listBox.

I can't convert System.Guid to Guid[].

Hrcko
 
B

Bjorn Abelli

...
I have a problem with this code because I
have a Guid in my listBox.

I can't convert System.Guid to Guid[].

I think you possibly need to explain a little more what your problem is...

If you mean that you want a single Guid to appear in an array, well...

Lets say you have a System.Guid variable g...

System.Guid[] guidArray = {g};

...or were you referring to how to extract the elements of the listbox *as*
an array of Guid's?

Guid[] guidArray = (Guid[])
new ArrayList( this.listBox1.Items ).ToArray(typeof(Guid));


// Bjorn A
 
H

Hrvoje Voda

I need to put a list of Guids from listBox into Guid Array.

Hrcko

Bjorn Abelli said:
...
I have a problem with this code because I
have a Guid in my listBox.

I can't convert System.Guid to Guid[].

I think you possibly need to explain a little more what your problem is...

If you mean that you want a single Guid to appear in an array, well...

Lets say you have a System.Guid variable g...

System.Guid[] guidArray = {g};

..or were you referring to how to extract the elements of the listbox *as*
an array of Guid's?

Guid[] guidArray = (Guid[])
new ArrayList( this.listBox1.Items ).ToArray(typeof(Guid));


// Bjorn A
 
B

Bjorn Abelli

...
I need to put a list of Guids from listBox into Guid Array.

So did you try the example I wrote?

Guid[] guidArray = (Guid[])
new ArrayList( this.listBox1.Items ).ToArray(typeof(Guid));


// Bjorn A
 
H

Hrvoje Voda

This all works fine.

Now, I would like to chek if that array list is empty.

Hrcko


Bjorn Abelli said:
...
I need to put a list of Guids from listBox into Guid Array.

So did you try the example I wrote?

Guid[] guidArray = (Guid[])
new ArrayList( this.listBox1.Items ).ToArray(typeof(Guid));


// Bjorn A
 
B

Bjorn Abelli

...
Now, I would like to chek if that array list is empty.

You could either check the number of items *before* you get them from the
listbox...

if (this.listBox1.Items.Count == 0) ...

....or check the length of the array after you've created it

if (guidArray.Length == 0) ...

// Bjorn A
 
M

Morten Wennevik

listBox1.Items.AddRange(guidArray);

Or, if your goal is to take the items from one ListBox (or other list control) and put them in another you can do

listBox2.Items.AddRange(listBox1.Items);
 
H

Hrvoje Voda

Correction :

I want to display items that ARE NOT in my array list...

I wrote some code but I don't know how to exclude those items
nUserID is an array list.
if (nUserID.Length != 0)

{


listUsers.DisplayMember = "UserName";

sSelectString = "UserID<>'" + nUserID.ToString() + "'";


DataRow[] Urows = db.dataSetAccounts.Users.Select(

sSelectString,"UserName",System.Data.DataViewRowState.CurrentRows);

foreach ( DataSetAccounts.UsersRow row in Urows )

{

DataSetAccounts.UsersRow users =

db.dataSetAccounts.Users.FindByUserID ( row.UserID );

if ( users != null )

{

listUsers.Items.Add( users );

}

}


}

Hrcko
 
B

Bjorn Abelli

...
I want to display items that ARE NOT in my array list...

I wrote some code but I don't know how to exclude those items
nUserID is an array list.

....and your code isn't that much helpful to understand your problem...

(BTW, your use of the words "array list" when actually talking about just an
"array" is a bit confusing...)

What I can see is that

- you have an array "nUserID".
- you have a ListBox "listUsers"

But my guess is that you're not storing items of the same type in both?

....and now you want to poulate "listUsers" with something you read from a
database, except for those that exist in "nUserID"?

Well...

Your code doesn't show what is what or of what types, or what the types look
like, but if I guess that a DataSetAccounts.UsersRow has an attribute
"UserID", which can or cannot be found in the array...


if ( users != null && Array.IndexOf(nUserID, users.UserID) < 0 )
{
listUsers.Items.Add( users );
}


// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dataset table into an array list 13
sum 4
checked listbox check all 3
check disabled 3
array byte to string 1
blob from database 4
array list 3
listbox search 1

Top