Checking items in an arraylist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    #1

    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(dgSearchAddr esses.Items.Cou nt == 0)
    {
    this.pnlSearchA ddresses.Visibl e = false;
    }

    //******** else if(alSearchaddr ess[0] == test)********//
    {
    this.pnlSearchA ddresses.Visibl e = false;
    }

    else
    {
    this.pnlSearchA ddresses.Visibl e = true;
    this.lblMessage .Text = "";
    }
    }

    Arraylist is being created like so: -

    Address newAddress = new Address();
    newAddress.Addr ess1 = "Address not supplied";
    newAddress.Addr ess2 = null;
    newAddress.Addr ess3 = " ";
    newAddress.Addr ess4 = null;
    newAddress.Addr ess5 = null;
    newAddress.Addr ess6 = null;


  • Jon Skeet [C# MVP]

    #2
    Re: Checking items in an arraylist

    Stephen <Stephen@discus sions.microsoft .com> wrote:[color=blue]
    > 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(dgSearchAddr esses.Items.Cou nt == 0)
    > {
    > this.pnlSearchA ddresses.Visibl e = false;
    > }
    >
    > //******** else if(alSearchaddr ess[0] == test)********//
    > {
    > this.pnlSearchA ddresses.Visibl e = false;
    > }
    >
    > else
    > {
    > this.pnlSearchA ddresses.Visibl e = true;
    > this.lblMessage .Text = "";
    > }
    > }[/color]

    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.
    [color=blue]
    > Arraylist is being created like so: -
    >
    > Address newAddress = new Address();
    > newAddress.Addr ess1 = "Address not supplied";
    > newAddress.Addr ess2 = null;
    > newAddress.Addr ess3 = " ";
    > newAddress.Addr ess4 = null;
    > newAddress.Addr ess5 = null;
    > newAddress.Addr ess6 = null;[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Morten Wennevik

      #3
      Re: Checking items in an arraylist

      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 = alSearchAddress es[0];
      Address a = (Address)o;
      if(a.Address1 == test)
      ...

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

      On Fri, 26 Nov 2004 06:07:06 -0800, Stephen
      <Stephen@discus sions.microsoft .com> wrote:
      [color=blue]
      > 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(dgSearchAddr esses.Items.Cou nt == 0)
      > {
      > this.pnlSearchA ddresses.Visibl e = false;
      > }
      >
      > //******** else if(alSearchaddr ess[0] == test)********//
      > {
      > this.pnlSearchA ddresses.Visibl e = false;
      > }
      >
      > else
      > {
      > this.pnlSearchA ddresses.Visibl e = true;
      > this.lblMessage .Text = "";
      > }
      > }
      >
      > Arraylist is being created like so: -
      >
      > Address newAddress = new Address();
      > newAddress.Addr ess1 = "Address not supplied";
      > newAddress.Addr ess2 = null;
      > newAddress.Addr ess3 = " ";
      > newAddress.Addr ess4 = null;
      > newAddress.Addr ess5 = null;
      > newAddress.Addr ess6 = null;
      >
      >[/color]



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

      Comment

      • Stephen

        #4
        Re: Checking items in an arraylist

        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(dgSearchAddr esses.Items.Cou nt == 0)
        {
        this.pnlSearchA ddresses.Visibl e = false;
        }

        else if(a.Address1 == test)
        {
        this.pnlSearchA ddresses.Visibl e = false;
        }

        "Morten Wennevik" wrote:
        [color=blue]
        > 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 = alSearchAddress es[0];
        > Address a = (Address)o;
        > if(a.Address1 == test)
        > ...
        >
        > Or you can put it all on one line with
        > if( ((Address)alSea rchAddresses[0]).Address1 == test )
        >
        > On Fri, 26 Nov 2004 06:07:06 -0800, Stephen
        > <Stephen@discus sions.microsoft .com> wrote:
        >[color=green]
        > > 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(dgSearchAddr esses.Items.Cou nt == 0)
        > > {
        > > this.pnlSearchA ddresses.Visibl e = false;
        > > }
        > >
        > > //******** else if(alSearchaddr ess[0] == test)********//
        > > {
        > > this.pnlSearchA ddresses.Visibl e = false;
        > > }
        > >
        > > else
        > > {
        > > this.pnlSearchA ddresses.Visibl e = true;
        > > this.lblMessage .Text = "";
        > > }
        > > }
        > >
        > > Arraylist is being created like so: -
        > >
        > > Address newAddress = new Address();
        > > newAddress.Addr ess1 = "Address not supplied";
        > > newAddress.Addr ess2 = null;
        > > newAddress.Addr ess3 = " ";
        > > newAddress.Addr ess4 = null;
        > > newAddress.Addr ess5 = null;
        > > newAddress.Addr ess6 = null;
        > >
        > >[/color]
        >
        >
        >
        > --
        > Happy Coding!
        > Morten Wennevik [C# MVP]
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Checking items in an arraylist

          Stephen <Stephen@discus sions.microsoft .com> wrote:[color=blue]
          > 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(dgSearchAddr esses.Items.Cou nt == 0)
          > {
          > this.pnlSearchA ddresses.Visibl e = false;
          > }
          >
          > else if(a.Address1 == test)
          > {
          > this.pnlSearchA ddresses.Visibl e = false;
          > }[/color]

          That suggests that your ViewState doesn't have the "Addresses" item
          set.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Stephen

            #6
            Re: Checking items in an arraylist

            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:
            [color=blue]
            > Stephen <Stephen@discus sions.microsoft .com> wrote:[color=green]
            > > 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(dgSearchAddr esses.Items.Cou nt == 0)
            > > {
            > > this.pnlSearchA ddresses.Visibl e = false;
            > > }
            > >
            > > //******** else if(alSearchaddr ess[0] == test)********//
            > > {
            > > this.pnlSearchA ddresses.Visibl e = false;
            > > }
            > >
            > > else
            > > {
            > > this.pnlSearchA ddresses.Visibl e = true;
            > > this.lblMessage .Text = "";
            > > }
            > > }[/color]
            >
            > 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.
            >[color=green]
            > > Arraylist is being created like so: -
            > >
            > > Address newAddress = new Address();
            > > newAddress.Addr ess1 = "Address not supplied";
            > > newAddress.Addr ess2 = null;
            > > newAddress.Addr ess3 = " ";
            > > newAddress.Addr ess4 = null;
            > > newAddress.Addr ess5 = null;
            > > newAddress.Addr ess6 = null;[/color]
            >
            > 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 - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Checking items in an arraylist

              Stephen <Stephen@discus sions.microsoft .com> wrote:[color=blue]
              > 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
              > }[/color]

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

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

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

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...