473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Checking items in an arraylist

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
6 1799
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
1
by: Stephen | last post by:
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...
6
by: Stephen | last post by:
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...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.