I am a newbie of C# and MS visual studio, and I want to use the C# class which defined in another file, but can't get it work.
Here is the program.cs
(and why can't I rename that file ?)
using System;
namespace TestCSharp2
{
class Program
{
static void Main(string[] args)
{
Class2 class2 = new Class2();
// here the IDE will complain that cant find namespace or balabala..
class2.setValue(10);
Console.WriteLine(class2.getValue().ToString());
Console.ReadKey();
}
}
}
And here is the Class2
that I want to use in file Class2.cs
:
namespace TestCSharp2
{
class Class2
{
int i;
public void setValue(int i)
{
this.i = i;
}
public int getValue()
{
return this.i;
}
}
}
Should I #include
or something? isn't use namespace
enough?
As some guys asked if they are in the same assembly/same project, I presume they were, because here is the procedure how they are created:
- A new project using the template of Console C# Project, then the
program.cs
was created by default. - The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where
program.cs
lives.
To be honest, I don't know if they are in same assembly / same project, but I guess they were.