I can not get a variable from another namespace

0

One project is WebApplication and the other is a Console :

"Test Project" SolutionName Structure below:

  • Business (Folder)

  • BusinessDAL (project)
  • DLL.cs file
  • Console (project)

  • Business.cs
  • // BusinessDAL Project, DLL.cs file

    using Console; //Isto não funciona, já tentei com e sem.
    
    namespace BusinessDAL
    {
        public class ArquivoDAL
        {   
    
           public static string caminhos(){  
    
    
    
               string valor =  Console.Caminho; // Nâo funciona 
               string valor =  Console.Business.Caminho; // Nâo funciona 
               string valor =  Caminho; // Nâo funciona 
    
               return valor;
    
          }
        }
    }
    

    // Console Project, Business.cs file

    namespace Console
    {
      public class Business
        {
            public static string Caminho = ConfigurationManager.AppSettings.Get("Path").ToString();
    
           //Isto já foi testado também
            public static string NomeDoProduto
             {
                 get { return NomeDoProduto; }
                 set { NomeDoProduto = Caminho ; }
             }
    
    
        }
    
    }
    

    Error Message: the name 'Console' does not exist in the current context

        
    asked by anonymous 04.08.2017 / 15:50

    2 answers

    2

    I created two projects: ConsoleApp1 and ConsoleApp2.

    What you should do is to add the ConsoleApp2 reference in the ConsoleApp1 project to access the ConsoleApp2 variables in the ConsoleApp1 project.

    To do this do the following:

    1. Add project reference 2 in project 1

    Right click on References , and select Add Reference :

    UnderProjects,selectSolutionandchecktheprojectyouwanttoreferenceandclickOK:

    2. Access the desired variable

    Once this is done, within project 1 you can already access project variable 2 as follows:

      namespace ConsoleApp1
      {
          public class Program
          {
              static void Main(string[] args)
              {
                  var teste = ConsoleApp2.Teste.Caminho;            
              }
          }
      }
    
        
    04.08.2017 / 15:56
    1

    Using Console.Business.Caminho works. I created a project without setting anything up and it worked. I just made some changes to simplify the compilation, but if your code is the one posted in the question there is no way to go wrong.

    I would change the name Console because if it is using a using System it will conflict with the Console class contained in that namespace . Of course if using the full name of what is accessing works, as in the code below. In this case it is even possible to delete the using Console '.

    Since you are on the same project you do not have to do anything else. If it is in another project and is in the same solution already is to have a reference for this project, unless it has corrupted something. If it's a project in a separate location (it's not what it shows in the question) then you need to add a project reference to it ( with visual support ).

    using Console;
    
    namespace BusinessDAL {
        public class ArquivoDAL {   
            string valor =  Console.Business.Caminho;
            static void Main() => System.Console.WriteLine("Hello, World!"); //só p/ compilar
        }
    }
    

    See working Coding Ground . Also I placed GitHub for future reference .

        
    04.08.2017 / 16:06