简体   繁体   中英

How can i access the properties of a returned object in c#

In the code showen bellow "Games" is an "Ilist".(Ilist of Games). "Home" is property of Game class that returns "team" - which is again another class that has property of Name. Name returns string. so if i want to set Name to some string, how do i access the property of Name? i tried Game.Home.Name = "string" but does not work

  //public team Home{get; set;}

public class InsertGameCommand : DatabaseCommand
{
    public DateTime Date { get; set; }
    public string HomeTeamId { get; set; }
    public string VisitorTeamId { get; set; }
    public int HomeScore { get; set; }
    public int VisitorScore { get; set; }
    public string ParkId { get; set; }
    public int Attendance { get; set; }

    public override void Execute(Database db)
    {
        // could not access parkId and Home
        db.Games.Add (new Game 
                         {
                             Date = Date, 
                             Home = HomeTeamId, 
                             Visitor = VisitorTeamId, 
                             HomeScore = HomeScore, 
                             VisitorScore = VisitorScore, 
                             Park = ParkId, 
                             Attendance = Attendance
                         }
                     );
        }
    }
}

// game class
public class Game
{
    public DateTime Date { get; set; }
    public Park Park { get; set; }
    // home class
    public Team Home { get; set; }

}


//team class
public class Team
{
    public League League { get; set; }
    public string Name { get; set; }
} 

First things first

First of all, this is horrible confusing code.

Avoid renaming things like this for readable code:

public Team Home { get; set; }

The issue

There are multiple errors in your code (NullReference, CastingError and for sure a misunderstanding how an database works) for adding a Game:

db.Games.Add (new Game 
                     {
                         Date = Date, 
                         Home = HomeTeamId, // <-- Exception (string to Team class)
                         Visitor = VisitorTeamId, // <-- Property doesn't exists on Game class
                         HomeScore = HomeScore, // <--- Also not exists
                         VisitorScore = VisitorScore, // <--- missing
                         Park = ParkId, // <-- Exception (string to unknown class)
                         Attendance = Attendance // also not defined in your 'Game' Class
                     }
                 );

The solution

How to unravel this spagetti code? Simple with clean coding:

Game Class

Make sure all required Properties are in your class defined (for access) and initialized (preventing NullReference Errors) :

public class Game
{
    public Game(string homeTeamId, string visitorTeamId)
    {
        // Default Constructor
        GamePark = new Park();
        HomeTeam = new Team(homeTeamId);
        VisitorTeam = new Team(visitorTeamId);
    }

    // Reference Objects
    public DateTime Date { get; set; }
    public Park GamePark { get; set; }
    public Team HomeTeam { get; set; }
    public Team VisitorTeam { get; set; }

    // Value Objects
    public int HomeScore { get; set; }
    public int VisitorScore { get; set; }
    public string ParkId { get; set; }
    public int Attendance { get; set; }
}

Team Class

I'm not sure whats the purpose of this class is, but it needs definitely an Id Property according to the rest of your mess.

public class Team
{
    public Team(string teamId)
    {
        TeamId = teamId;
        TeamsLeague = new League();
    }

    public string TeamId { get; set; }
    public League TeamsLeague { get; set; }
    public string Name { get; set; }
}

Creating an Game Object from refactoring above

Simply calling the constructor and passing the 2 values for the Teams:

public void Example()
{
    Game game = new Game("Team1","Team2");
    db.Games.Add(game);
    ...
}

I hope this basic construct guides you in the right way. Now everything should be accessible and writable. You even can extend your classes constructors for setting more parameters (like the scores).


My Recommendation

Pick up some tutorials or look for a friend who can teach you how to code. There are clearly a lot of misunderstandings of programming right now in your mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM