简体   繁体   English

C# 错误 - 非静态字段需要对象引用

[英]C# Error - Object reference is reqiured for the non-static field

I am very new to c# but I am stuck.我对 c# 很陌生,但我被卡住了。 I have explained my scenario below.我在下面解释了我的场景。

Now I have a list of enums like this,现在我有一个像这样的枚举列表,

public enum Colors
{
   Black,
   Blue,
   Red
}

I have 2 classes with switch statements(I have shown just 1 class since both the classes are similar).我有 2 个带有 switch 语句的类(我只展示了 1 个类,因为这两个类都相似)。 Now I am getting an error like this An object reference is requesred for the non-static field, method, or property 'RandomColor.GetColors' .现在我收到这样的错误An object reference is requesred for the non-static field, method, or property 'RandomColor.GetColors' Also I could have used the answer version in all my class that uses this switch statement but since I want to do like changing value in one file and it changes that value in all class for me.此外,我本可以在所有使用此 switch 语句的班级中使用答案版本,但因为我想更改一个文件中的值,并且它会为我更改所有班级中的值。

public class RandomColorService
{
   public readonly string _url;

   public RandomColorService()
   {
      switch (RandomColor.GetColors)
      {
        case Colors.Black:
            _url = "Use url for black color";
            break;
        case Colors.Blue:
            _url = "Use url for blue color";
            break;
        case Colors.Red:
            _url = "Use url for red color";
            break;
      }
   }
}

Therefore to achieve that I tried to create this class but now If I put static in the below class everything works perfectly, but I am trying to make it more dynamic so that I can change the value of GetColors from different class to a different color.因此,为了实现这一点,我尝试创建这个类,但现在如果我将静态放在下面的类中,一切正常,但我试图使其更具动态性,以便我可以将GetColors的值从不同的类更改为不同的颜色。 So that it uses the appropriate url in the above class.以便它使用上述类中的适当 url。

public class RandomColor //put static here
{
   public Colors GetColors { get; set; } = Colors.Black; //put static here
}

Can you guys help me with this error?你们能帮我解决这个错误吗?

The most straight forward way to change the value of a property is with a set statement:更改属性值的最直接方法是使用 set 语句:

var myTestDays = new TestDays();
myTestDays.DayTesting = Days.Sunday;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误:非静态字段需要对象引用 - C# error : An object reference is required for the non-static field 需要对象引用才能访问C#中的非静态字段 - An object reference is required to access non-static field in C# Object 参考 c# 中的非静态字段 - Object reference for non-static field in c# C#错误(使用接口方法):非静态字段,方法或属性需要对象引用 - C# error(using interface methods): An object reference is required for the non-static field, method, or property C#错误:非静态字段,方法或属性需要对象引用 - C# error: An object reference is required for the non-static field, method, or property C#中出错:“非静态字段,方法或属性需要对象引用” - Error in C#: “An object reference is required for the non-static field, method, or property” C#修复错误:“非静态字段,方法或属性需要对象引用” - C# Fixing error: “An object reference is required for the non-static field, method, or property” C#错误:非静态字段,方法或属性需要对象引用 - C# Error: Object reference is required for the non-static field, method, or property 为什么我在C#中得到“非静态字段,方法或属性需要对象引用”错误? - Why am I getting “object reference is required for the non-static field, method, or property” error in C#? C# 错误:“非静态字段、方法或属性需要对象引用” - C# error: "An object reference is required for the non-static field, method, or property"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM