Error changing static property value

1

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

A statistical property StringConexao .

isServ indicates whether it is a server or client.

servidor save machine name .

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

My class ConnectionData :

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";
            }
        }
    }
}

Visual Studio error:

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 client the servidor property is defined in the settings by the user.

    
asked by anonymous 18.10.2015 / 01:01

2 answers

2

You can solve this in two ways. One is to take static of the property. See more about 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";
            }
        }
    }
}

See working on dotNetFiddle .

But this will require you to create an instance of the class and either pass it as a parameter, or create a new one every time you need it, in the case of the client that involves additional logic to determine the server. This is not ideal. Even more. If you are going to do this, it would be best to apply the Standard Singleton in it.

To tell you the truth I think the logic that determines what to use if you are a customer should be inside this class too and not come from outside.

The other solution is to transform everything to static where set data from isServ and possibly servidor once and get StringConexao as many times as you want :

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";
            }
        }
    }
}

See working on dotNetFiddle .

You can probably improve this, but this is the basis.

    
18.10.2015 / 01:57
1

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

From what I can understand from your business rule, this property does not have to be static. So, you just need to change her signature from

public static string StringConexao { ... }

for

public string StringConexao { ... }

You will only need a method / property static when your class does not need to be instantiated to call it.

    
18.10.2015 / 01:41