枚举已定义但未在类中找到 [英] Enum is defined but not found in the class

查看:64
本文介绍了枚举已定义但未在类中找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该解决方案包含三个类:SongGenre,Song和Library(+程序)。我只是按照说明进行操作,因此大多数编码都来自我的讲座和书,而没有太多的经验。这就是您所看到的,我并不为此感到骄傲。指针真的很感激。主要的原因是为什么枚举值不能在其他类中看到?

此代码已修复(请参见注释)。

The solution consists of three classes: the SongGenre, the Song and the Library (+ Program). I am just following the instructions so most of coding comes from my lectures and the book and not much of the experience. It is what what you see and I am not really proud of it. Pointers are really appreciated. The main one is why the enum values can not be seen in another classes?
This code has been fixed (see comments).

   namespace SongLibrary
   {      
      [Flags]
      enum SongGenre
      {            
          Unclassified = 0,
          Pop = 0b1,
          Rock = 0b10,
          Blues = 0b100,
          Country = 0b1_000,
          Metal = 0b10_000,
          Soul = 0b100_000
       }
    }
  


namespace SongLibrary
{
    /*This acts like a record for the song. The setter is missing for all the properties.
     * There are no fields.
     * This class comprise of four auto-implemented properties with public getters and 
     * setters absent. */
    public class Song
    {
        public string Artist { get; }
        public string Title { get; }
        public double Length { get; }
        public SongGenre Genre { get; }

        /*This constructor that takes four arguments and assigns them to the appropriate  properties.*/
        public Song(string title, string artist, double length, SongGenre genre)
        {
            Artist = artist;
            Title = title;
            Length = length;            
            SongGenre Genre = SongGenre.genre;/*<-ERROR 'SongGenre does not contain a definition for 'genre'*/
        }
        public override string ToString()
        {
            return string.Format("[{0} by ,{1} ,({2}) ,{3}min]", Title, Artist, Genre, Length);
        }
     }
  }


   namespace SongLibrary
   {
    public static class Library
    {
        /*This is a static class therefore all the members also have to be static. Class members 
         * are accessed using the type instead of object reference.
         * There are no properties.
         * There is no constructor for this class.
         * There are four over-loaded methods. */

        /*This private field is a list of song object is a class variable.*/
        private static List<string> songs = new List<string> { "title", "artist", "length", "genre" };

        /*This is a public class method that does not take any argument and displays all the songs in 
         * the collection.*/
        public static void DisplaySongs()
        {
            for (int i = 0; i < songs.Count; i++)
                Console.WriteLine(songs[i]);
        }
        /*This is a public class method that takes a double argument and displays only songs that are 
         * longer than the argument.*/
        public static void DisplaySongs(double longerThan)
        {
            foreach (string songs in songs)
            {
                if (songs.Length > longerThan)
                {
                    Console.WriteLine("\n" + songs);
                }
            }
        }
        /*This is a public class method that takes a SongGenre argument and displays only songs that 
         * are of this genre.*/
        public static void DisplaySongs(SongGenre genre)
        {
            foreach (string songs in songs)
            {
                if (songs.Genre == genre)/*<-ERROR 'string' does not contain a definition for 'Genre'
                                          * and no accessable extension method 'Genre' accepting a first
                                          * argument of type 'string' could be found*/
                {
                    Console.WriteLine("\n" + songs);
                }
            }
        }
        /*This is a public class method that takes a string argument and displays only songs by this artist.*/
        public static void DisplaySongs(string artist)
        {
            foreach (string songs in songs)
            {
                if (songs.Artist == artist) /*< -ERROR 'string' does not contain a definition for 'Artist'
                                            * and no accessable extension method 'Artist' accepting a first
                                            * argument of type 'string' could be found */
                {
                    Console.WriteLine("\n" + songs);
                }
            }
        }

        /*This a class method that is public. It takes a single string argument that represents a text file containing 
         * a collection of songs. You will read all the data and create songs and add it to the songs collection.You 
         * will have to read four lines to create one Song. Your loop body should have four ReadLine(). */
        public static void LoadSongs(string fileName)
        {
            /*Initialize the songs field to a new List of Song*/
            List<string> songs = new List<string> { "title", "artist", "length", "genre" };

            /*Declare four string variable (title, artist, length, genre) to store the results of four in reader.ReadLine().*/
            string title;
            string artist;
            double length;
            SongGenre genre;

            /*The first ReadLine() is a string representing the title of the song. This can and should be used as a check 
             * for termination condition. If this is empty then there are no more songs to read i.e. it is the end of 
             * the file. The next ReadLine() will get the Artist. The next ReadLine() will be a string that represents 
             * the weight. Use the Convert.ToDouble to get the required type. The next ReadLine() will be a string that 
             * represents the genre. Use the Enum.Parse() to get the required type. Use the above four variables to create 
             * a Song object. Add the newly created object to the collection.And finally do one more read for the title 
             * to re-enter the loop.*/

            TextReader reader = new StreamReader(filename);//<-ERROR The name 'filename' does not exist in the current context
            string line = reader.ReadLine();
            while (line != null)
            {
                string[] data = line.Split();
                title.Add(data[0]);//<-ERROR Use of unassigned local variable 'title'| 'string' does not contain definition for 'Add'
                artist.Add(data[1]);//<-ERROR Use of unassigned local variable 'artist'| 'string' does not contain definition for 'Add'
                length.Add(Convert.ToDouble(data[2]));/*<-ERROR Use of unassigned local variable 'length'| 'string' does not contain 
                                                       * definition for 'Add'*/
                genre.Add(Enum.Parse(data[3]));/*<-ERROR Use of unassigned local variable 'genre' |ERROR 'string' does not contain 
                                                * definition for 'Add' | ERROR The type arguments for method Enum.Parse cannot be 
                                                inferred from the usage*/                
                line = reader.ReadLine();
            }
            reader.Close();
        }
    }   
 }


class Program
    {        
        static void Main(string[] args)
        {
            List<string> songs = new List<string>();
            string filename = @"D:\songs4.txt";//<-Warning The variable 'filename' is assigned but it's value never used.

            //To test the constructor and the ToString method
            Console.WriteLine(new Song("Baby", "Justin Bebier", 3.35, SongGenre.Pop));//<-ERROR 'Pop'

            //This is first time to use the bitwise or. It is used to specify a combination of genres
            Console.WriteLine(new Song("The Promise", "Chris Cornell", 4.26, SongGenre.Country | SongGenre.Rock));//<-ERROR 'Country' and 'Rock'

            Library.LoadSongs("songs4.txt");     //Class methods are invoke with the class name
            Console.WriteLine("\n\nAll songs");
            Library.DisplaySongs();

            SongGenre genre = SongGenre.Rock;//<-ERROR 'SongGenre' does no contain a definition for 'Rock'
            Console.WriteLine($"\n\n{genre} songs");
            Library.DisplaySongs(genre);

            string artist = "Bob Dylan";
            Console.WriteLine($"\n\nSongs by {artist}");
            Library.DisplaySongs(artist);

            double length = 5.0;
            Console.WriteLine($"\n\nSongs more than {length}mins");
            Library.DisplaySongs(length);

            Console.ReadKey();
        }
    }
}

song4.txt文件用于测试解决方案:

song4.txt file is used to test the solution:

Baby
Justin Bebier
3.35
Pop
Fearless
Taylor Swift
4.03
Pop
Runaway Love
Ludacris
4.41
Pop
My Heart Will Go On
Celine Dion
4.41
Pop
Jesus Take The Wheel
Carrie Underwood
3.31
Country
If Tomorrow Never Comes
Garth Brooks
3.40
Country
Set Fire To Rain
Adele
4.01
Soul
Don't You Remember
Adele
3.03
Soul
Signed Sealed Deliverd I'm Yours
Stevie Wonder
2.39
Soul
Just Another Night
Mick Jagger
5.15
Rock
Brown Sugar
Mick Jagger
3.50
Rock
All I Want Is You
Bono
6.30
Metal
Beautiful Day
Bono
4.08
Metal
Like A Rolling Stone
Bob Dylan
6.08
Rock
Just Like a Woman
Bob Dylan
4.51
Rock
Hurricane
Bob Dylan
8.33
Rock
Subterranean Homesick Blues
Bob Dylan
2.24
Rock
Tangled Up In Blue
Bob Dylan
5.40
Rock
Love Me
Elvis Presley
2.42
Rock
In The Getto
Elvis Presley
2.31
Rock
All Shook Up
Elvis Presley
1.54
Rock

输出应如下所示:

Baby by Justin Bebier (Pop) 3.35min
The Promise by Chris Cornell (Rock, Country) 4.26min
All songs
Baby by Justin Bebier (Pop) 3.35min
Fearless by Taylor Swift (Pop) 4.03min
Runaway Love by Ludacris (Pop) 4.41min
My Heart Will Go On by Celine Dion (Pop) 4.41min
Jesus Take The Wheel by Carrie Underwood (Country) 3.31min
If Tomorrow Never Comes by Garth Brooks (Country) 3.40min
Set Fire To Rain by Adele (Soul) 4.01min
Don't You Remember by Adele (Soul) 3.03min
Signed Sealed Deliverd I'm Yours by Stevie Wonder (Soul) 2.39min
Just Another Night by Mick Jagger (Rock) 5.15min
Brown Sugar by Mick Jagger (Rock) 3.50min
All I Want Is You by Bono (Metal) 6.30min
Beautiful Day by Bono (Metal) 4.08min
Like A Rolling Stone by Bob Dylan (Rock) 6.08min
Just Like a Woman by Bob Dylan (Rock) 4.51min
Hurricane by Bob Dylan (Rock) 8.33min
Subterranean Homesick Blues by Bob Dylan (Rock) 2.24min
Tangled Up In Blue by Bob Dylan (Rock) 5.40min
Love Me by Elvis Presley (Rock) 2.42min
In The Getto by Elvis Presley (Rock) 2.31min
All Shook Up by Elvis Presley (Rock) 1.54min


Rock songs
Just Another Night by Mick Jagger (Rock) 5.15min
Brown Sugar by Mick Jagger (Rock) 3.50min
Like A Rolling Stone by Bob Dylan (Rock) 6.08min
Just Like a Woman by Bob Dylan (Rock) 4.51min
Hurricane by Bob Dylan (Rock) 8.33min
Subterranean Homesick Blues by Bob Dylan (Rock) 2.24min
Tangled Up In Blue by Bob Dylan (Rock) 5.40min
Love Me by Elvis Presley (Rock) 2.42min
In The Getto by Elvis Presley (Rock) 2.31min
All Shook Up by Elvis Presley (Rock) 1.54min


Songs by Bob Dylan
Like A Rolling Stone by Bob Dylan (Rock) 6.08min
Just Like a Woman by Bob Dylan (Rock) 4.51min
Hurricane by Bob Dylan (Rock) 8.33min
Subterranean Homesick Blues by Bob Dylan (Rock) 2.24min
Tangled Up In Blue by Bob Dylan (Rock) 5.40min


Songs more than 5mins
Just Another Night by Mick Jagger (Rock) 5.15min
All I Want Is You by Bono (Metal) 6.30min
Like A Rolling Stone by Bob Dylan (Rock) 6.08min
Hurricane by Bob Dylan (Rock) 8.33min
Tangled Up In Blue by Bob Dylan (Rock) 5.40min


推荐答案

它有很多不同的地方,需要一些时间来进行一些解释,但是这是一个基本问题(您从这里开始指向我您的问题)类型不能在其他类别中看到的是Genre枚举是在称为SongGenre的类中声明的,而不是直接在名称空间中声明的,因此您没有正确地引用它(其类型为SongGenre.Genre,而不是Genre),因此在Song类中(例如),您可以这样声明:

There are a couple of different bits wrong with it and it'll take a little while to work through with some explanations, but the basic problem (that you pointed me to here from your question) of "Genre can't be seen in other classes" is that the Genre enum is declared inside a class called SongGenre rather than being declared in the namespace directly, and you're hence not referring to it properly (it's of type SongGenre.Genre, not Genre) so in the Song class (for example) you'd declare like:

public SongGenre.Genre Genre { get; }
       ^^^^^^^^^^^^^^^ ^^^^^
     this is the type   the name

因此,这是Song构造函数中的语法错误:

Consequentially this is a bit of a syntax error in the Song contructor:

SongGenre Genre = SongGenre.genre;/*<-ERROR 'SongGenre does not contain a definition for 'genre'*/

应类似于:

Genre = SongGenre.Genre.Blues;

或类似:

Genre = genre;

但是您必须调整构造函数,而不是采用SongGenre类,而采用SongGenre.Genre枚举:

But then you have to adjust your constructor not to take a SongGenre class but to take a SongGenre.Genre enum:

public Song(string title, string artist, double length, SongGenre.Genre genre)




通过在SongGenre类中使用该枚举,实际上使您头疼。您应该考虑扔掉SongGenre类并将枚举直接移到名称空间中,而是将枚举重命名为SongGenre:


It's actually causing you a lot of headaches by having that enum inside the SongGenre class. You should consider throwing the SongGenre class away and moving the enum into the namespace directly, instead and renaming the enum to be SongGenre:

namespace whatever{

  enum SongGenre{ Blues...

这意味着您不必引用它始终使用类名作为前缀,您现有的代码将更像预期的那样工作

This means you don't have to refer to it by the class name prefix all the time and your existing code will work more like expected

您在这里还有另一种类型的困惑:

You have another type confusion here:

                if (songs.Genre == genre)/*<-ERROR 'string' does not contain a definition for 'Genre'
                                          * and no accessable extension method 'Genre' accepting a first
                                          * argument of type 'string' could be found*/
                {
                    Console.WriteLine("\n" + songs);
                }

歌曲是一个字符串列表,而不是歌曲列表,并且字符串没有Genre属性。尝试使用 List< Song>

songs is a list of strings, not a list of Songs, and strings don't have a Genre property. Try List<Song> instead

= new List<string> { "title", "artist", "length", "genre" };

这对我来说没有必要;您是否期望这些成为某些东西的列标题?这仅声明了4个字符串的列表,与歌曲无关。您也许可以将这些字符串加载到组合框中,以便用户可以选择要搜索的内容。 -但它们与歌曲无关

This doesnt need to make sense to me; are you expecting these to be column headers to somthing? This just declares a list of 4 strings, nothing really to do with songs. You could perhaps load these strings into a combo box so the user can "choose a thing to search by" - but they aren't anything to do with songs

title.Add(data[0]);//<-ERROR Use of unassigned local variable 'title'| 'string' does not contain definition for 'Add'

title是字符串,而不是列表或其他容器,并且它是不能添加到

title is a string, not a list or other container, and it cannot be added to

TextReader reader = new StreamReader(filename);//<-ERROR The name 'filename' does not exist in the current context
            string line = reader.ReadLine();
            while (line != null)
            {
                string[] data = line.Split();
                title.Add(data[0]);//<-ERROR Use of unassigned local variable 'title'| 'string' does not contain definition for 'Add'
                artist.Add(data[1]);//<-ERROR Use of unassigned local variable 'artist'| 'string' does not contain definition for 'Add'
                length.Add(Convert.ToDouble(data[2]));/*<-ERROR Use of unassigned local variable 'length'| 'string' does not contain 
                                                       * definition for 'Add'*/
                genre.Add(Enum.Parse(data[3]));/*<-ERROR Use of unassigned local variable 'genre' |ERROR 'string' does not contain 
                                                * definition for 'Add' | ERROR The type arguments for method Enum.Parse cannot be 
                                                inferred from the usage*/                
                line = reader.ReadLine();
            }
            reader.Close();

如果我正在读取该文件,我会这样做:

If I was reading that file I'd do it like:

//read all lines into an array
var songFile = File.ReadAllLines("...path to file...");

List<Song> library = new List<Song>();

//iterate, skipping 4 lines at a time
for(int i = 0; i< songFile.Length; i+=4){

  string artist = songFile[i];
  string title = songFile[i+1];
  double durn = double.Parse(songFile[i+2]);
  Genre gen = (Genre)Enum.Parse(songFile[3]);

  Song s = new Song(artist, title, durn, gen);
    
  library.Add(s);

}

这篇关于枚举已定义但未在类中找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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