Home > Software engineering >  Accessing property value from an array of objects c#
Accessing property value from an array of objects c#

Time:10-05

I am trying to find the "player" with the highest salary from an array of "BaseBallPlayer" objects. I know I need to loop through the array and compare the salary properties with an "if" statement. However, I cant seem to figure out how to access a specific property from the objects in the array.

here is a snippet of my main():

 static void Main(string[] args)
        {
            BasketBallPlayer basketBP1 = new BasketBallPlayer("Jeff", 24, 30000.00, 12.2, 6, 7, 8);
            BasketBallPlayer basketBP2 = new BasketBallPlayer("Jim", 27, 35000, 18, 5, 17, 9);
            BasketBallPlayer basketBP3 = new BasketBallPlayer("James", 32, 65000, 34, 87, 15, 12);

            object[] BasketBT = new object[] {basketBP1, basketBP2, basketBP3 };

            BaseBallPlayer baseBP1 = new BaseBallPlayer("Craig", 26, 53000, 53, 12, 9);
            BaseBallPlayer baseBP2 = new BaseBallPlayer("Chris", 35, 66000, 67, 19, 7);
            BaseBallPlayer baseBP3 = new BaseBallPlayer("Charlie", 32, 75000, 87, 23, 4);
            

            object[] BaseBT = new object[] { baseBP1, baseBP2, baseBP3 };

            foreach (object player in BaseBT)
            {
                
            }
                           
        }

here is the parent class of BaseBallPlayer:

    {
        protected int age { get; set; }
        protected string name { get; set; }
        public double salary { get; set; }

        public sportsPlayer(string Name, int Age, double Salary)
        {
            this.name = Name; 
            this.age = Age;
            this.salary = Salary;
        }

        public override string ToString()
        {
            string details = string.Format("Name: {0} \n Age: {1} \n Salary: {2} \n ", this.name, this.age, this.salary);
            return details;
        }
    }

And here is the class BaseBallPlayer:

 class BaseBallPlayer: sportsPlayer
    {
        double battingAverage { get; set; }
        int homeRuns { get; set; }
        int errors { get; set; }

        public BaseBallPlayer(string Name, int Age, double Salary, double BattingAverage, int HomeRuns, int Errors): base(Name, Age, Salary)
        {
            this.battingAverage = BattingAverage;
            this.homeRuns = HomeRuns;
            this.errors = Errors;
        }

        public override string ToString()
        {
            string details = string.Format("Batting Average: {0} \n Home Runs: {1} \n Errors: {2} \n", this.battingAverage, this.homeRuns, this.errors);
            return base.ToString()   details;
        }
    }

CodePudding user response:

In order to access properties in a type, you should declare variables of that type and not the object type, so your code should look like this in order to be able to access player.salary :

 static void Main(string[] args)
    {
        BasketBallPlayer basketBP1 = new BasketBallPlayer("Jeff", 24, 30000.00, 12.2, 6, 7, 8);
        BasketBallPlayer basketBP2 = new BasketBallPlayer("Jim", 27, 35000, 18, 5, 17, 9);
        BasketBallPlayer basketBP3 = new BasketBallPlayer("James", 32, 65000, 34, 87, 15, 12);

        sportsPlayer[] BasketBT = new object[] {basketBP1, basketBP2, basketBP3 };

        BaseBallPlayer baseBP1 = new BaseBallPlayer("Craig", 26, 53000, 53, 12, 9);
        BaseBallPlayer baseBP2 = new BaseBallPlayer("Chris", 35, 66000, 67, 19, 7);
        BaseBallPlayer baseBP3 = new BaseBallPlayer("Charlie", 32, 75000, 87, 23, 4);
        

        sportsPlayer[] BaseBT = new object[] { baseBP1, baseBP2, baseBP3 };

        foreach (sportsPlayer player in BaseBT)
        {
            // here you can access player.salary
        }
                       
    }

CodePudding user response:

you have to fix a type of BaseBT as a BaseBallPlayer[]

BaseBallPlayer[] BaseBT = new BaseBallPlayer[] { baseBP1, baseBP2, baseBP3 };

player with max salary

var playerMaxSalary=  BaseBT.OrderByDescending(b=>b.salary).FirstOrDefault();

result

Name: Charlie
 Age: 32
 Salary: 75000
 Batting Average: 87
 Home Runs: 23
 Errors: 4

test

    foreach (var player in BaseBT)
    {
        Console.WriteLine(player.ToString());
    }

result:

Name: Craig
 Age: 26
 Salary: 53000
 Batting Average: 53
 Home Runs: 12
 Errors: 9

Name: Chris
 Age: 35
 Salary: 66000
 Batting Average: 67
 Home Runs: 19
 Errors: 7

Name: Charlie
 Age: 32
 Salary: 75000
 Batting Average: 87
 Home Runs: 23
 Errors: 4

you will have the same reslt if you define BaseBT as a List of BaseBallPlayer

List<BaseBallPlayer> BaseBT = new List<BaseBallPlayer> { baseBP1, baseBP2, baseBP3};
  • Related