C# 如何解决“...是一种在给定上下文中无效的“类型”?(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2278931/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve '...is a 'type', which is not valid in the given context'? (C#)
提问by Penguen
The following code produces the error:
以下代码产生错误:
Error : 'CERas.CERAS' is a 'type', which is not valid in the given context
错误:“CERAs.CERAS”是“类型”,在给定的上下文中无效
Why does this error occur?
为什么会出现这个错误?
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinApp_WMI2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CERas.CERAS = new CERas.CERAS();
        }
    }
}
采纳答案by Adriaan Stander
Change
改变
private void Form1_Load(object sender, EventArgs e) 
    { 
        CERas.CERAS = new CERas.CERAS(); 
    } 
to
到
private void Form1_Load(object sender, EventArgs e) 
    { 
        CERas.CERAS c = new CERas.CERAS(); 
    } 
Or if you wish to use it later again
或者如果您想稍后再次使用它
change it to
将其更改为
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
namespace WinApp_WMI2 
{ 
    public partial class Form1 : Form 
    { 
        CERas.CERAS m_CERAS;
        public Form1() 
        { 
            InitializeComponent(); 
        } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
        m_CERAS = new CERas.CERAS(); 
    } 
} 
}
回答by Darin Dimitrov
CERAS is a class name which cannot be assigned. As the class implements IDisposablea typical usage would be:
CERAS 是一个不能分配的类名。由于该类实现了IDisposable,一个典型的用法是:
using (CERas.CERAS ceras = new CERas.CERAS())
{
    // call some method on ceras
}
回答by Marcel Gheorghita
You forgot to specify the variable name. It should be CERas.CERAS newCeras = new CERas.CERAS();
您忘记指定变量名称。它应该是CERas.CERAS newCeras = new CERas.CERAS();

