我的第一个 C# 枚举,如何解决错误 CS0117?
My First C# Enum, How Do I resolve Error CS0117?
我是C#初学者,复习了我的class笔记,网上研究,甚至抄袭了一些人。我仍然不明白我在这段代码中做错了什么。我也尝试将数据类型从字符串更改为整数,但仍然出现错误。
prog.cs(12,57): error CS0117: 'Months' does not contain a definition for `monthNum'
prog.cs(17,13): (Location of the symbol related to previous error)
我正在尝试为一年中的几个月创建枚举,接收用户输入的月份数字,并从枚举列表中输出月份名称。
using System;
namespace Homework
{
public class Program
{
public static void Main(string[] args)
{
Console.Write("Enter month number >> ");
string monthNum = Console.ReadLine();
Console.WriteLine(monthNum + " is " + (string)Months.monthNum);
}
}
}
public enum Months
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
还有其他方法,不过你可以使用Enum
相关方法:
if (Enum.TryParse(monthNum, out Months monthEnum) &&
Enum.IsDefined(monthEnum))
{
Console.WriteLine($"{monthNum} is {monthEnum}");
}
else
{
Console.WriteLine("Invalid month");
}
我是C#初学者,复习了我的class笔记,网上研究,甚至抄袭了一些人。我仍然不明白我在这段代码中做错了什么。我也尝试将数据类型从字符串更改为整数,但仍然出现错误。
prog.cs(12,57): error CS0117: 'Months' does not contain a definition for `monthNum' prog.cs(17,13): (Location of the symbol related to previous error)
我正在尝试为一年中的几个月创建枚举,接收用户输入的月份数字,并从枚举列表中输出月份名称。
using System;
namespace Homework
{
public class Program
{
public static void Main(string[] args)
{
Console.Write("Enter month number >> ");
string monthNum = Console.ReadLine();
Console.WriteLine(monthNum + " is " + (string)Months.monthNum);
}
}
}
public enum Months
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
还有其他方法,不过你可以使用Enum
相关方法:
if (Enum.TryParse(monthNum, out Months monthEnum) &&
Enum.IsDefined(monthEnum))
{
Console.WriteLine($"{monthNum} is {monthEnum}");
}
else
{
Console.WriteLine("Invalid month");
}