枚举作为 C# 函数中的参数
enum as an argument in C# function
我正在尝试编写一个逐字符写入文本的程序。我想使用枚举选择写入速度。但它会引发错误。所以,这是我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace WritingApp
{
class Program
{
public enum WriteMode { Slow, SlowRandom, Normal, NormalRandom, Fast, FastRandom };
static void Main(string[] args)
{
MessageWrite("smth", WriteMode.Fast); //Error here
//An object reference is required for the non-static field, method, or property
//'Program.MessageWrite(string, Program.WriteMode)'
}
void MessageWrite(string message, WriteMode mode)
{
for(int i = 0; i < message.Length; i++)
{
Console.Write(message[i]);
}
}
}
}
你应该MessageWrite
静态化。
static void MessageWrite(string message, WriteMode mode)
{
for (int i = 0; i < message.Length; i++)
{
Console.Write(message[i]);
}
}
看看Why static methods can't call non-static methods directly?
我正在尝试编写一个逐字符写入文本的程序。我想使用枚举选择写入速度。但它会引发错误。所以,这是我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace WritingApp
{
class Program
{
public enum WriteMode { Slow, SlowRandom, Normal, NormalRandom, Fast, FastRandom };
static void Main(string[] args)
{
MessageWrite("smth", WriteMode.Fast); //Error here
//An object reference is required for the non-static field, method, or property
//'Program.MessageWrite(string, Program.WriteMode)'
}
void MessageWrite(string message, WriteMode mode)
{
for(int i = 0; i < message.Length; i++)
{
Console.Write(message[i]);
}
}
}
}
你应该MessageWrite
静态化。
static void MessageWrite(string message, WriteMode mode)
{
for (int i = 0; i < message.Length; i++)
{
Console.Write(message[i]);
}
}
看看Why static methods can't call non-static methods directly?