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

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

P: n/a
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using plist;

namespace plist
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ArrayList funcList = new ArrayList();
ArrayList arrayData = new ArrayList();
funcList = pgm2.AEdr(arrayData);
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(object sender, EventArgs e)
{
}
}
}

Pgm2 ****
using System;
using System.Collections;
using System.Collections.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(objList);
for (int i=0; i<funcList.Count; i++)
{
adr = funcList[i].ToString(adr);
edr = funcList[i].ToString(edr);
arrayData.Add(adr);
arrayData.Add(edr);
}
return arrayData;
}
}
}

Pgm3 ****
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace plist
{
class pgm3
{
public static ArrayList dataList(ArrayList objList)
{
SqlConnection connection = GetConn.GetConnection();
ArrayList dataList = new ArrayList();
string selectStatement = @"select adr, edr from
wdTable";
SqlCommand selectCommand = new SqlCommand(selectStatement,
connection);
SqlDataReader reader;
connection.Open();
reader = selectCommand.ExecuteReader
(CommandBehavior.SingleResul t);
while (reader.Read())
{
pgm2 pgm2list = new
pgm2((string)reader["adr"],(string)reader
["edr"]);
pgm2list.Adr = (string)reader["adr"];
pgm2list.Edr = (string)reader["edr"];
dataList.Add(pgm2list);
}
objList = dataList;
connection.Close();
return objList;
}
}
}
Oct 14 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Oct 14 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.