2010-02-17 37 views
11

Các mã sau đây tạo ra lỗi:Cách giải quyết '... là' loại ', không hợp lệ trong ngữ cảnh cụ thể'? (C#)

Error : 'CERas.CERAS' is a 'type', which is not valid in the given context

Tại sao lỗi này xảy ra?

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(); 
     } 
    } 
} 

Trả lời

16

Thay đổi

private void Form1_Load(object sender, EventArgs e) 
    { 
     CERas.CERAS = new CERas.CERAS(); 
    } 

để

private void Form1_Load(object sender, EventArgs e) 
    { 
     CERas.CERAS c = new CERas.CERAS(); 
    } 

Hoặc nếu bạn muốn sử dụng nó sau này một lần nữa

thay đổi nó để

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(); 
    } 
} 


} 
3

CERAS là tên lớp không thể được chỉ định. Khi lớp thực hiện IDisposable, cách sử dụng thông thường sẽ là:

using (CERas.CERAS ceras = new CERas.CERAS()) 
{ 
    // call some method on ceras 
} 
4

Bạn quên chỉ định tên biến. Nó phải là CERas.CERAS newCeras = new CERas.CERAS();

Các vấn đề liên quan