Checking items in an arraylist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Im trying to carry work out an else if clause in the below method but i'm
having a lot of difficulty getting the correct code which allows me to check
and see if an arraylist items has text in it containing Address not supplied.
Im trying to do something below but its not working properly. Can someone
help me do this please. the code i use is below.

private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

//******** else if(alSearchaddress[0] == test)********//
{
this.pnlSearchAddresses.Visible = false;
}

else
{
this.pnlSearchAddresses.Visible = true;
this.lblMessage.Text = "";
}
}

Arraylist is being created like so: -

Address newAddress = new Address();
newAddress.Address1 = "Address not supplied";
newAddress.Address2 = null;
newAddress.Address3 = " ";
newAddress.Address4 = null;
newAddress.Address5 = null;
newAddress.Address6 = null;
 
Stephen said:
Im trying to carry work out an else if clause in the below method but i'm
having a lot of difficulty getting the correct code which allows me to check
and see if an arraylist items has text in it containing Address not supplied.
Im trying to do something below but its not working properly. Can someone
help me do this please. the code i use is below.

private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

//******** else if(alSearchaddress[0] == test)********//
{
this.pnlSearchAddresses.Visible = false;
}

else
{
this.pnlSearchAddresses.Visible = true;
this.lblMessage.Text = "";
}
}

The "else" line there would be checking for reference equality, as the
compiler doesn't know to use the string overload. Either cast it to
string, or call Equals instead.
Arraylist is being created like so: -

Address newAddress = new Address();
newAddress.Address1 = "Address not supplied";
newAddress.Address2 = null;
newAddress.Address3 = " ";
newAddress.Address4 = null;
newAddress.Address5 = null;
newAddress.Address6 = null;

That hasn't created an ArrayList at all.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi Stephen,

An ArrayList stores all the items as Object so you need to cast it back to
its original type before you can do much with it.

object o = alSearchAddresses[0];
Address a = (Address)o;
if(a.Address1 == test)
...

Or you can put it all on one line with
if( ((Address)alSearchAddresses[0]).Address1 == test )

Im trying to carry work out an else if clause in the below method but i'm
having a lot of difficulty getting the correct code which allows me to
check
and see if an arraylist items has text in it containing Address not
supplied.
Im trying to do something below but its not working properly. Can
someone
help me do this please. the code i use is below.

private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

//******** else if(alSearchaddress[0] == test)********//
{
this.pnlSearchAddresses.Visible = false;
}

else
{
this.pnlSearchAddresses.Visible = true;
this.lblMessage.Text = "";
}
}

Arraylist is being created like so: -

Address newAddress = new Address();
newAddress.Address1 = "Address not supplied";
newAddress.Address2 = null;
newAddress.Address3 = " ";
newAddress.Address4 = null;
newAddress.Address5 = null;
newAddress.Address6 = null;
 
I changed my code to below and got the following error in the browser window
have you any idea what i've done wrong.

Error
Object reference not set to an instance of an object.
for line: -
object o = alSearchaddress[0];

Code changed to0: -
private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";
object o = alSearchaddress[0];
Address a = (Address)o;

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

else if(a.Address1 == test)
{
this.pnlSearchAddresses.Visible = false;
}

Morten Wennevik said:
Hi Stephen,

An ArrayList stores all the items as Object so you need to cast it back to
its original type before you can do much with it.

object o = alSearchAddresses[0];
Address a = (Address)o;
if(a.Address1 == test)
...

Or you can put it all on one line with
if( ((Address)alSearchAddresses[0]).Address1 == test )

Im trying to carry work out an else if clause in the below method but i'm
having a lot of difficulty getting the correct code which allows me to
check
and see if an arraylist items has text in it containing Address not
supplied.
Im trying to do something below but its not working properly. Can
someone
help me do this please. the code i use is below.

private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

//******** else if(alSearchaddress[0] == test)********//
{
this.pnlSearchAddresses.Visible = false;
}

else
{
this.pnlSearchAddresses.Visible = true;
this.lblMessage.Text = "";
}
}

Arraylist is being created like so: -

Address newAddress = new Address();
newAddress.Address1 = "Address not supplied";
newAddress.Address2 = null;
newAddress.Address3 = " ";
newAddress.Address4 = null;
newAddress.Address5 = null;
newAddress.Address6 = null;
 
Stephen said:
I changed my code to below and got the following error in the browser window
have you any idea what i've done wrong.

Error
Object reference not set to an instance of an object.
for line: -
object o = alSearchaddress[0];

Code changed to0: -
private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";
object o = alSearchaddress[0];
Address a = (Address)o;

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

else if(a.Address1 == test)
{
this.pnlSearchAddresses.Visible = false;
}

That suggests that your ViewState doesn't have the "Addresses" item
set.
 
Sorry this was only a snippit of the code. My problem isn't really anything
to do with how the arraylist is created my problem is trying to determine
whether the arraylist has the text I outlined in it. I need to write some
sort of if condition which checks this.
basically

if(/*ArrayList has text in it containing "Address Not Found" I need to do
the following below in the curly brackets */)
{
//if true do this
}

Jon Skeet said:
Stephen said:
Im trying to carry work out an else if clause in the below method but i'm
having a lot of difficulty getting the correct code which allows me to check
and see if an arraylist items has text in it containing Address not supplied.
Im trying to do something below but its not working properly. Can someone
help me do this please. the code i use is below.

private void checkArrayList()
{
ArrayList alSearchaddress;
alSearchaddress = (ArrayList) ViewState["Addresses"];
string test = "Address not supplied";

if(dgSearchAddresses.Items.Count == 0)
{
this.pnlSearchAddresses.Visible = false;
}

//******** else if(alSearchaddress[0] == test)********//
{
this.pnlSearchAddresses.Visible = false;
}

else
{
this.pnlSearchAddresses.Visible = true;
this.lblMessage.Text = "";
}
}

The "else" line there would be checking for reference equality, as the
compiler doesn't know to use the string overload. Either cast it to
string, or call Equals instead.
Arraylist is being created like so: -

Address newAddress = new Address();
newAddress.Address1 = "Address not supplied";
newAddress.Address2 = null;
newAddress.Address3 = " ";
newAddress.Address4 = null;
newAddress.Address5 = null;
newAddress.Address6 = null;

That hasn't created an ArrayList at all.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Stephen said:
Sorry this was only a snippit of the code. My problem isn't really anything
to do with how the arraylist is created my problem is trying to determine
whether the arraylist has the text I outlined in it. I need to write some
sort of if condition which checks this.
basically

if(/*ArrayList has text in it containing "Address Not Found" I need to do
the following below in the curly brackets */)
{
//if true do this
}

By "has text in it" do you mean anywhere? If so:

if (theArrayList.IndexOf("Address Not Found") != -1)
{
....
}

However, that will only work if the ArrayList directly contains strings
- not Address objects.
 
Back
Top