Link to home
Create AccountLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

List<struct> help!

In the code snippet below, where I have marked WRONG is where I'm having problems...

I want to be able to take data from the table and put them in a list of structures.

What am I missing?
struct JobData {
  string jobName;
  string jobDate;
}
	
List<JobData> sArray = new List<JobData>();

and this is the code:

//  get the job name and date from all of the records
string selectCmd = "SELECT JOB_NAME, JOB_DATE FROM JobInfo";
c.CommandText = selectCmd;

conn.Open();
dr = c.ExecuteReader();

while(dr.Read())	{
	JobData jd = new JobData();
	sArray.Add(jobName);  <-------- WRONG!
				
			}

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

I think you might be wanting something like:


/// Not this but
while(dr.Read())	{
	JobData jd = new JobData();
	sArray.Add(jobName);  <-------- WRONG!
}

/// This???
while(dr.Read()) {
	JobData jd = new JobData();
	/// Without knowing what dr is reading or if jobName is a 
	jd.jobName = dr!JOB_NAME;
	jd.jobDate = dr!JOB_DATE;
}

Open in new window

Ooops...  Forgot this:


/// This???
while(dr.Read()) {
	JobData jd = new JobData();
	/// Without knowing what dr is reading or if jobName is a 
	jd.jobName = dr!JOB_NAME;
	jd.jobDate = dr!JOB_DATE;
	sArray.Add(jd);
}

Open in new window

And yet one more change...  Sorry mixed some vb.net in with the c...  :(


/// This???
while(dr.Read()) {
	JobData jd = new JobData();
	/// Without knowing what dr is reading or if jobName is a 
	jd.jobName = Convert.ToString(dr["JOB_NAME"]);
	jd.jobDate = Convert.ToDataTime(dr["JOB_DATE"]);
	sArray.Add(jd);
}

Open in new window

Avatar of rmmarsh

ASKER

Well.... jd.jobName and jd.jobDate are not accessable due to their protection level...

I need to keep them global...
while(dr.Read()) {
	JobData jd = new JobData();
	jd.jobName = dr[1];
	jd.jobDate = dr[0];
	sArray.Add(jd);
}

Open in new window

They are not global outside of the function, this is true, however, you are adding each "new" instance of jd to the Array.  The array is "global" with the code that you have provided.  To access each jd (as it were) you would need to step through the array elements.

-saige-
Avatar of rmmarsh

ASKER

I moved the struct as shown... now it's worse - I got 20 errors from the build!

Arrrrrgggghhhh!

:D
public partial class AppDelegate 
	{
		
		List<JobData> sArray = new List<JobData>();
		
		
		//  display contents of database
		public void viewDatabase ()	 {
			
			struct JobData {
				string jobName;
				string jobDate;
			};

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of rmmarsh

ASKER

Saige:  the statements with AS won't build (Unexpected Symbol)... I've never used AS, so at this point I'm lost.  I got the data into the List, but now don't know how to get each item out! :D

lazyberezovsky:  thanks, the "public" got rid of the addressability errors...
Avatar of rmmarsh

ASKER

Saige... never mind, I figured it out... thanks to both of you for your help...