Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic Collection &Method Problem

Generic Collection &Method Problem

Scheduled Pinned Locked Moved C#
helptutorialcsharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haz13
    wrote on last edited by
    #1

    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

    L C 2 Replies Last reply
    0
    • H haz13

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • L Lost User

        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

        H Offline
        H Offline
        haz13
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • H haz13

          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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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 )

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups