C#创建新的类属性……事物(我是新的..非常新) [英] C# Create new class properties ... things (I'm new.. very new)

查看:69
本文介绍了C#创建新的类属性……事物(我是新的..非常新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为Songs的类.它包含一些变量:名称",全名"和目录".

表单上的想法是让用户使用OpenFileDialog选择.wav文件,以将歌曲添加到列表框中的列表中.每次用户输入文件时,它都应该创建一个新的Songs(类)东西.
因此,他们输入的歌曲将附有3条信息,因为这是一类歌曲"(对不起,如果我说不通,请纠正我,如果我做错了!).

列表中的每个项目都将具有:
name-从FileName中提取的列表上显示的名称,例如song.wav
fullname-全名,包括目录,例如C:\ Users \ Joel \ Music \ song.wav
目录-仅文件名目录,例如C:\ Users \ Joel \ Music

好的,我该怎么做?

我已经设置了类,但是我不知道如何为输入到每首歌曲列表中的每个项目创建Songs类的新版本!
用户将看到listBox,就像每个没有附加目录的名称"属性的列表一样.
就像song1.wav song2.wav song3.wav ...
但是当他们双击它时,我希望程序读取目录以查找文件.
因此,当您双击列表框中的歌曲时,它将读取属性全名"以查找文件.

我的问题是:
我以前使用过类,但是我总是不得不在程序开始之前声明类的内容,例如"Static Monster example"(Monster是类名)
但是我总是必须一开始就声明它们.但是对于这个新程序,我无法做到这一点,因为我需要它为用户选择的每首歌曲创建一个新曲.

有什么想法吗?
我为这样的废话而感到抱歉,并没有很好地解释事情,您可能根本听不懂.但是我确实需要帮助-如果需要的话,我会改写,谢谢

I have created a class called Songs. It contains some variables called: "name","fullname" and "directory".

The idea on the Form is for a user to use OpenFileDialog to select a .wav file to add the song to a list in the a listbox. Everytime the user enters a file, it should create a new Songs (the class) thing.
So the song they input will have 3 informations attached because it''s a class Songs (Sorry If I''m not making sense, correct me if I''m wrong!).

Each item on the list will have:
name - The name shown on the list extracted from the FileName e.g. song.wav
fullname - The full name including directory e.g. C:\Users\Joel\Music\song.wav
directory - Just the file name directory e.g. C:\Users\Joel\Music

Ok, how do I do this?

I have the class set up but I don''t know how to create a new version of the Songs class for each item input to the list for each song!
The user will see the listBox like a list of the "name" property for each one without the directory attached.
like song1.wav song2.wav song3.wav ...
but when they double click it I want the program to read the directory to find the file.
So when you double click the song on the listbox it will read the property "fullname" to find the file.

The problem i have is:
I have used classes before but I always had to declare the class thing before at the start of the program like "Static Monster example" (Monster is class name)
but I always had to declare these at the start. But for this new program I can''t do that because I need it to create a new one for each song the user chooses.

Any ideas?
I''m sorry for such a crap post and badly explained things and you might not understand any of it at all. But I really need help with this - I''ll rephrase if you want, thanks

推荐答案

Joel,

我觉得您对类型和对象(实例)和引用一无所知.类Song将是类型而不是对象.根据命名约定,它不应为复数.该名称下的类只有一个,但是可以创建许多实例.您不会创建新的歌曲(类)". LanFanNinja在代码中实例为song时的示例.现在,这是一个变量名,它实际上是对实际对象的引用,而类型是引用类型.这意味着如果您做了
Joel,

I feel you have a mess about types and objects (instances), and references. The class Song would be a type and not an object.the It should not be plural, according to the naming conventions. The class under this name is only one, but many instances can be created. You don''t create "a new Songs (a class) thing". An example if the instance is song in the code by LanFanNinja. Now, this is a variable name, which is actually the reference to the actual object, and the type is the reference type. It means that if you did
Song song1 = new Song("first");
Song song2 = song1;

您将有两个引用,它们仅引用使用构造函数创建的一个对象.

如果您以自然(而不是装箱)形式复制整数对象,则将有两个不同的对象.如果更改其中一个,则另一个将保持不变.对于参考对象,如果更改了song2,则也会在song1中观察到此更改,因为song2song1的工作方式相同.您将这两个变量视为同一事物的名称.

—SA

you would have two references referencing only one object created using the constructor.

If you copied, say, integer object in its natural (not boxed) form, you would have two different objects. If you change one of them, another one would remain the same. With reference objects, if you changed song2 you would observe this change in song1 as well, because song2 and song1 work like the same object; you consider these two variables as the names of the same thing.

—SA


这是我为您提供的最新解决方案,希望它能为您提供更多的解决方案.

songsListBox包含使用addSongsButton添加的歌曲,然后可以使用addToPlaylistButton选择这些歌曲并将其发送到playListBox.

我相信您可以找出其余的所有更改,但是如果不只是询问,我会为您提供帮助.

注意:同样,此代码没有经过严格的测试,因此如果您遇到任何奇怪的问题,请告诉我,我本人或此处的其他人将尽力为您提供帮助.

让我知道如何为您解决问题. :)

Here is my latest solution for you hopefully it is more what you are looking for.

The songsListBox holds the songs you add using the addSongsButton theses songs can then be selected and sent to the playListBox using the addToPlaylistButton.

I am sure you can figure out the rest of the changes but if not just ask and I will help you.

NOTE: Again this code was not heavily tested so if you encounter any strange problems let me know and myself or someone else on here will try to help you.

Let me know how it works out for you. :)

public partial class Form1 : Form
{
    SoundPlayer player;
    Dictionary<string, Song> songs;
    Song selectedSong = null;

    public Form1()
    {
        InitializeComponent();

        songs = new Dictionary<string, Song>();

        // this allows you to select multiple songs from the 
        // songsListBox to add to the playListBox by holding
        // down the Ctrl or Shift key.
        songsListBox.SelectionMode = SelectionMode.MultiExtended;

        //allows multiple files to be selected
        // by holding down the Ctrl or Shift key.
        openSongDialog.Multiselect = true;
    }

    //add songs to the songsListBox
    private void addSongsButton_Click(object sender, EventArgs e)
    {
        if (openSongDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            for (int i = 0; i < openSongDialog.FileNames.Length; i++)
            {
                Song song = new Song(openSongDialog.FileNames[i]);
                songs.Add(song.Name, song);
                songsListBox.Items.Add(song.Name);
            }
        }
    }

    //removes selected songs from the songsListBox and also the playListBox
    private void removeSongsButton_Click(object sender, EventArgs e)
    {
        if (selectedSong != null && songsListBox.SelectedItems.Contains(selectedSong.Name))
        {
            StopPlayback();
            selectedSong = null;
        }

        for (int i = songsListBox.SelectedItems.Count; i > 0; i--)
        {
            string selectedSongName = songsListBox.SelectedItems[i - 1].ToString();

            songsListBox.Items.Remove(selectedSongName);
            playListBox.Items.Remove(selectedSongName);
            songs.Remove(selectedSongName);
        }
    }

    private void playListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (playListBox.SelectedItem != null &&
            songs[playListBox.SelectedItem.ToString()] != selectedSong)
        {
            StopPlayback();
            selectedSong = songs[playListBox.SelectedItem.ToString()];
        }
    }

    private void playButton_Click(object sender, EventArgs e)
    {
        if (selectedSong != null)
        {
            player = new SoundPlayer(selectedSong.FullPath);
            player.Play();
        }
    }

    private void stopButton_Click(object sender, EventArgs e)
    {
        StopPlayback();
    }

    private void StopPlayback()
    {
        if (player != null)
        {
            player.Stop();
            player.Dispose();
        }
    }

    //adds songs selected in the songsListBox to the playListBox
    private void addToPlaylistButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < songsListBox.SelectedItems.Count; i++)
        {
            // the if statement checks if playListBox already
            // contains a song with this name before adding it.
            if (!playListBox.Items.Contains(songsListBox.SelectedItems[i]))
            {
                playListBox.Items.Add(songsListBox.SelectedItems[i]);
            }
        }
    }

    //removes selected song from the playListBox
    private void removeFromPlaylistButton_Click(object sender, EventArgs e)
    {
        if (selectedSong != null && selectedSong.Name == playListBox.SelectedItem.ToString())
        {
            StopPlayback();
            selectedSong = null;
        }

        playListBox.Items.Remove(playListBox.SelectedItem);
    }

    //removes all songs from the playListBox
    private void clearPlaylistButton_Click(object sender, EventArgs e)
    {
        if (selectedSong != null)
        {
            StopPlayback();
            selectedSong = null;
        }

        playListBox.Items.Clear();
    }
}

class Song
{
    internal string FullPath { get; private set; }
    internal string Directory { get; private set; }
    internal string Name { get; private set; }

    public Song(string path)
    {
        FullPath = path;
        Directory = System.IO.Path.GetDirectoryName(path);
        Name = System.IO.Path.GetFileNameWithoutExtension(path);
    }
}


类似的方法应该对您有用.如果您有任何疑问,请提问,我会尽力帮助您.

注意:我没有尝试运行此代码,因此它可能包含一个或两个错误.让我知道是否是这种情况,我会帮助您解决.

Something like this should work for you. If you have any questions just ask and I will try to help you.

NOTE: I did not try to run this code so it could contain an error or two. Let me know if this is the case and I will help you figure it out.

class Song
{
    internal string Name { get; private set; }
    internal string FullPath { get; private set; }
    internal string Directory { get; private set; }

    public Song(string path)
    {
        FullPath = path;
        Name = Path.GetFileNameWithoutExtension(path);
        Directory = Path.GetDirectoryName(path);
    } 
}

// * NOTE: openSongDialog is of the type OpenFileDialog *
class Form1 : Form
{
    List<Song> songs = new List<Song>();

    if (openSongDialog.ShowDialog() == DialogResult.OK)
    {
        Song song = new Song(openSongDialog.FileName);
       
        songs.Add(song);
    }
}




考虑了这一点后,我意识到您最好改用Dictionary.

使用字典可以使您使用列表框中的歌曲名称为歌曲建立索引.

这是一个示例:




After thinking about this some more I realized you would probably be better off to use a Dictionary instead.

Using a Dictionary would allow you to index songs using the songs name from the ListBox.

Here is an example:

class Form1 : Form
{
    Dictionary<string, Song> songs = new Dictionary<string, Song>();

    if (openSongDialog.ShowDialog() == DialogResult.OK)
    {
        Song song = new Song(openSongDialog.FileName);
       
        songs.Add(song.Name, song);
    }
}



然后,您可以执行类似的操作以在ListBox中选择歌曲.



Then you could do something like this to get the song selected in the ListBox.

Song selectedSong = songs[songListBox.SelectedItem.ToString()];


这篇关于C#创建新的类属性……事物(我是新的..非常新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆