Can't utilize an existing Class in a new C# project? - c#

I'm a student learning mainly C++, but this term we have to code our math assignments using C#.
Our professor supplied a basic skeleton program but I'm not very good at C#. He gave us two class files (.cs) but when I add them to my project, I'm unable to utilize them at all. I can't create a class object from either class.
The classes are just Line3d and Point3d. They have the variables needed to compute points and collision.
Thanks for any advice.

Compile your project.
Use Ctrl + . or bulb icon (type your class name you want to use and locate your cursor position over that class name) to resolve namespace for these classes or write using directive manually.

C# classes are normally encapsulated in namespaces. In Visual Studio, adding a new class will generate a file containing a namespace similar to PROJECT_NAME.SUBFOLDER.SUBSUBFOLDER For example:
// MyClass.cs
using System;
namespace MyProject
{
public class MyClass
{
}
}
And then you can reference that from another class in the same namespace, but you can't reference it from a class in another namespace (unless it's a namespace that starts with MyProject.).
// Line3d.cs
using System;
namespace TemplateProject
{
public class Line3d
{
}
}
// MyClass.cs
using System;
namespace MyProject
{
public class MyClass
{
public Line3d LineInstance {get;set;}
}
}
In this example, it won't work because the compiler doesn't know which namespace Line3d exists in (and, indeed, two class with the exact same name could exist in two different namespaces). You need to instruct the compiler to include classes from the TemplateProject namespace (note this doesn't include classes in the TemplateProject.ChildNamespace namespace):
// MyClass.cs
using System;
using TemplateProject;
namespace MyProject
{
public class MyClass
{
public Line3d LineInstance {get;set;}
}
}
Now you should be able to find the Line3d class and use it.
Besides manually referencing the namespace, you can also right-click an unknown class reference, select "Quick actions and Refactorings...", and then you will see something like "using TemplateProject;". Click on this and it will automatically add the using for you.
You can also use the Ctrl+. keyboard shortcut, which does the same as right-click/Quick actions, if you don't want to use the mouse.

select your project and press [ Shift + Alt + A ] to add existing files.
you can see dialog form that allows to open cs file on project.
After that, you can use professor's class files.

Related

C# Namespace, Folders, Compiler Error

I'm using VS 2017 v15.5.0.
I have a minimal Console project named, Con_02. The namespace for the main class in this project is simply Con_02 (the class is shown below).
Within this project I add a new folder named, Business. Within the Business folder I create a class named, Employee. The default namespace generated by VS for the Employee class is Con_02.Business. I simplify this namespace to Business.
Back in my main class I instantiate Employee. Here is my full main class:
namespace Con_02 {
class Program {
Business.Employee e1 = new Business.Employee();
private static void Main() { }
}
}
So far, so good. Everything compiles.
Now, I create another class, Company, in the Business folder. VS generates a namespace, Con_02.Business.
Now, the main Con_02.Program class no longer compiles. Specifically, the creation of the Business.Employee object which had previously compiled just fine, gives me a compiler error:
The type or namespace name 'Employee' does not exist in the namespace 'Con_02.Business' (are you missing an assembly reference?)
I'm not asking how to fix the problem so much as I'm trying to understand why compiler seems to assume a namespace relative to Con_02.
Since you are creating a new namespace called Con_02.Business which contains Company class the Business.Employee is considered to be under Con_02.Business namespace but Con_02.Business contains nothing but Company class.
Better to change
namespace Con_02.Business
{
class Company
{
}
}
to
namespace Business
{
class Company
{
}
}
or just use Employee e1 = new Employee();
Remember namespace is only about grouping the classes.
Add
using Business;
at the top of you Program class.
and remove The Business-part from your Employee instantiation.
OR. Use the same namespace for all you classes.

In C#, How Can I Use Public Class Members(Methods Or Variables) From Different Assembly

I am working on C# encapsulation from Tutorialspoint.com. And I read this What is the difference between Public, Private, Protected, and Nothing?1 question from Stackoverflow. I read answer and i understood access specifiers in teoric. Now I want to make console application with this subject in visual studio.
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can only be accessed by code in the same class or struct.
protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
Variables or methods with public access specifier are accessed from same assembly and different assembly. But this station is different in internal description. Internal types variables and methods can accessed only same assembly but not different assembly in c#. I want to test this station in C#.So that i create two project and call method or variables between each other.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TutorialsPoint.Encapsulation
{
public class PublicEncapsulation
{
//member variables
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
}
Above code is my 'PublicEncapsulation.cs' and i should call its members from other assembly.My other assembly project's class is Program.cs. I want to connect PublicEncapsulation.cs's members from Program.cs(other assembly). How can i do this calling operation from other assemblies in c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections;
namespace CallOtherAssemblyVariablesOrMethods
{
class Program
{
static void Main(string[] args)
{
/*Call PublicEncapsulation.cs's members in there.*/
}
}
}
Above class is Program.cs. I want to call other asssembly PublicEncapsulation.cs's members in here.
I guess in your Program.cs you have something like this:
var typeFromOtherAssembly = new InternalEncapsulation();
// Here you expect a compiler error:
var area = typeFromOtherAssembly.GetArea();
// This should return a string.
var details = typeFromOtherAssembly.Display();
You think the new and Display() would work, and that the (internal) GetArea() call would show a compiler error:
'InternalEncapsulation' does not contain a definition for 'GetArea' and no extension method 'GetArea' accepting a first argument of type 'InternalEncapsulation' could be found (are you missing a using directive or an assembly reference?)
But you didn't specify an access modifier for the InternalEncapsulation class, so it's internal:
Internal is the default if no access modifier is specified.
So at new InternalEncapsulation you get another compiler error:
InternalEncapsulation is inaccessible due to its protection level
So you need to make it public:
public class InternalEncapsulation
2 days ago i have an simple problems. I solved my my problems with Stackoverflow. I wanted to see difference between internal and public access specifier. And then i created two project to see difference of them. If i could call public methods and couldn't call internal methods from other assembly and then theoretical knowledge is supported with C# console application. I wanted to do this. But i can't see other project's public members. And then i found solution in this How to use a Class from one C# project with another C# project tutorial. I should have added reference in project with right click.
SOLUTION STEPS
1. In the 'Solution Explorer' tree, expand the
'CallOtherAssemblyVariablesOrMethods' project and then right-click
the project and select 'Add Reference' from the menu.
2. On the 'AddReference' dialog, select the 'Projects' tab and select your
'TutorialsPoint ' project.
3. If you are using namespaces then you will need to import the namespaces for your 'CallOtherAssemblyVariablesOrMethods' types by adding 'using' statements to your files in 'TutorialsPoint'.
Many thanks to everyone...

Using Multiple Namespaces to Describe a Class

I am writing code for a game, and wanted to include my main method in two different namespaces so that it could easily access all the classes from both the 'Engine' and 'Core' Namespaces.
namespace Engine Core
{
class ExampleClass
{
}
}
Although I just put a space between Engine and Core, I know that this syntax is incorrect, I would like to know how to make a class a member of multiple namespaces. If this is not possible, is there anything that I could do that would act the same? (Classes in neither these two namespaces having to refer to this class by 'Engine.' or 'Core.'
A class can not belong to two different namespaces.
If you want to refer to a class of the Engine or Core namespaces without explicitly writing the namespace each time you reference a type of those namespaces, just use using at the beginning of the file. The using directive allows the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:
using Engine;
or
using Core;
Check the documentation: using Directive
So you want someone to be able to access ExampleClass by using Engine.ExampleClass and Core.ExampleClass? I'm not sure why you would (I'm sure you have your reasons) but two ways to expose something like this are:
namespace Foo
{
abstract class ExampleClass
{
//Only implement the class here
}
}
namespace Engine
{
class ExampleClass : Foo.ExampleClass
{
//Don't implement anything here (other than constructors to call base constructors)
}
}
namespace Core
{
class ExampleClass : Foo.ExampleClass
{
//Don't implement anything here (other than constructors to call base constructors)
}
}
Or you could use namespace aliasing but every cs file using the class would require the alias to be defined.
using Engine = Core;

C# Namespace issue

I am new to C# and I wish to know how to use my own namespace in c#.
Suppose I have a namespace MyNamespace1.
And I have another namespace MyNamespace2. I want to use MyNamespace1. But when I use
using MyNamespace1;
it is not recognizable.I want to know how to do this.
MSDN is your friend in learning about this.
The namespace keyword is used to declare a scope.
namespaces used to organize it's many classes
namespace N1 // N1
{
class C1 // N1.C1
{
class C2 // N1.C1.C2
{
}
}
namespace N2 // N1.N2
{
class C2 // N1.N2.C2
{
}
}
}
Using Namespaces (C# Programming Guide)
Check your accessibility level of your namespace. If it is in same project then you can access it directly.
using YourProjectName.NamespaceThatYouCreated;
If it is another project like dll etc then add reference to that library or project.
access namespace as:
using AnotherProject.CreatedNameSpacename;
http://msdn.microsoft.com/en-us/library/z2kcy19k%28v=vs.80%29.aspx
and this one in particular:
http://www.csharp-station.com/Tutorials/Lesson06.aspx
Also, what I've discovered, if you're using asp.net you might need to change the property "Build action" of the class you wish to use in your page to "Compile".
If both of your name spaces are in different project then you must add the reference of the other project to access it's namespace. In your case add the project reference of MyNamespace1 to the project the project of MyNamespace2.

C# "linking" classes

I'm starting a simple C# console solution via Mono on Mac OS X.
I have a Main.cs file for starters, but I want to create a separate class and be able to access object of that class from my Main.cs file.
How can I access that class from the Main.cs file?
Say my class name was Math.
In my Main.cs file, can I create a new object like so:
Math calculator = new Math()
Without referencing the class in the Main.cs file in any way?
Or, do I have to use some sort of import statement/directive?
You need a using statement if your Main and Math are in different name spaces, otherwise it just works. Below is an example. The using System brings in the library that contains the Console class, but no using is required to use the Math class.
Program.cs:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Math caclulator = new Math();
Console.WriteLine(caclulator.Add(1, 2));
}
}
}
Math.cs:
namespace ConsoleApplication1
{
class Math
{
public int Add(int x, int y)
{
return x + y;
}
}
}
There are two scenarios here. Either this class is in a separate dll (class library project), or under the same project. To reference within the same project not additional work is needed, other than referencing it with the correct namespace (as mentioned in other posts).
In the case of a separate dll, you need to add a refence to the project in the project definition. Most default projects come with a reference to System.dll and other related libraries. It is recommended to name your dll's based on what namespaces are defined within it. If you have classes like Foo.Mathematics.IntMath, Foo.Mathematics.DblMath then I suggest you name it Foo.Mathematics.dll.
When I was where you are, I picked up .NET Framework Essentials from O'Reilly and it had answers to all my questions at the time.

Categories

Resources