Click here to Skip to main content
16,017,707 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two namespace App and IncentiveApp i have to access variable in App namespace in InentiveApp.How can i do this?

What I have tried:

I have two namespace App and IncentiveApp i have to access variable in App namespace in InentiveApp.How can i do this?
Posted
Updated 2-Feb-22 15:21pm
Comments
Richard MacCutchan 2-May-16 3:11am    
Go to the C# documentation and study the use of classes and methods. You cannot directly call a method in a namespace, you need a class or object to act as the interface. And if you look at any of the millions of samples of C# code around, you will see it happening everywhere.
Sergey Alexandrovich Kryukov 2-May-16 3:18am    
The question makes no sense at all. Namespaces cannot change any behavior, they just define the full names of top-level types. There is no such thing as "call a method from namespace". A method is either a static method of some class/struct, or an instance method called on one of the instances.

In other words, start learning programming and .NET from scratch, this is the only thing you can do. The non-existing problem you so incorrectly pose is related to your unawareness of the very basic things.

—SA

You can use the using Directive (C# Reference)[^]

First you need a class, that defines the variable either as static as a regular member.
Let's say
C#
namespace App
{
    public class MyClass
    {
        public MyClass()
        {
            myVariable = 1;
        }
    
        public int myVariable;

        public static int StaticVariable = 3;
    
    }
}


C#
using App; // at the beginning of your .cs file
MyClass myClass = new MyClass();
var v = myClass.myVariable; // defined in the App namespace
var stat = MyClass.StaticVariable;


or you can use the full identifier
C#
App.MyClass myClass = new App.MyClass();
var v = myClass.myVariable;
var stat = App.MyClass.StaticVariable;


Then if you should access a variable directly from outside the scope of the class is another matter entirely.
 
Share this answer
 
v2
It is a very basic question. Just go through C# documentation as Richard MacCutchan said. Anyway refer the below link,
c# - Accessing variables from other namespaces - Stack Overflow[^]
 
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