I can’t get a variable from another namespace

Asked

Viewed 742 times

-1

A project is Webapplication and the other one is like Console:

"Projectste" Nomedasolution Structure below:

  1. Business(Pasta)

    1. Businessdal(project)
      1. Arquivodal.Cs
  2. Console(project)

    1. Business.Cs

//Project Businessdal, Arquivodal.Cs

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

  • I believe that is your question. https://stackoverflow.com/questions/740937/accessing-variables-from-other-namespaces

2 answers

2


I created two projects: Consoleapp1 and Consoleapp2.

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

For that do the following:

1. Add project 2 reference to project 1

Right click on References, and select Add Reference:

inserir a descrição da imagem aqui

Within Projects, select Solution and mark the project you want to reference and click OK:

inserir a descrição da imagem aqui

2. Access the desired variable

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

  namespace ConsoleApp1
  {
      public class Program
      {
          static void Main(string[] args)
          {
              var teste = ConsoleApp2.Teste.Caminho;            
          }
      }
  }
  • Sorry, I forgot to quote this option has already been used.

  • Remove using and use what I just said to access the variable.

  • This has also been done...

  • I got it, I’ll edit the answer

  • One project is webapplication type and the other is console type, remembering that this is not the real project just put fictitious parts to simulate

  • Yes, it will work the same. Have you even tried to do any of the things mentioned here? I created a project from scratch and it worked. If I create another web type and another console will work too.

Show 1 more comment

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 you’re using a using System will give conflict with the class Console contained in that namespace. Of course, using the full name of what you are accessing works, as in the code below. In this case it is possible to even delete the using Console`.

Since you’re on the same project, you don’t have to do anything else. If you’re in another project and you’re in the same solution, you should have a reference to this project, unless you’ve corrupted something. If it is a separate site project (not shown in the question) then you need add a design reference to her (visually supported).

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

See working ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • is because I’m working on another project was done using fictional names with only small pieces of code, but I appreciate the suggestions.

  • Then the question is not clear. If you put one thing and are doing another, we have no way of knowing.

  • as I said is fictional, but the doubt is to simulate a project you used is of the webapplication type and the other is of the console type?

  • So I’m thinking it’s duplicate, you already have several of these answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.