Hey guys. I have a question about C # classes. I have a Character.cs file in which I create a class.

public class Character { public string Name { get; set; } public string Surname { get; set; } } 

Later in the main file in the character creation class, I record the data here:

 Character.Character character = new Character.Character { Name = CharacterController.LoadCharacterData(player).Result[0].Name, Surname = CharacterController.LoadCharacterData(player).Result[0].Surname }; 

Data is taken from the database. In another file, I wrote the data in the database

 public static bool CreateCharacter(Client player, string name, string surname) { Character.Character character = new Character.Character { Name = name, Surname = surname }; Characters.InsertOneAsync(character); return false; } 

And now I want to write this data (about the character) into global variables and so that I can use it in any part of the code / in any file. If something is not so explained - ask. I will answer in the comments.

  • one
    Global variables are evil. - iluxa1810

2 answers 2

  1. It is a bad idea. This is contrary to the ideology of the PLO.
  2. But you can do this if you declare Character variables static.
  • I can not make static, because when creating a new character it complains about the variable Name / Surname that they are static. - Joseph
  • so don't make static and surneym static, make Character instance static. - Andrew
  • and if you need a list of characters that would be accessible from everywhere ... just create a public static List <character> - Andrew

Global variables are evil.

It is better to make specialized classes for working with your class and pass Character as an argument.

Yes, you have to transfer a lot of variables here and there, but each class will be self-sufficient.

  • Can you set an example or throw off where I can get acquainted with it in more detail? I have already suffered a little with the designer, in which I passed the character and nothing came of it. Now try again. - Joseph
  • Simply, create auxiliary classes and transfer your class to them in the constructor and work. Nothing supernatural. - iluxa1810