a1jit Posted June 5, 2006 Posted June 5, 2006 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 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. 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.. Quote
Cags Posted June 5, 2006 Posted June 5, 2006 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? Quote Anybody looking for a graduate programmer (Midlands, England)?
a1jit Posted June 5, 2006 Author Posted June 5, 2006 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 Quote
Cags Posted June 5, 2006 Posted June 5, 2006 The architecture... 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... 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); } Quote Anybody looking for a graduate programmer (Midlands, England)?
a1jit Posted June 5, 2006 Author Posted June 5, 2006 (edited) 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. public RuleDbDataTypes.CAnalysisField[] processAnalysesNames() { RuleDbDataTypes.CAnalysisField[] AnalysisNames=null AnalysisNames = new RuleDbDataTypes.CAnalysisField[]; while (reader...) return CAnalysisField; } thanks a million.. Edited June 26, 2006 by PlausiblyDamp Quote
Cags Posted June 5, 2006 Posted June 5, 2006 The line public class CAnalysisFieldCollection : CollectionBase simply creates a class called CAnalysisFieldCollection which inherits from CollectionBase. A1. CAnalysisFieldCollection myCollection = new CAnalysisFieldCollection; A2. return myCollection; Quote Anybody looking for a graduate programmer (Midlands, England)?
a1jit Posted June 5, 2006 Author Posted June 5, 2006 (edited) 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.. 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.. Edited June 26, 2006 by PlausiblyDamp Quote
Cags Posted June 5, 2006 Posted June 5, 2006 You can access items from a CAnalysisFieldCollection object using the following code.... (myCollection is a CAnalysisFieldCollection object). foreach(CAnalysisField field in myCollection) { double id = field.id; string fullname = field.fullname; } Quote Anybody looking for a graduate programmer (Midlands, England)?
a1jit Posted June 5, 2006 Author Posted June 5, 2006 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 Quote
mskeel Posted June 7, 2006 Posted June 7, 2006 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. 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. // ... } Quote
a1jit Posted June 8, 2006 Author Posted June 8, 2006 Hi mskeel, thanks for pointing out another solution..seems simplier for .net2.0..thankss.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.