Error changing static property value

Asked

Viewed 106 times

1

I have my class DadosConexao, this class is responsible for storing the connection string of the database, it contains the following properties:

A statistical property StringConexao.

isServ indicates if you are a server or client.

servidor keeps the name of the machine.

The problem is I can’t move the property StringConexao, that in the case and the "Data Source=" a part of the string that would be the name of the machine that contains the database.

My class Dadosconexao:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DadosConexao
    {

        private String servidor;
        private Boolean isServ;       

        public DadosConexao(bool isServ, string servidor)
        {
            this.isServ = isServ;
            this.servidor = servidor;
        }

        public static string StringConexao 
        {
            get 
            {
                if (!isServ)//Da erro aqui
                    servidor = Environment.MachineName.ToString();//Caso isServ for true pega o nome da maquina local.

                return "Data Source=" + servidor + 
                       "\\MINHAINSTANCIA;" + 
                       "Initial Catalog=MINHABASE;" + 
                       "Persist Security Info=True;" + 
                       "User ID=sa;" + 
                       "Password=123456";
            }
        }
    }
}

Error presented by Visual Studio:

Error 1 An object reference is required 
for the non-static field, method, 
or property 'DAL.DadosConexao.isServ'

The system works as a client or server, if the client is the property servidor is set in user settings.

  • tried to use if (isServ == false) ?

  • 1

    Yes, the problem of this class has been solved, and I’m using if (isServ).

2 answers

2


You can solve this in two ways. One is taking static of property. See more about the static.

using System;

public static class Program {
    public static void Main() {
        var con = new DAL.DadosConexao(true, null);
    }
}

namespace DAL {
    public class DadosConexao {

        private string servidor;
        private bool isServ;       

        public DadosConexao(bool isServ, string servidor) {
            this.isServ = isServ;
            this.servidor = servidor;
        }

        public string StringConexao {
            get {
                if (!isServ)
                    servidor = Environment.MachineName.ToString();

                return "Data Source=" + servidor + 
                       "\\MINHAINSTANCIA;" + 
                       "Initial Catalog=MINHABASE;" + 
                       "Persist Security Info=True;" + 
                       "User ID=sa;" + 
                       "Password=123456";
            }
        }
    }
}

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

But this will require creating an instance of the class and passing it as a parameter, or creating a new one every time you need it, which in the case of the client involves additional logic to determine the server. This is not ideal. Even more so. If you are going to do this, it would be better to apply the singleton pattern in it.

To tell you the truth I think the logic that determines what to use if you’re a client should be in this class too and not come from outside.

The other solution is to turn everything into static where you need set the data of isServ and possibly of servidor once and catch the StringConexao as often as you like, as expected:

using System;

public static class Program {
    public static void Main() {
        DAL.DadosConexao.isServ = true;
        var con = DAL.DadosConexao.StringConexao;
    }
}

namespace DAL {
    public static class DadosConexao {
        public static string servidor { get; set; }
        public static bool isServ { get; set; }

        public static string StringConexao {
            get {
                if (!isServ) servidor = Environment.MachineName.ToString();
                return "Data Source=" + servidor + 
                       "\\MINHAINSTANCIA;" + 
                       "Initial Catalog=MINHABASE;" + 
                       "Persist Security Info=True;" + 
                       "User ID=sa;" + 
                       "Password=123456";
            }
        }
    }
}

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

You probably have a way to make it better, but this is the basis.

  • Thanks for the help, the second suggestion and great.

  • How you determine which server is when running on the client?

  • The server will be determined at the time of system implementation, a flag.

  • Yes, but how is it implemented?

  • The implementation phase still lacks some business rules to be defined, for me to see what will be the most viable and simplest way.

  • I’d help you more, but without knowing this, there’s no way.

  • Inside the system you will have a module that will create the database, if it is served it will execute the call that creates the database on the machine that is installed, otherwise the name of the server will be requested.

Show 2 more comments

1

You are trying to access a non-static property within a static property.

From what I understand of your business rule, this property does not need to be static. So, you just need to exchange her signature of

public static string StringConexao { ... }

for

public string StringConexao { ... }

You will only need a method/property static when your class doesn’t need to be instantiated to call you.

  • Vlw for the help.

Browser other questions tagged

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