Generic Collection &Method Problem
-
I am really stuck trying work out and understand how to achieve the following. I have a collection of objects and want to peform some data analysis on each of the fields in turn using a method. The collection
List<CardData> CardDataCollection = new List<CardData>();
CardData class contains fields called Card1,Card2,Card3 etc The code below has been simplified to show what I'm currently doing for one of the collection object fields (CardDataCollection[i].Card1
).public static void CalcData() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card1 == 1) { //do data processing here } } }
I need to perform the same exercise on each of the Card fields (for Card1 upto Card100) in the collection so I want to create a method that will allow me pass in some kind of reference to the collection object field (
CardX
). Note: I need access using the [i] as the data processing requires me to use for example [i-5] (i.e can't use foreach loops) I know what I want it do but just can quite get my head around how to do it. I'll try to explian below how I would change the code.public static void CalcData(reference to relevant card number field eg Card50) { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (Card50\[i\] == 1) //I know this can't be done { //do data processing here } } }
Please excuse any incorrect technical description as I'm only new to this C#. Thanks in advance for any help you can give me.
Haz
-
I am really stuck trying work out and understand how to achieve the following. I have a collection of objects and want to peform some data analysis on each of the fields in turn using a method. The collection
List<CardData> CardDataCollection = new List<CardData>();
CardData class contains fields called Card1,Card2,Card3 etc The code below has been simplified to show what I'm currently doing for one of the collection object fields (CardDataCollection[i].Card1
).public static void CalcData() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card1 == 1) { //do data processing here } } }
I need to perform the same exercise on each of the Card fields (for Card1 upto Card100) in the collection so I want to create a method that will allow me pass in some kind of reference to the collection object field (
CardX
). Note: I need access using the [i] as the data processing requires me to use for example [i-5] (i.e can't use foreach loops) I know what I want it do but just can quite get my head around how to do it. I'll try to explian below how I would change the code.public static void CalcData(reference to relevant card number field eg Card50) { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (Card50\[i\] == 1) //I know this can't be done { //do data processing here } } }
Please excuse any incorrect technical description as I'm only new to this C#. Thanks in advance for any help you can give me.
Haz
-
haz13 wrote:
I need to perform the same exercise on each of the Card fields (for Card1 upto Card100)
Why don't you use a card array of size 100? regards
Sorry Greeeg I don't understand how that would help what I'm doing (please explain more - maybe I haven't explined it very well)? Basically I need to itterate through all the collection object fields in turn. I have a method that does it for one of the fields, but instead of copying it 100 times and slightly changing the field refernces I was trying to create a method that was more generic (this is the bit I can't get my head around). CardDataCollection[0].Card1 CardDataCollection[1].Card1 CardDataCollection[2].Card1 CardDataCollection[3].Card1 CardDataCollection[4].Card1 CardDataCollection[5].Card1 to end of collection Then I need to do the same for the Card2 field And so on for each of the remaining Card fields upto 100 CardDataCollection[0].Card100 CardDataCollection[1].Card100 CardDataCollection[2].Card100 CardDataCollection[3].Card100 CardDataCollection[4].Card100 CardDataCollection[5].Card100 to end of collection Put another way I need one method to prevent me creating 100 very similar methods as described below.
public static void CalcDataCard1() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card1 == 1) { //do data processing here } } } public static void CalcDataCard2() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card2 == 1) { //do data processing here } } } public static void CalcDataCard3() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card3 == 1) { //do data processing here } } }
Thanks again for any help.
Haz
-
I am really stuck trying work out and understand how to achieve the following. I have a collection of objects and want to peform some data analysis on each of the fields in turn using a method. The collection
List<CardData> CardDataCollection = new List<CardData>();
CardData class contains fields called Card1,Card2,Card3 etc The code below has been simplified to show what I'm currently doing for one of the collection object fields (CardDataCollection[i].Card1
).public static void CalcData() { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (CardDataCollection\[i\].Card1 == 1) { //do data processing here } } }
I need to perform the same exercise on each of the Card fields (for Card1 upto Card100) in the collection so I want to create a method that will allow me pass in some kind of reference to the collection object field (
CardX
). Note: I need access using the [i] as the data processing requires me to use for example [i-5] (i.e can't use foreach loops) I know what I want it do but just can quite get my head around how to do it. I'll try to explian below how I would change the code.public static void CalcData(reference to relevant card number field eg Card50) { int i; for (i = 0; i < CardDataCollection.Count; i++) { if (Card50\[i\] == 1) //I know this can't be done { //do data processing here } } }
Please excuse any incorrect technical description as I'm only new to this C#. Thanks in advance for any help you can give me.
Haz
haz13 wrote:
(for Card1 upto Card100)
As someone else said, instead of having 100 fields, have a collection which contains them ( and if they are named card1-card100, an array makes sense, you just put the number in the indexer ). Then you can iterate over them with foreach to do your test.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )