I have a class in whose field I need to hang the value obtained in the method of another class.

Those. insert data into the connection string that is read by the Config class method.

This is how it looks in practice. So It swears at me:

"field initializer cannot be used

Here is my code:

namespace DBSync { class FireBird { Config config; public FireBird(Config config) { this.config = config; } string connectionString = "User=SYSDBA;" + "Password=masterkey;" + "Database=" + config "DataSource=localhost;" + "Port=3050;" + "Dialect=3;" + "Charset=NONE;" + "Role=;" + "Connection lifetime=15;" + "Pooling=true;" + "MinPoolSize=0;" + "MaxPoolSize=50;" + "Packet Size=8192;" + "ServerType=0"; 

Here is the code of the part that the config reads:

 namespace DBSync { public class Config { string fbLogin; string fbPass; string fbPath; string PGLogin; string PGPass; string SQLLitePath; public void parseConfig() { 

The question is how to do it? And why now does not work?

  • In the first block of code, the syntax is broken. Transfer the connectionString assignment above to the constructor - now it hangs outside the methods, right in the body of the class. In addition, the line where "Database=" + config - here lacks access to the config field and the + sign after that. - nzeemin
  • @nzeemin in the first part of your answer, do you mean it? img.ctrlv.in/img/16/04/18/5714b7ef3e43a.png - Dmitry Bubnenkov

1 answer 1

The error clearly indicates that non-static fields cannot be used to initialize fields other than in the constructor.

Thus, for the solution, you need to transfer the initialization to the constructor

 public FireBird(Config config) { this.config = config; this.connectionStroing = "..."+ "..."+this.config+ "..." } 
  • Ie the declaration of the connection string goes to the constructor, and in the constructor just initialization? I have by the way earned the config without this . Why? Did you do it right, did everything with your hands, or did it have to be somehow different? img.ctrlv.in/img/16/04/18/5714b8e03ad02.png - Dmitry Bubnenkov
  • @Suliman, yes, the declaration is standard outside the constructor, in the constructor - initialization . It is entirely natural that the config earned Without this , since you have the config parameter. The last question and the picture did not understand - Grundy
  • so, and if I didn’t pass a config instance to the class, then I wouldn’t be able to contact this without it? Or could it? On the second issue. Well, I did all the fields there as a public one. It is right? - Dmitry Bubnenkov
  • @Suliman, if an instance of the config were not transmitted, it would work with this , as there is a config field, but at the time of execution it will most likely be a NullReferenceException because this field is not initialized, because an instance of the config is not passed. According to Public - in theory, all fields should be private or protected, since fields are data, and data should be hidden :) - Grundy
  • and what would this point to? I thought it points to the variables of the current class. And then how? - Dmitry Bubnenkov