Home > Software engineering >  How do i access specific list object from a function? C#
How do i access specific list object from a function? C#

Time:10-21

Im new to programming and this might be some rookie problem that im having, but id appreaciate a hand.

            void Sort()
            {

                List<Lag> lagen = new List<Lag>() { AIK, DIF, MLM, BJK };

                List<Lag> sorted = lagen.OrderByDescending(x => x.poäng)
                                                .ThenByDescending(x => x.målSK)
                                                .ThenByDescending(x => x.mål)
                                                .ToList();
                Print(sorted);
            }

This is my sorting function, where i take one list, and turn it into a list called "sorted". What i want to do now is let the user pick one of the objects in the sorted list by entering a number. This far iw written the following code. Where int hemlag, and int bortlag are objects in the sorted list.

And now i want to change the value of "Lag hemmalag" & "Lag bortalag" depending on what numbers (what team) the user put in.

So for example, if the user input "int hemlag" is 2.. i want hemmalag = to be the 2nd object in the list "sorted". And now i run into a problem. Cause i cant access that sorted list from this function.

My theories are that it might have to do something with returning that list from the sorting function, but i have not yet found a way to do that...

            void ChangeStats(int hemlag, int bortlag, int mål, int insläpp)
            {

                Sortera();

                Lag hemmalag = AIK;
                Lag bortalag = AIK;

                if (hemlag == 1) { ; }
                if (hemlag == 2) { hemmalag = DIF; }
                if (hemlag == 3) { hemmalag = MLM; }
                if (hemlag == 4) { hemmalag = BJK; }


                if (bortlag == 1) { bortalag = AIK; }
                if (bortlag == 2) { bortalag = DIF; }
                if (bortlag == 3) { bortalag = MLM; }
                if (bortlag == 4) { bortalag = BJK; }

                hemmalag.mål  = mål;
                hemmalag.insläppta  = insläpp;

                bortalag.insläppta  = mål;
                bortalag.mål  = insläpp;

                if (mål > insläpp)
                {
                    hemmalag.poäng  = 3;
                    hemmalag.vinster  ;
                    hemmalag.spel  ;

                    bortalag.förlorade  ;
                    bortalag.spel  ;

                }

                if (mål < insläpp)
                {
                    bortalag.poäng  = 3;
                    bortalag.vinster  ;
                    bortalag.spel  ;

                    hemmalag.förlorade  ;
                    hemmalag.spel  ;

                }
                if (mål == insläpp)
                {
                    bortalag.lika  ;
                    bortalag.poäng  ;

                    hemmalag.lika  ;
                    bortalag.poäng  ;
                }
                Console.WriteLine("Stats changed");
                Console.WriteLine("---");
                Save();
                Sortera();
            
            }

Help appreciated, cheers!

CodePudding user response:

A good practice when programming is to try to ensure that a function has a specific purpose, and only does that specific thing. In your case your Sort-function actually does three things, create the list, sort it, and print it. So lets rewrite your sort-function to return the sorted value:

List<Lag> Sort(IEnumerable<Lag> lagen)
{
    return lagen.OrderByDescending(x => x.poäng)
            .ThenByDescending(x => x.målSK)
            .ThenByDescending(x => x.mål)
            .ToList();
}

This uses the IEnumerable<Lag> to accept any kind of sequence of Lag, List, array, HashSet etc. It helps make the code more flexible to accept a wider type of arguments.

Assuming you got the printing and user input correct, the change stats function should probably look something like:

List<Lag> ChangeStats(List<Lag> lagen, int hemlagIndex, int bortlagIndex, int mål, int insläpp){

    var hemlag = lagen[hemlagIndex];
    var bortalag = lagen[bortlagIndex];
    
    // Do the stat-update logic
    ...

    return lagen
}

You should probably also make your safe-method take a sequence of Lag as input, and move sorting and saving outside the ChangeStats method. Again try to make sure each method has a specific purpose.

These examples only uses method parameters for all the data. This is often a good thing since it makes it more obvious what data the method is using. But in some cases it might be preferable to use a field in the class instead, something like:

public class MinaLag{
    private List<Lag> lagen = new List<Lag>(){ AIK, DIF, MLM, BJK };
    public void Sort(){
        lagen = lagen.OrderBy(...);
    }
    public void ChangeStats(int hemlagIndex, int bortlagIndex, int mål, int insläpp){
       var hemlag = lagen[hemlagIndex];
       var bortalag = lagen[bortlagIndex];
       ...
    }
    public void Print(){...}
    public void Save(Stream s){...}
    public static MinLag Load(Stream s){...}
}

This wraps a list of the objects and provides methods to do all the required operations on them, removing the need for the user give and receive the list for each called method.

CodePudding user response:

Here is my example on the global scope list. I'm not 100% this can sort the issue but I'm confident.

class Example {
  private List<Lag> Lagen {
    get;
    set;
  }  // Global Scope - Make it public if you need to access it from another
     // class.

  public Example() {
    this.Lagen = new List<Lag>{AIK, DIF, MLM,
                               BJK};  // Assign intial values on class execution
  }
  void Sort() {
    // Everything else will be the same but now you can access it from anywhere
    // within the class
    List<Lag> sorted = Lagen.OrderByDescending(x => x.poäng)
                           .ThenByDescending(x => x.målSK)
                           .ThenByDescending(x => x.mål)
                           .ToList();
    Print(sorted);
  }
}

CodePudding user response:

your description is very convoluted and I'm not fully getting what you're up to, but...

You can return the sorted list from the Sort() function by changing the return type from void to List and at its bottom just having line like return sorted

You can also consider leaving void as return type but passing original list to it and turning it to a sorted one inside. List is an object so you'll get it back in the calling function and can further work with it there.

CodePudding user response:

   private List<Lag> lagen = new List<Lag>() { AIK, DIF, MLM, BJK };
        private void Sortera()
        {
            lagen = lagen.OrderByDescending(x => x.poäng)
                                            .ThenByDescending(x => x.målSK)
                                            .ThenByDescending(x => x.mål)
                                            .ToList();
            Print(lagen);
        }
        private void ChangeStats(int hemlag, int bortlag, int mål, int insläpp)
        {
            Sortera();

            Lag hemmalag = AIK;
            Lag bortalag = AIK;

            if (hemlag == 1) {; }
            if (hemlag == 2) { lagen[1] = AIK }
            if (hemlag == 3) { lagen[2] = MLM; }
            if (hemlag == 4) { lagen[3] = BJK; }

            if (bortlag == 1) { lagen[0] = AIK; }
            if (bortlag == 2) { lagen[1] = DIF; }
            if (bortlag == 3) { lagen[2] = MLM; }
            if (bortlag == 4) { lagen[3] = BJK; }
         etc.....

I'm still not sure what the rest of the solution means but this way you can change your list items

  • Related