Click here to Skip to main content
15,886,199 members
Home / Discussions / C#
   

C#

 
GeneralRe: session & cookie Pin
Arefeh Haghpnah12-Nov-09 21:25
Arefeh Haghpnah12-Nov-09 21:25 
QuestionRestore main window from other process Pin
Daniel Leykauf11-Nov-09 6:14
Daniel Leykauf11-Nov-09 6:14 
AnswerRe: Restore main window from other process Pin
Paulo Zemek11-Nov-09 6:59
mvaPaulo Zemek11-Nov-09 6:59 
GeneralRe: Restore main window from other process Pin
Daniel Leykauf11-Nov-09 7:10
Daniel Leykauf11-Nov-09 7:10 
Question[Message Deleted] Pin
Crapaw4511-Nov-09 5:49
Crapaw4511-Nov-09 5:49 
QuestionActive UICulture in setup rpoject Pin
waelmail10511-Nov-09 5:19
waelmail10511-Nov-09 5:19 
Questioncan't get depth chart to display Pin
rooster215411-Nov-09 4:47
rooster215411-Nov-09 4:47 
AnswerRe: can't get depth chart to display Pin
Keith Barrow11-Nov-09 6:24
professionalKeith Barrow11-Nov-09 6:24 
First, a direct answer to your questions:

Method 1
var depthChart = t.DepthChart;
Console.WriteLine(depthChart);

depthChart will be a List<Player> so the .ToString() method will just return the typename of the depthChart. To get the player roster you need to iterate over the list and get output each player individually. You should put a method in your Team class (NOT in the main method- bad encapsulation).

Method 2
foreach (PlayerPosition pos in Enum.GetValues(typeof(PlayerPosition)))
{
    Console.Write(pos.ToString() + ':');
    depthChart[(int)pos].ForEach(p => Console.Write(' ' + p.lastName));
    Console.WriteLine();
}


Is more complicated. The position of a type of player in the depth chart doesn't correspond to the position in the depthChart list, unless you ONLY add them in the order they appear in the enum. To do so would be very bad design. If (and only if) you have one player per position, you could consider a Dictionary<PlayerPosition, Player> Though even this is messy.


Now for the hard part, I think the overall design of the above is wrong. Some people will disagree with me, others will (hopefully) agree you can wrapper all the following up in <IMO> tags if you like . There is debate about the use of enums. I fall into the camp that they are [mostly] evil. The prescence of enums normally indicates a lack of polymorphism that should be expressed. In your case I think you should have something like the following:


public Interface IPlayer
{
  //....Interface Members here e.g.
  string Surname {get; set;}
  string PositionName { get; }
}

public abstract class Player : IPlayer
{
  public string  Surname {get; set;}
  public string PositionName  { get; protected set; }

  // .... Rest of common implementation here
}

public class QuaterBack : Player
{
  // .... Rest of  Quaterback-specific implementation here
  public QuaterBack()
  {
     PositionName = "Quater Back";
  }
}


public class WideReciever : Player
{
  // .... Rest of  WideReciever-specific implementation here
  public QuaterBack()
  {
     PositionName = "Wide Reciever";
  }
}

//TODO: Other Positional Classes....

public interface ITeam
{
  List<IPlayer> Players {get; }
}
public class Team : ITeam
{
  public List<IPlayer> Players {get; private set}

  public string TeamRoster()
  {
    //This is a really grubby implementation, but is good enough to demonstate....
    string roster = "";
    Players.ForEach( p=> roster += p.PositionName + ": " + p.Surname + " ");
    return roster;
  }

  
  public Team()
  {
    Players = new List<IPlayer>();
  }

}



The big question should now be why bother? Functionality is now encapsulated in the correct place, Player handles whatever the player does,Team will handle team-level stuff. Also, you can add extra properties to Player, for example a Positional Shortcode e.g. ("QB"), in the original implementation you would have to keep a dictionary for such information (and any other information relating to the position. Additionally The above can be made to demonstrate polymorhpism: As an example, let's say only Quaterbacks are allowed to kick off (I don't know enough about American football for a real example), you can add a KickOff method to a Quaterback without implementing it for any of the other player types. Not only that, but long enums often lead to messy switch statements (or long lists of ifs), giving off a rank code-smell as they go.

The code I've given isn't perfect (I haven't thought about it very deeply), but hopefully will provide a starting point. For example, I think there really should be a IPlayerType interface subclassed by PlayerType type inherted per position (e.g. QuaterBack), and the Player should expose a property of IPlayerType, rather than BE a QuaterBack or LineBack or whatever. This will allow the player to change position easily and encapsultes the position properly, against the player.

Take a look at for some started WRT enums:

http://devpinoy.org/blogs/cruizer/archive/2007/09/12/enums-are-evil.aspx[^]

and http://www.colourcoding.net/Blog/archive/2009/07/25/enums-are-evil-the-state-pattern.aspx[^]

One of the problems in describing the reason why you shouldn't use enums is that they work when starting out writing a class (so any explanation of why not to use them seems longer/harder than the original), but as the class develops the shortfalls start to appear.

Hope this helps!

CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks

GeneralRe: can't get depth chart to display Pin
rooster215411-Nov-09 9:37
rooster215411-Nov-09 9:37 
GeneralRe: can't get depth chart to display Pin
Keith Barrow11-Nov-09 13:28
professionalKeith Barrow11-Nov-09 13:28 
QuestionHow to resize two texbox proportionally? Pin
Zeokat11-Nov-09 1:21
Zeokat11-Nov-09 1:21 
AnswerRe: How to resize two texbox proportionally? Pin
Lyon Sun11-Nov-09 2:12
Lyon Sun11-Nov-09 2:12 
GeneralRe: How to resize two texbox proportionally? Pin
Zeokat11-Nov-09 10:04
Zeokat11-Nov-09 10:04 
GeneralRe: How to resize two texbox proportionally? Pin
Zeokat12-Nov-09 0:10
Zeokat12-Nov-09 0:10 
Questionhow to perform image erosion in c#? Pin
Rubasini11-Nov-09 1:04
Rubasini11-Nov-09 1:04 
Questionhow to build remote system call application ? Pin
sudhir behera11-Nov-09 0:24
sudhir behera11-Nov-09 0:24 
AnswerRe: how to build remote system call application ? Pin
Abhishek Sur11-Nov-09 0:45
professionalAbhishek Sur11-Nov-09 0:45 
QuestionCreate radio buttons using data from XML Pin
Zar Ni10-Nov-09 22:59
Zar Ni10-Nov-09 22:59 
AnswerRe: Create radio buttons using data from XML Pin
Calla10-Nov-09 23:31
Calla10-Nov-09 23:31 
QuestionTableLayoutPanel Problem Pin
Tim Daughton10-Nov-09 22:32
Tim Daughton10-Nov-09 22:32 
AnswerMessage Closed Pin
10-Nov-09 22:38
stancrm10-Nov-09 22:38 
GeneralRe: TableLayoutPanel Problem Pin
Tim Daughton10-Nov-09 22:59
Tim Daughton10-Nov-09 22:59 
Questionupdating multiple rows of a single column with different values at runtime from C# Pin
Member 236780010-Nov-09 22:18
Member 236780010-Nov-09 22:18 
AnswerRe: updating multiple rows of a single column with different values at runtime from C# Pin
MumbleB11-Nov-09 0:24
MumbleB11-Nov-09 0:24 
GeneralRe: updating multiple rows of a single column with different values at runtime from C# Pin
Member 236780011-Nov-09 8:15
Member 236780011-Nov-09 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.