30

The following code produces the error:

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

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();
        }
    }
}
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Penguen
  • 16,836
  • 42
  • 130
  • 205

5 Answers5

33

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


}
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
6

CERAS is a class name which cannot be assigned. As the class implements IDisposable a typical usage would be:

using (CERas.CERAS ceras = new CERas.CERAS())
{
    // call some method on ceras
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
6

You forgot to specify the variable name. It should be CERas.CERAS newCeras = new CERas.CERAS();

Marcel Gheorghita
  • 1,853
  • 13
  • 19
2

This exception can also be raised when dealing with arrays and forgetting keyword new. (Happened in my case)

Array Syntax(C#)

data_type [] arrayName =  new data_type[size];
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Kiragu
  • 98
  • 2
  • 7
0

for me it was another problem where I wrote

alertScript as = alert.GetComponent<alertScript>();

don't use as as var name ...

Jaxx0rr
  • 507
  • 4
  • 7