By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Checking items in an arraylist

P: n/a
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;
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Stephen <St*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
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 )

On Fri, 26 Nov 2004 06:07:06 -0800, Stephen
<St*****@discussions.microsoft.com> wrote:
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;


--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #3

P: n/a
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" wrote:
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 )

On Fri, 26 Nov 2004 06:07:06 -0800, Stephen
<St*****@discussions.microsoft.com> wrote:
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;


--
Happy Coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #4

P: n/a
Stephen <St*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

P: n/a
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 [C# MVP]" wrote:
Stephen <St*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #6

P: n/a
Stephen <St*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.