Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,725 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,345 people online right now. Registration is fast and FREE... Join Now!




List<> problems

 
Reply to this topicStart new topic

List<> problems, Problems refering to class

maffelu
post 11 Oct, 2008 - 08:11 PM
Post #1


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 29


My Contributions


Hello!
I've used list<> for a while, but now I've stumbled upon a problem where I'm blind. For some reason I can't get this to work. I've created a class, Players, and I want to add players to a list. The list is supposed to hold football players, thus it contains name, shirtnumber, date of birth etc.
But after I've entered the players to the list, I can't show the list, here's the code:


The class:
CODE
public class Players
    {
        string name;
        string position;
        string motherclub;
        string nationality;
        int birthyear;
        int number;
        
        public Players(string name, string position, string motherclub, string nationality, int birthyear, int number)
        {
            this.name = name;
            this.position = position;
            this.motherclub = motherclub;
            this.nationality = nationality;
            this.birthyear = birthyear;
            this.number = number;
            
        }
    }


The list:
CODE
class Program
    {
        public static void Main(string[] args)
        {

            
            List<Players> play = new List<Players>();
            
            play.Add(new Players("Dusan Melichárek", "Mĺlvakt", "FC TIC", "Tjeckien", 1983, 40));
            
            play.ForEach(delegate(Players S)
                         {
                             Console.WriteLine("{0}, {1}", //<--HERE! When I type S, I can't find anything!
                         }


Now, when I use the list.ForEach method, I can't select the parts of the list that I wan't to output (like S.name or S.position). I can't understand why, but I bet it's something simple and stupid smile.gif

Can anyone see what I've done wrong?
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 11 Oct, 2008 - 08:22 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,008



Thanked 170 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well that is because you haven't added any properties or made any variables public in your Players class.

Try adding this to your players class...

csharp

// Property called "Name"
public string Name
{
get { return name; }
}


Then again press your "S" and suddenly you will see the Name property appear. Keep in mind that your variable are also set to private by default here so that is why you are not seeing them either. However, good design says you should expose your variables through properties and not make your members public unless you absolutely need to.

Once you expose methods/properties in your Players class, then you will have options when you press "S" in your function.

"At DIC we be name exposing and property abusing code ninjas... we also abuse n00bs by placing them in a burlap bag and beating them with reeds... pretty standard really." decap.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 11 Oct, 2008 - 10:37 PM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,909



Thanked 116 times

Dream Kudos: 8450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


You could also add an Indexer that would give you access to all the properties of your class

csharp

#region Indexer
/// <summary>
/// Indexer for our collection
/// </summary>
/// <param name="index">Index of the collection</param>
/// <returns>Listing at that index</returns>
public Players this[int index]
{
get { return (Players)this.InnerList[index]; }
}
#endregion


Of course this would be in a class such a PlayersCollection which wouod inherit from the abstract class CollectionBase. Doing it this way and you have a strongly typed list smile.gif
User is offlineProfile CardPM

Go to the top of the page

maffelu
post 11 Oct, 2008 - 10:37 PM
Post #4


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 29


My Contributions


Well, that's weird, cause this works, and it's basically the same:

The class:
CODE

public class Person
    {
        public int age;
        public string name;
        public Person(int age, string name)
        {
            this.age = age;
            this.name = name;
        }
    }


The list:
CODE

List<Person> people = new List<Person>();
            
            people.Add(new Person(24, "Magnus"));
            people.Add(new Person(32, "Ida"));
            
                                  people.ForEach(delegate(Person P)
                                                 {
                                                     Console.WriteLine(string.Format("{0}, {1}", P.name, P.age));
                                                 });
                                                 Console.Read();


I don't see the differance.
User is offlineProfile CardPM

Go to the top of the page

maffelu
post 11 Oct, 2008 - 11:25 PM
Post #5


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 29


My Contributions


[SOLVED]

As I stated in the original question, there has to be something stupid at work here, and there is. I have not created public properties. Now I have, and it works =)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:20AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month