Reputation: 9065
I am a newbie to C# and MS Visual Studio, and I want to use the C# class which is defined in another file, but can't get it to 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 can't 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 were in the same assembly/same project, I presume they were, because here is the procedure for how they are created:
program.cs
was created by default.program.cs
lives.To be honest, I don't know if they are in same assembly / same project, but I guess they were.
Upvotes: 48
Views: 186364
Reputation: 369
According to your example here it seems that they both reside in the same namespace. I conclude that they are both part of the same project (if you haven't created another project with the same namespace) and all classes by default are defined as internal to the project they are defined in, if haven't declared otherwise, therefore I guess the problem is that your file is not included in your project.
You can include it by right clicking the file in the solution explorer window => Include in project, if you cannot see the file inside the project files in the solution explorer then click the show the upper menu button of the solution explorer called show all files (just hover your mouse cursor over the button there and you'll see the names of the buttons).
Just for basic knowledge:
If the file resides in a different project\ assembly then it has to be defined,
otherwise it has to be defined at least as internal or public.
In case your class is inheriting from that class that it can be protected as well.
Upvotes: 2
Reputation: 1
Just make two projects in two different files then rename the "Program.cs" of one of the two files and copy it then paste it next to the Program.cs of the other file and that's it.
Upvotes: -1
Reputation: 19
When u diclare your , var you , can use private , declarasion
using System; private Class class;
Upvotes: -3
Reputation: 1
In your project there will be a file with .csproj
extension.
Double click on it to open the project in the Visual Studio. Otherwise, if you make a new class, it won't link with other classes.
Upvotes: -2
Reputation: 131
Yeah, I just made the same 'noob' error and found this thread. I had in fact added the class to the solution and not to the project. So it looked like this:
Just adding this in the hope to be of help to someone.
Upvotes: 13
Reputation: 437
I was having the same problem here. Found out that the problem was with an Advanced Property of the file. There is there an option with the name 'Compilation Action' (may be not with the exact words, I am translating - my VS is in Portuguese).
My Class1.cs file was there as "Content" and I just had to change it to "Compile" to make it work, and have the classes recognized by the others files in the same project.
Upvotes: 1
Reputation: 120
namespace TestCSharp2
{
**public** class Class2
{
int i;
public void setValue(int i)
{
this.i = i;
}
public int getValue()
{
return this.i;
}
}
}
Add the 'Public' declaration before 'class Class2'.
Upvotes: 4
Reputation: 13077
According to your explanation you haven't included your Class2.cs
in your project. You have just created the required Class file but haven't included that in the project.
The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where program.cs lives.
Do the following to overcome this,
Simply Right click
on your project then -> [Add] - > [Existing Item...] : Select Class2.cs
and press OK
Problem should be solved now.
Furthermore, when adding new classes use this procedure,
Right click
on project -> [Add] -> Select Required Item (ex - A class, Form etc.)
Upvotes: 37
Reputation: 1609
It would be more beneficial for us if we could see the actual project structure, as the classes alone do not say that much.
Assuming that both .cs files are in the same project (if they are in different projects inside the same solution, you'd have to add a reference to the project containing Class2.cs), you can click on the Class2
occurrence in your code that is underlined in red and press CTRL + . (period) or click on the blue bar that should be there. The first option appearing will then add the appropriate using
statement automatically. If there is no such menu, it may indicate that there is something wrong with the project structure or a reference missing.
You could try making Class2
public
, but it sounds like this can't be a problem here, since by default what you did is internal class Class2
and thus Class2
should be accessible if both are living in the same project/assembly. If you are referencing a different assembly or project wherein Class2
is contained, you have to make it public
in order to access it, as internal
classes can't be accessed from outside their assembly.
As for renaming: You can click Program.cs
in the Solution Explorer and press F2 to rename it. It will then open up a dialog window asking you if the class Program
itself and all references thereof should be renamed as well, which is usually what you want. Or you could just rename the class Program
in the declaration and again open up the menu with the small blue bar (or, again, CTRL+.) and do the same, but it won't automatically rename the actual file accordingly.
Edit after your question edit: I have never used this option you used, but from quick checking I think that it's really not inside the same project then. Do the following when adding new classes to a project: In the Solution Explorer, right click the project you created and select [Add] -> [Class] or [Add] -> [New Item...] and then select 'Class'. This will automatically make the new class part of the project and thus the assembly (the assembly is basically the 'end product' after building the project). For me, there is also the shortcut Alt+Shift+C working to create a new class.
Upvotes: 5