473,508 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass ArrayLists to classes and back?

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[i] = objList.adr[i];
funcList.edr[i] = objList.edr[i];
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.Single Result);
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

--
JB
Oct 11 '08 #1
5 1385
On Fri, 10 Oct 2008 19:29:01 -0700, JB <JB@discussions.microsoft.com>
wrote:
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?
At the very least, if something doesn't work, you need to explain in what
way it doesn't work. Ideally, you would post a concise-but-complete code
sample.

The code you posted won't even compile without modification. In
"***pgm1", your call to "AEdr(ArrayList arrayData)" includes the type
specifier "ArrayList" when it should only include the actual variable
being passed ("arrayData").

That _might_ be the issue you're having, or maybe it's something else.
You haven't provided nearly enough detail for anyone to know.

Pete
Oct 11 '08 #2
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!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 10 Oct 2008 19:29:01 -0700, JB <JB@discussions.microsoft.com>
wrote:
>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?

At the very least, if something doesn't work, you need to explain in what
way it doesn't work. Ideally, you would post a concise-but-complete code
sample.

The code you posted won't even compile without modification. In
"***pgm1", your call to "AEdr(ArrayList arrayData)" includes the type
specifier "ArrayList" when it should only include the actual variable
being passed ("arrayData").

That _might_ be the issue you're having, or maybe it's something else.
You haven't provided nearly enough detail for anyone to know.

Pete

Oct 11 '08 #3
On 11 Oct, 03:29, JB <J...@discussions.microsoft.comwrote:
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[i] = objList.adr[i];
* * * * * * funcList.edr[i] = objList.edr[i];
* * * * * * 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.Single Result);
* * 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

--
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<CustomerGetCustomers()
{
return CustomerDALC.GetCustomerData();
}
}

DAL

public class CustomerDALC
{
public static List<CustomerGetCustomerData()
{
List<CustomergetList = 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.Single Result);
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;
}
}
Oct 11 '08 #4
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);
}

"JB" <JB@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
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[i] = objList.adr[i];
funcList.edr[i] = objList.edr[i];
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.Single Result);
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

--
JB
Oct 11 '08 #5
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.
On Oct 10, 10:29*pm, JB <J...@discussions.microsoft.comwrote:
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[i] = objList.adr[i];
* * * * * * funcList.edr[i] = objList.edr[i];
* * * * * * 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.Single Result);
* * 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

--
JB
Oct 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4072
by: Pierre Rouleau | last post by:
I have a GUI application written in Python (with WxPython) which uses some low level classes also written in Python. These low level Python classes are given a pointer to some GUI Python object. ...
4
1607
by: Christiaan | last post by:
Hi, I have two classes, Department and Employer, the Department contains an ArrayList (employers) filled with Employer objects. I want to display an ArrayList filled with Department objects using...
5
1900
by: adolf garlic | last post by:
Suggestions please for strategy to share values across app. Scenario: I have an asp.net app that uses some com components along with .net classes. Configuration settings for various things...
7
2351
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the statement that solved the problem is necessary. The inspiration for the statement came from an undocumented...
3
5464
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
5
2384
by: drdave | last post by:
I would like to have ten arraylists created within a loop.. is there a conversion or something I can do to acheive this.. pseudo: Dim counter As Integer = 0 Dim ArrName As ArrayList ...
2
8469
by: JosAH | last post by:
What is an ArrayList In Java, Arrays are used to store multiple items of the same time in an easy to use structure. When you want to store multiple pieces of data and have data types relate to...
12
11006
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
3498
by: =?Utf-8?B?SkI=?= | last post by:
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...
0
7224
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
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5049
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
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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.