Question Problem understanding Classes and lists.

Status
Not open for further replies.

Morry

Member
Joined
Feb 21, 2017
Messages
10
Programming Experience
Beginner
Hi everyone!

I have been using Lists in classes as property but gott in a bit of a closed road now. I wanted to create a program where i have a list of people and the movies they like so..

<unreadable code removed>

Now with the code above i tryed to create a collection where i have multipale movies under one user, and i did it so that if i select a user from the Users list i can klick the button and it would move all the movies from the movie list to that user in the users list.

The problem is iam unable to retain the list of movies to display them in another list, i tryed many things but it didn't work. so now when klick i get the last saved values and i can't really specify anything from the list like a different use with his own movies etc..

So please can anyone tell me what i should use, or even search for in the internet because i don't seem to be able to search for the correct terms or the right things, And couple more questions :joyous:.

1. if i use Combobox to display the users, do they still have the movies attached to them?
2. how can i movie items from a Listbox to a List but one item at the time, i already tried IF, Foreach and they didn't work.

Thank you for your help!
 
I can't even begin to read that code you posted, I could fix it for you or better yet you could edit your post and paste your code in an xcode block, like so: [xcode=c#]Your code here[/xcode]
 
As JB says, your code is very hard to read because it's been posted using the wrong formatting tags. Without looking at the code, I can tell you that your types should look something like this:
public class Movie
{
    // ...
}

public class User
{
    // ...

    private List<Movie> _movies = new List<Movie>();

    public List<Movie> Movies
    {
        get
        {
            return _movies;
        }
    }
}

or, in recent versions, like this:
public class Movie
{
    // ...
}

public class User
{
    // ...

    public List<Movie> Movies { get; } = new List<Movie>();
}

Each User object has a property that is a List<Movie>. If you really want a separate type for the Movie list then you define a type that IS a list, not a type that HAS a list:
public class Movie
{
    // ...
}

public class MovieCollection : System.Collections.ObjectModel.Collection<Movie>
{
    // ...
}

public class User
{
    // ...

    public MovieCollection Movies { get; } = new MovieCollection();
}
Microsoft's guidelines suggest doing that if you're creating a public API, i.e. a library that will be used by other parties, but not if it's all in the same project.
 
I have deleted your most recent post because it was clearly unreadable, so you shouldn't even have submitted it. Did you just copy and paste the already badly formatted code from post #1? As you can see from post #3, if you copy the code as plain text from the IDE and paste it into the appropriate tags, it becomes very readable.
 
I have deleted your most recent post because it was clearly unreadable, so you shouldn't even have submitted it. Did you just copy and paste the already badly formatted code from post #1? As you can see from post #3, if you copy the code as plain text from the IDE and paste it into the appropriate tags, it becomes very readable.

Oh sorry doesnt seem to work for me. I will try to post a clearer code soon.
 
The problem iam getting is that when i save the info about the Users in a list then attach the Items of the listbox(Movies) to that user, when i want to retrive the data, it will show the info about the last User only, so it keep overriding each other.
 
How can i move one item at the time from a listbox into a list, because foreach doesn't work since iam talking all items, any idea? :)
 
The problem iam getting is that when i save the info about the Users in a list then attach the Items of the listbox(Movies) to that user, when i want to retrive the data, it will show the info about the last User only, so it keep overriding each other.

I don't know what you mean. Design your types properly first, then do what you think is required and, if it doesn't work, post back and show us what you did and tell us exactly what happened and how that differs from your expectation. Saying things like "it keep overriding each other" doesn't help because we don't know what "it" is. If you do it right then it will work so you must be doing it wrong. If we can't see what you're doing then we can't see what's wrong with it.
 
How can i move one item at the time from a listbox into a list, because foreach doesn't work since iam talking all items, any idea? :)
Is 'foreach' the only type of loop in C#?
 
I did try for loop too, didn't work or i just didn't set it right. btw i can't reply with Quote because i can't use space for some reason only in this Reply to thread.
 
I did try for loop too, didn't work or i just didn't set it right.

Then maybe the thing to do is to show us what you did and tell us what happened. That said, did you think it through? A for loop usually works by looping from the first index in a list to the last index. If you're removing items though, the last index in the list is going to reduce by 1 each time. That means that you're going to start hitting invalid indexes halfway through the loop. How do you think you could change the loop to alleviate that?
 
Then maybe the thing to do is to show us what you did and tell us what happened. That said, did you think it through? A for loop usually works by looping from the first index in a list to the last index. If you're removing items though, the last index in the list is going to reduce by 1 each time. That means that you're going to start hitting invalid indexes halfway through the loop. How do you think you could change the loop to alleviate that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fotboll
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int Antalspelare = 0;
        List<Player>  playerlist = new List<Player>();
        List<Match> matchlist = new List<Match>();
        List<Team> teamlist = new List<Team>();
        Team chosenteam = new Team();

        private void btnAddplayer_Click(object sender, EventArgs e)
        {
            // Add players and display them in the listbox
            Player playerinfo = new Player();
            playerinfo.Firstname = txtboxF?rnamn.Text;
            playerinfo.Surname = txtboxEfternamn.Text;
            playerinfo.Number = int.Parse(txtboxNummer.Text);
            if (Antalspelare < 25)
            {
                Antalspelare++;
                playerlist.Add(playerinfo);
            }
            else
            {
                MessageBox.Show(" You can't put more players! ");
            }

            listboxPlayer.DataSource = null;
            listboxPlayer.DataSource = playerlist;
        }

        private void btnAddteam_Click(object sender, EventArgs e)
        {
            // Add a team and display it into the listbox
            Team teamname = new Team();            
            teamname.Name = txtboxTeam.Text;
            teamlist.Add(teamname);

            listboxTeam.DataSource = teamlist;
            txtboxTeam.Clear();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //Create a match and display it in the listbox
            Match matchsetup = new Match();

           matchsetup.Hometeam = comboHometeam.Text;
           matchsetup.Foreignteam = comboForeignteam.Text;
           matchsetup.Date = timepickerMatch.Value.ToString();

            listboxHometeam.Items.Add(matchsetup.Hometeam);
            listboxForeignteam.Items.Add(matchsetup.Foreignteam);

            matchlist.Add(matchsetup);
            listboxMatch.DataSource = null;
            listboxMatch.DataSource = matchlist;
        }

        private void btnPlayertoteam_Click_1(object sender, EventArgs e)
        {
            //Add players to a team
            Antalspelare = 0;
            chosenteam = (Team)listboxTeam.SelectedItem;
            chosenteam.Name = listboxTeam.SelectedItem.ToString();

            foreach (Player item in listboxPlayer.Items)
            {
                chosenteam.Playerteam.Add(item);
            }

            comboForeignteam.Items.Add(chosenteam.Name);
            comboHometeam.Items.Add(chosenteam.Name);

            playerlist.Clear();

            listboxPlayer.DataSource = null;
        }

        private void btnHometeam_Click(object sender, EventArgs e)
        {
        }

        private void btnForeignteam_Click(object sender, EventArgs e)
        {
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fotboll
{
    public class Player 
    {
        public int Warning { get; set; }
        public string Surname { get; set; }
        public string Firstname { get; set; }
        public int Number { get; set; }

        public Player()
        {
        }
        
        public override string ToString()
        {
            return Firstname + " " + Surname + " " + Number;
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fotboll
{
    public class Player 
    {
        public int Warning { get; set; }
        public string Surname { get; set; }
        public string Firstname { get; set; }
        public int Number { get; set; }

        public Player()
        {
        }

        public override string ToString()
        {
            return Firstname + " " + Surname + " " + Number;
        }
    }
}
 
Last edited:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fotboll
{
    public class Match 
    {
        public string Date { get; set; }
        public int Stadium { get; set; }
        public string Hometeam { get; set; }
        public string Foreignteam { get; set; }

        public override string ToString()
        {
            return Hometeam + " - " + Foreignteam + Environment.NewLine + "  "+Date;
        }
    }
}
 
Status
Not open for further replies.
Back
Top Bottom