locked
set, get in T<list> RRS feed

  • Question

  • Hello, i am from Poland so please don't use sophisticated language.

    I am making poker game. I have t<list> witch name is karta (eng. card), have 2 values wartość, symbol ( eng. value, symbol).

    My function which check what we have ( one pair, strit, two pair etc...) need to sort t<list> what i have done. But i can do it only on cards in my hand, so I want to make copy of cards, but i dont know how to set set in my t<list> 

     class karta
        {
            int wartosc;
            string symbol;
    
            public int Wartosc
            {
                get { return wartosc; }
                
            }
    
            public string Symbol
            {
                get { return symbol; }
            }

    how to write code to set to do somethink like this:

                List<karta> kopia = new List<karta>();
    
                for(int i=0; i<5; i++)
                {
                    kopia[i].Wartosc = karty_posiadane[i].Wartosc;
                    kopia[i].Symbol = karty_posiadane[i].Symbol;
                }

    karty_posiadane = cards in my hand 

    kopia should by copy of karty_posiadane


    Piotr Robak

    Monday, April 4, 2016 10:49 AM

All replies

  • Hi,

    If you want a copy of the list you can do like this:

                List<karta> karty_posiadane = new List<karta>();
                List<karta> kopia = new List<karta>();
    
                kopia = karty_posiadane;

    Monday, April 4, 2016 10:54 AM
  • Hi,

    Your karta class needs some way of setting its properties. Typically this would done using a constructor:

    class karta
    {
            int wartosc;
            string symbol;
    
            public int Wartosc
            {
                get { return wartosc; }
                
            }
    
            public string Symbol
            {
                get { return symbol; }
            }
    
    
           public karta(int wartosc, string symbol)
           {
              this.wartosc = wartosc;
              this.symbol = symbol;
           }
    
    }

    When you want to create a copy of an existing karta, you can then use this constructor:

    for(int i=0; i<5; i++)
    {
       kopia.Add(new karta(karty_posiadane[i].Wartosc,
                           karty_posiadane[i].Symbol));
    }

    Monday, April 4, 2016 10:57 AM
  • I think i have solved problem but i am not sure, is it good ?

            public int Wartosc
            {
                get { return wartosc; }
                set {}
            }
    
            public string Symbol
            {
                get { return symbol; }
                set { }
            }
    Good?



    Piotr Robak

    Monday, April 4, 2016 10:58 AM
  • Your set methods are not doing anything!

    See my previous post.

    Monday, April 4, 2016 10:59 AM
  • Try this too:

        List<karta> kopia = new List<karta>(karty_posiadane);

    Or

        List<karta> kopia = karty_posiadane.Take(5).ToList();

    The loop is not needed. Sorting kopia will not affect karty_posiadane.

    • Edited by Viorel_MVP Monday, April 4, 2016 11:05 AM
    Monday, April 4, 2016 11:01 AM
  • Try this too:

        List<karta> kopia = new List<karta>(karty_posiadane);

    The loop is not needed. Sorting kopia will not affect karty_posiadane.

    Oh yes. Just making a copy of the list for sorting would work without needing to make copies of each card.

    Should have realised that by the time I got to the end of my code example. Doh!

    Monday, April 4, 2016 11:05 AM
  • Yes, but i have to write code to set section. In karta class
    I wrote an answer look below and tell me, is it correct ?

    Piotr Robak

    Monday, April 4, 2016 11:07 AM
  • Yes, but i have to write code to set section. In karta class
    I wrote an answer look below and tell me, is it correct ?

    Piotr Robak

    @Piotr Robak,

    Where did you write the code? Do you mean the last reply?

    As RJP1973 says, your set methods are not doing anything!

    Here is Properties (C# Programming Guide), please have a look. Note the sample, please.

    Best regards,

    kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 5, 2016 8:25 AM