How to pass extract data from an ArrayList passed back from a clas

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SkI=?=

    How to pass extract data from an ArrayList passed back from a clas

    Hello

    My pgm1 (User Interface Level) passes an empty ArrayList to pgm2
    (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate
    the ArrayList.

    Question1: When pgm2 gets the ArrayList back from pgm3 how to extract and
    separate the fields out fo the ArrayLists?

    Question2: When pgm3 gets ArrayList back from pgm2 how to separate the
    fields out of the Arraylist and assign it to variables that will be process
    later on?

    Below is the code simplified:

    Pgm1 ****
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.Collecti ons;
    using plist;

    namespace plist
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeCompo nent();
    }
    private void Form1_Load(obje ct sender, EventArgs e)
    {
    ArrayList funcList = new ArrayList();
    ArrayList arrayData = new ArrayList();
    funcList = pgm2.AEdr(array Data);
    string adr = "";
    string edr = "";
    for (int i = 0; i < funcList.Count; i++)
    {
    "adr" + i = funcList[i].ToString(adr);
    "edr" + i = funcList[i].ToString(edr);
    }
    //label1.Text = "there are " + funcList.Count + " rows";
    }
    private void label1_Click(ob ject sender, EventArgs e)
    {
    }
    }
    }

    Pgm2 ****
    using System;
    using System.Collecti ons;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace plist
    {
    class pgm2
    {
    private string adr;
    private string edr;
    public pgm2(string adr, string edr)
    {
    Adr = adr;
    Edr = edr;
    }
    public string Adr
    {
    get { return adr; }
    set { adr = value; }
    }
    public string Edr
    {
    get { return edr; }
    set { edr = value; }
    }
    public static ArrayList AEdr(ArrayList arrayData)
    {
    string adr = "";
    string edr = "";
    ArrayList funcList = new ArrayList();
    ArrayList objList = new ArrayList();
    funcList = pgm3.dataList(o bjList);
    for (int i=0; i<funcList.Coun t; i++)
    {
    adr = funcList[i].ToString(adr);
    edr = funcList[i].ToString(edr);
    arrayData.Add(a dr);
    arrayData.Add(e dr);
    }
    return arrayData;
    }
    }
    }

    Pgm3 ****
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Collecti ons;
    using System.Data;
    using System.Data.Sql Client;

    namespace plist
    {
    class pgm3
    {
    public static ArrayList dataList(ArrayL ist objList)
    {
    SqlConnection connection = GetConn.GetConn ection();
    ArrayList dataList = new ArrayList();
    string selectStatement = @"select adr, edr from
    wdTable";
    SqlCommand selectCommand = new SqlCommand(sele ctStatement,
    connection);
    SqlDataReader reader;
    connection.Open ();
    reader = selectCommand.E xecuteReader
    (CommandBehavio r.SingleResul t);
    while (reader.Read())
    {
    pgm2 pgm2list = new
    pgm2((string)re ader["adr"],(string)reader
    ["edr"]);
    pgm2list.Adr = (string)reader["adr"];
    pgm2list.Edr = (string)reader["edr"];
    dataList.Add(pg m2list);
    }
    objList = dataList;
    connection.Clos e();
    return objList;
    }
    }
    }


  • Peter Duniho

    #2
    Re: How to pass extract data from an ArrayList passed back from aclas

    On Tue, 14 Oct 2008 16:11:03 -0700, JB <JB@discussions .microsoft.com>
    wrote:
    Hello
    >
    My pgm1 (User Interface Level) passes an empty ArrayList to pgm2
    (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to
    populate
    the ArrayList.
    Actually, no it doesn't. The pgm3 class never uses the ArrayList that was
    passed to it. It just ignores it.
    Question1: When pgm2 gets the ArrayList back from pgm3 how to extract
    and
    separate the fields out fo the ArrayLists?
    Well, you can design it however you like. But you do need to make sure
    that the code retrieving the data uses the same technique as the code
    adding the data. Right now you add instances of the pgm2 class, but then
    try to retrieve them as if they were alternating elements in the
    ArrayList. Instead, you should just cast each element to pgm2 and
    retrieve the properties directly.

    That said...
    Question2: When pgm3 gets ArrayList back from pgm2 how to separate the
    fields out of the Arraylist and assign it to variables that will be
    process
    later on?
    Do you mean "when pgm1 gets ArrayList back from pgm2"? Otherwise, your
    question doesn't make sense.

    It seems to me that the code you posted is just plain bogus. It's not
    "simplified ", it's been contorted into something that couldn't possibly
    work. Don't post code like that. It's not useful. And the code you
    posted looks to me as though it might not even run; you are passing the
    previous result of a call to ToString() to the next call. At best, the
    argument would be ignored, but it may actually throw an exception
    complaining about a bad format string.

    It seems to me that there are at least two major things you need to figure
    out:

    -- do you want the lower level code to populate an existing ArrayList,
    or do you just want the lower level code to return a new one. Right now,
    you are passing down an ArrayList that is only used in the middle step.
    The lowest level code (in pgm3) ignores it completely; you wind up
    allocating not one, but _two_ ArrayList instances that are simply never
    used. That's just dopey.

    -- do you want to store the data in a single class, or do you want
    alternate elements in the ArrayList to track each field being returned?
    Right now, the lowest level creates an instance of a single class (the
    pgm2 class), but the middle level tries (and fails) to break the two
    properties of that class into alternating elements of the output ArrayList.

    Finally, you should go back and reread the advice that was given to you in
    your previous question. In particular, if you're passing an ArrayList to
    a method, it can just modify that ArrayList without having to return the
    instance reference. The caller will just use the reference it already
    had. Conversely, if you want to return a new ArrayList instance, don't
    even bother passing an existing one in. That's wasteful and pointless.

    Pete

    Comment

    Working...