Creating Dynamic array with struct

a1jit

Regular
Joined
Aug 19, 2005
Messages
89
Hi Guys,

I have been working on this for the past few hours but cant seem to get it right..

I have a web service..and i plan to use the dynamic array for array declaration

I have a struct like below

Code:
		public struct CAnalysisField
		{
			public double id;
			public string name;
			public string fullname;
			public string description;
		}


And below is my code to loop through data reader.

Code:
public RuleDbDataTypes.CAnalysisField[] processAnalysesNames()
{

RuleDbDataTypes.CAnalysisField[] AnalysisNames=null;int i=0;
OracleConnection con = databaseLogon(“x”,”x”,”x”);
OracleDataReader reader = processORA(con,constructSQL(ConfigurationSettings.AppSettings["getAnalysesNames"]));

//AnalysisNames = new RuleDbDataTypes.CAnalysisField[]; // find way to create dynamic array

while (reader.Read())
{
AnalysisNames[i].id = reader.GetDouble(0);
AnalysisNames[i].name = reader.GetString(1);
AnalysisNames[i].fullname = reader.GetString(2);
AnalysisNames[i].description = reader.GetString(3);
i++;
}

databaseLogout(con);
return AnalysisNames;
}


The problem is that i dont know how to create a dynamic array and keep appending the array size as the record goes in.

I dont want to execute a separate query to get the count(*), as it will take extra time, and i feel its good if we can change the size of array dynamically..

thank you very much for any help..
 
Creating a strongly typed array class could solve your problem. Essentially you could then have an array that works similar to an ArrayList. I'm not 100% sure off the top of my head if it's possible to create a strongly typed array of structs, but if it isn't you could possibly use a small class rather than a struct?
 
Hi Cags, thanks for the quick reply..would appreciate if you have a short example of strongly typed array so that i can get a better picture of it.

thank you very much
 
The architecture...
C#:
public struct CAnalysisField
{
	public double id;
	public string name;
	public string fullname;
	public string description;
}

public class CAnalysisFieldCollection : CollectionBase
{
	public CAnalysisField this[int index] 
	{
		get { return (CAnalysisField)List[index]; }
		set { List[index] = value; }
	}

	public void Add(CAnalysisField value)
	{
		List.Add(value);
	}
}

It's use in a loop would be something like...
C#:
while (reader.Read())
{
	CAnalysisField tmpItem = new CAnalysisField();
	tmpItem.id = reader.GetDouble(0);
	tmpItem.name = reader.GetString(1);
	tmpItem.fullname = reader.GetString(2);
	tmpItem.description = reader.GetString(3);
	myCollection.Add(tmpItem);
}
 
Hi Cags, thank you very much for pointing out the code..would appreciate if you are able to provide some explanation for the "public class CAnalysisFieldCollection : CollectionBase"

And just have 2 questions,
1) what is the declaration for myCollection object?
2) How do i return the array back to the function? Initiality this is what i did.

C#:
public RuleDbDataTypes.CAnalysisField[] processAnalysesNames()
{
RuleDbDataTypes.CAnalysisField[] AnalysisNames=null
AnalysisNames = new RuleDbDataTypes.CAnalysisField[];
while (reader...)
    return CAnalysisField;
}
thanks a million..
 
Last edited by a moderator:
The line
C#:
public class CAnalysisFieldCollection : CollectionBase
simply creates a class called CAnalysisFieldCollection which inherits from CollectionBase.

A1.
C#:
CAnalysisFieldCollection myCollection = new CAnalysisFieldCollection;
A2.
C#:
return myCollection;
 
Hi Cags,

thank you very very very much..you have really helped me alot..

i seems the value is going in properly..

but i try to retrieve it, but im not sure im retrieving it the correct way..

the web service seems to show page cant be displayed after executing this
statements below..
C#:
foreach (CAnalysisFieldCollection CAF in CAnalysisNames)
  {
	double id = CAF[0].id;
	string fullname = CAF[0].fullname;
  }
How do i actually loop back the items in CAnalysisNames?

thamk you alot again..
 
Last edited by a moderator:
You can access items from a CAnalysisFieldCollection object using the following code.... (myCollection is a CAnalysisFieldCollection object).
C#:
foreach(CAnalysisField field in myCollection)
{
double id = field.id;
string fullname = field.fullname;
}
 
Cags,

U r just greatttttt...thank you sooo much for guiding me through up to the solutions..thank you very very much..u r a great programmer..
I reallly appreciate it..thanks a millioonnn cags..thankss
 
I know this has already been resolved, but for the .Net 2.0 folks out there, a generic list is an even better solution (in my opinion) as there is no coding required to create a stronly typed, dynamic list.

C#:
using Sytem.Collections.Generics;

// ...

public RuleDbDataTypes.CAnalysisField[] processAnalysesNames()
{

RuleDbDataTypes.CAnalysisField[] AnalysisNames=null;int i=0;
OracleConnection con = databaseLogon(“x”,”x”,”x”);
OracleDataReader reader = processORA(con,constructSQL(ConfigurationSettings.AppSettings["getAnalysesNames"]));

List<CAnalysisField> AnalysisNames = new List<CAnalysisField>(); // And now you have a stronly typed list.

 // ...

}
 
Hi mskeel,
thanks for pointing out another solution..seems simplier for .net2.0..thankss..
 
Back
Top