How to pass ArrayLists to classes and back?

J

JB

Hello

I am passing an arraylist to be filled with data from pgm1 to pgm2 to
pgm3 where it gets filled, then I pass it back from pgm3 to pgm2 to pgm1
where I will use the data from arraylist. The problem is that I am having
trouble passing the arraylist back and forth. Below is a sample of what I am
trying to do and I would like to know where I am going wrong?

***pgm1:
private void Form1_Load(object sender, System.EventArgs e)
{
string Adr;
string Edr;
int IRows;
ArrayList funcList;
ArrayList arrayData;
arrayData.Add("Adr");
arrayData.Add("Edr");
arrayData.Add("IRows");
funcList.Add("Adr");
funcList.Add("Edr");
funcList.Add("IRows");
funcList = pgm2.AEdr(ArrayList arrayData);
label1.Text = "there are " + funcList.Count + "rows";
}

***pgm2:
public void AEdr(ArrayList arrayData)
{
ArrayList funcList = new ArrayList();
ArrayList objList = new ArrayList();
funcList = pgm3.dataList(objList);
for(int i = 0; i < objList.Count; i++)
{
string adr;
string edr;
int irows;
funcList.adr = objList.adr;
funcList.edr = objList.edr;
funcList.irows = irows++;
arrayData.Add(funcList);
}
return arrayData;
}
***pgm3:
public ArrayList dataList(ArrayList objList)
{
ArrayList(objList) getList = new ArrayList (objList);
SqlConnection connection = new
SqlConnection("Data Source=localhost;Initial Catalog=sDatabase;Integrated
Security=True");
string selectStatement = @"select adr, edr from wdTable";
SqlCommand selectCommand = new SqlCommand(selectStatement,
connection);
SqlDataReader reader;
connection.Open();
reader = selectCommand.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
objList getList = new objList
((string)reader["adr"],(string)reader["edr"]);
getList.Adr = (string)reader["adr"];
getList.Edr = (string)reader["edr"];
objList.Add(getList);
}
reader.Close;
connection.Close();
return objList;
}

Thanks
JB
 
N

Nathan Sokalski

Peter is definitely correct in his statement, but I am noticing one other
thing in your code. In pgm2 and pgm3, you return the parameter that you pass
in. This is not necessary, since an ArrayList is an Object and is therefore
a reference variable. So rather than return a value to assign to a variable
in the calling function, just make the function void. In the function,
modify the ArrayList parameter as you would have otherwise, and then when
you return to the calling function, it will be updated, since it is really
the same variable. I am not always the best person at explaining things, but
if you do not already, you should learn the concept of and difference
between value and reference variables, it is a very important part of
object-oriented programming. Good Luck!
 
J

JTC^..^

Hello

    I am passing an arraylist to be filled with data from pgm1 to pgm2 to
pgm3 where it gets filled, then I pass it back from pgm3 to pgm2 to pgm1
where I will  use the data from arraylist.  The problem is that I am having
trouble passing the arraylist back and forth.  Below is a sample of what I am
trying to do and I would like to know where I am going wrong?

***pgm1:
private void Form1_Load(object sender, System.EventArgs e)
{
    string Adr;
    string Edr;
    int IRows;
    ArrayList funcList;
    ArrayList arrayData;
    arrayData.Add("Adr");
    arrayData.Add("Edr");
    arrayData.Add("IRows");
    funcList.Add("Adr");
    funcList.Add("Edr");
    funcList.Add("IRows");
    funcList = pgm2.AEdr(ArrayList arrayData);
    label1.Text = "there are "  + funcList.Count + "rows";

}

***pgm2:
public void AEdr(ArrayList arrayData)
{
    ArrayList funcList = new ArrayList();
    ArrayList objList = new ArrayList();
    funcList = pgm3.dataList(objList);
    for(int i = 0; i < objList.Count; i++)
        {
            string adr;
            string edr;
            int irows;
            funcList.adr = objList.adr;
            funcList.edr = objList.edr;
            funcList.irows = irows++;
            arrayData.Add(funcList);
        }
        return arrayData;}

***pgm3:
public ArrayList dataList(ArrayList objList)
{
    ArrayList(objList) getList = new ArrayList (objList);
    SqlConnection connection = new
    SqlConnection("Data Source=localhost;Initial Catalog=sDatabase;Integrated
        Security=True");
    string selectStatement = @"select adr, edr from wdTable";
    SqlCommand selectCommand = new SqlCommand(selectStatement,
        connection);
    SqlDataReader reader;
    connection.Open();
    reader = selectCommand.ExecuteReader(CommandBehavior.SingleResult);
    while (reader.Read())
    {
        objList getList = new objList
((string)reader["adr"],(string)reader["edr"]);
        getList.Adr = (string)reader["adr"];
        getList.Edr = (string)reader["edr"];
        objList.Add(getList);
    }
    reader.Close;
    connection.Close();
    return objList;

}

    Thanks
    JB


As pointed out in the previous post you need to be explain the problem
you are having. However, my mind reading powers are telling me that
you are designing a tiered solution, pgm1 pgm2 pgm3. being the the UI,
BLL, DAL respectively.

My code deciphering machine has produced a consise example based on a
predicted nTier architecture: CodeExample example =
RandomCodeInterpreter.Refactor(randomCode, Architecture.nTier) ;) I
it's working and has managed to give you a good idea. Joking aside
though, in the future please be more consise with your posts.

UI

public partial class OrderForm : Form
{
public OrderForm()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.lbCustomers.DataSource = Customer.GetCustomers();
}
}

BLL

public class Customer
{
private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}

private string _emailAddress;
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}

public static List<Customer> GetCustomers()
{
return CustomerDALC.GetCustomerData();
}
}

DAL

public class CustomerDALC
{
public static List<Customer> GetCustomerData()
{
List<Customer> getList = new List<Customer>();

SqlConnection connection = new SqlConnection("Data
Source=localhost;Initial Catalog=sDatabase;Integrated Security=True");
string selectStatement = @"select adr, edr from wdTable";
SqlCommand selectCommand = new SqlCommand(selectStatement,
connection);
SqlDataReader reader;
connection.Open();
reader =
selectCommand.ExecuteReader(CommandBehavior.SingleResult);
while (reader.Read())
{
Customer customer = new Customer();
customer.Address = (string)reader["adr"];
customer.EmailAddress = (string)reader["edr"];
getList.Add(customer);
}
reader.Close();
connection.Close();

return getList;
}
}
 
D

Davita

When you pass a class reference as a parameter to a method, there is no need
for the method to return the arraylist object with return keyword, instead,
the changes made in calling method will be reflected on the caller method
too. for example

ArrayList aa = new ArrayList();
InsertOne(aa);
//Now 1 is inserted into aa
Console.WriteLine(aa[0]);

public void InsertOne(ArrayList ar)
{
ar.Add(1);
}
 
H

harborsparrow

Dear JB,

I think that you have been a little confused about what an ArrayList
variable is. It is just a reference (kind of like a pointer in C).
It is not the list itself.

If you want to get an ArrayList back from another method, that method
needs to have a signature like this:

public ArrayList SomeMethod(ArrayList inputReference)
{
ArrayList localList = inputReference;
localList.Add("some new data");
return localList;
}

But in the code below, you are calling (in pgm2) a method of type
void. The method you call needs to return an ArrayList type.

Hope this helps.


Hello

    I am passing an arraylist to be filled with data from pgm1 to pgm2 to
pgm3 where it gets filled, then I pass it back from pgm3 to pgm2 to pgm1
where I will  use the data from arraylist.  The problem is that I am having
trouble passing the arraylist back and forth.  Below is a sample of what I am
trying to do and I would like to know where I am going wrong?

***pgm1:
private void Form1_Load(object sender, System.EventArgs e)
{
    string Adr;
    string Edr;
    int IRows;
    ArrayList funcList;
    ArrayList arrayData;
    arrayData.Add("Adr");
    arrayData.Add("Edr");
    arrayData.Add("IRows");
    funcList.Add("Adr");
    funcList.Add("Edr");
    funcList.Add("IRows");
    funcList = pgm2.AEdr(ArrayList arrayData);
    label1.Text = "there are "  + funcList.Count + "rows";

}

***pgm2:
public void AEdr(ArrayList arrayData)
{
    ArrayList funcList = new ArrayList();
    ArrayList objList = new ArrayList();
    funcList = pgm3.dataList(objList);
    for(int i = 0; i < objList.Count; i++)
        {
            string adr;
            string edr;
            int irows;
            funcList.adr = objList.adr;
            funcList.edr = objList.edr;
            funcList.irows = irows++;
            arrayData.Add(funcList);
        }
        return arrayData;}

***pgm3:
public ArrayList dataList(ArrayList objList)
{
    ArrayList(objList) getList = new ArrayList (objList);
    SqlConnection connection = new
    SqlConnection("Data Source=localhost;Initial Catalog=sDatabase;Integrated
        Security=True");
    string selectStatement = @"select adr, edr from wdTable";
    SqlCommand selectCommand = new SqlCommand(selectStatement,
        connection);
    SqlDataReader reader;
    connection.Open();
    reader = selectCommand.ExecuteReader(CommandBehavior.SingleResult);
    while (reader.Read())
    {
        objList getList = new objList
((string)reader["adr"],(string)reader["edr"]);
        getList.Adr = (string)reader["adr"];
        getList.Edr = (string)reader["edr"];
        objList.Add(getList);
    }
    reader.Close;
    connection.Close();
    return objList;

}

    Thanks
    JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top