mhc
mhc

Reputation: 43

how to use a new class method in other apps?

I used this code in c++ to use a class (that I defined before)in my other Apps.

#include class_name ;

How can I define a public class that could be used in all apps? Thanks

Upvotes: 1

Views: 81

Answers (5)

Jodrell
Jodrell

Reputation: 35716

To access classes from external assemblies you must add a reference to an external assembly. This will allow you to access public classes from the external assembly.

To specify a class from a namespace outside your current scope you must prefix the class's type specifier with its namespace name. To avoid this overhead, you can "include" the external namespace with the using directive.

Multiple namespaces can exist within a single assembly.


Assembly Fruit:

namespace Common
{
    public class Strange
    {
       var mystery = new Mystery() // Won't compile, no reference to Mystery.
    }
}

namespace Fruit
{
    public class Orange
    {
    }
}

Assembly Vegetables:

References Fruit

namespace Common
{
    public class Mystery
    {
    }
}

namespace Fungi
{
    public class Mushroom
    {
    }
}

namespace Vegetables
{
    using Common;

    public Class Carrot
    {
        var strange = new Strange() // Compiles correctly.
        var mystery = new Mystery() // Compiles correctly.
        var orange = new Orange() // Won't compile, what's an Orange?
        var orange = new Fruit.Orange() // Compiles correctly.
        var mushroom = new Mushroom() // Won't compile, what's a Mushroom?
        var mushroom = new Fungi.Mushroom() // Compiles correctly.
    }
}

Upvotes: 3

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

Every class is created under a namespace

namespace abc{

public MyClass{
//functionality
}

}

To use your class on a different application,you need to import the namespace.

using abc;

public class usingClass{

MyClass obj = new MyClass();

}

Upvotes: 0

Servy
Servy

Reputation: 203823

If the other class is public or internal (and in the same assembly if they are internal), is in the same project, and has the same namespace, then you don't need to do anything at all. You will be able to refer to the other class by just using it's class name.

If they are in different namespaces then you can use a using statement (at the top of the file) to bring the other namespace into scope, or you can refer to the other class using the fully qualified name (i.e. OuterNamespace.InnerNamespace.ClassName) every time you use the class. (Which almost nobody ever does, everyone just uses using statements because they are so much more convenient.)

If the class is in another project entirely then you will need to add a reference to that class through visual studio. If you are creating a project that is designed to be referenced by other projects then it's project type should be a "class library".

Upvotes: 0

Chris Snowden
Chris Snowden

Reputation: 5002

You have to include the namespace as below and also add any references if in another project.

using class_namespace;

Upvotes: 0

SLaks
SLaks

Reputation: 887365

You need to create a Class Library project, which compiles to a DLL file.

You can then add a reference to it in other projects.

Upvotes: 2

Related Questions