Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a folder with name learncs in c drive and it contains two notepads and i wrote code in them and saved as Class1.cs and Class2.cs.(C:\learncs\Class1.cs,Class2.cs).

code in Class1.cs

C#
using System;
class Class1
{
  public void Display()
  {
    Console.WriteLine("This is Display() in Class1");
  }
}


code in Class2.cs

C#
using System;
class Class2
{
  static void Main()
  {
    Class1 c=new Class1();
    c.Display();
    Console.ReadLine();
  }
}


When i compile Class2.cs it is giving me the error like

error CS0246 :The type or namespace name 'Class1' could not be found(are you missing a using directive or an assembly rererence?)

so i tried by adding name space also i.e., namespace learncs before the class type and also using learncs statement in Class2.cs but not able to resolve it .Can anyone help me out plz...
Posted
Updated 30-Apr-15 17:19pm
v2
Comments
virang_21 30-Apr-15 23:04pm    
Declare your class1 as public.

Access modifiers https://msdn.microsoft.com/en-us/library/ms173121.aspx

Yes, if you do it right. Which are you trying to do?

0) Compile both into one EXE, the following works:
csc /t:exe class1.cs class2.cs

1) Compile Class1 into a DLL and compile Class2 into an EXE that refers to the DLL, the following gives errors like you describe:
csc /t:library class1.cs
csc /t:exe /r:class1.dll class2.cs

"
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
" -- MSDN
 
Share this answer
 
v5
Comments
Abhinav S 1-May-15 1:38am    
5 indeed
Sergey Alexandrovich Kryukov 1-May-15 3:22am    
Voted 4. All right, but you should have mentioned that each file is compiled into assembly. DLL or EXE, does not matter. It would work even of both where ".EXE", it only required an entry point. Besides, it would be important to explain that having non-static Class1 is pointless, it should be done static. And the names of classes are bad.
—SA
make class1 as public


C#
public class Class1  // add public access modifier
   {
       public void Display()
       {
           Console.WriteLine("This is Display() in Class1");
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900