-2

I want to use textBox1.Text in a different class not just in the main where the text boxes are defined so to say. Is there any way to make it global? Because it only allows me to use it in the main thing, not in the seperate class that I have to make in my task.

I need to store the text from the textBox in a List that is in a different class so the user can't add the same name twice so I need to remember what was typed in in the first input.

This is the class where I created a List and where I want to store those inputs:

internal class Clanovi
{
    public static List<Clan> Dodaj()
    {
        List<Clan> clanovi = new List<Clan>();
        clanovi.Add(new Clan() { KorIme = textBox1.Text, Lozinka = textBox2.Text });
        return clanovi;
    }
}
class Clan
{                                          
    public string KorIme { get; set; }
    public string Lozinka { get; set; }
}

This is WinForm btw.

dodekja
  • 537
  • 11
  • 24
Zoof
  • 55
  • 6
  • 1
    https://stackoverflow.com/a/14303767/215752 This is an answer to how to have global objects in C# I answered a while ago. The short answer is to use a static class to store your global objects. – Hogan Oct 24 '22 at 16:52
  • @Hogan It doesn't let me use the global object in the public void textBox1 that is on the Windows form. – Zoof Oct 24 '22 at 17:00
  • 1
    Hard to understand. Show the bits of your forms code that is not working maybe that gets us an impression with what you are struggling – Ralf Oct 24 '22 at 17:02
  • @Ralf clanovi.Add(new Clan() { KorIme = textBox1.Text, Lozinka = textBox2.Text }); – Zoof Oct 24 '22 at 17:21
  • This part shown in the code above doesn't let me use the textBox1.Text – Zoof Oct 24 '22 at 17:21
  • From here: public void textBox1_TextChanged(object sender, EventArgs e) { } – Zoof Oct 24 '22 at 17:22
  • It does not really make sense to make `textBox1` global by itself because it belongs to the form it is displayed in. However, you can make the *form( global by storing a reference to it in a static variable, then access the `textBox1` property of the global form, e.g. `var text = Globals.MyForm.textBox1.Text` – John Wu Oct 24 '22 at 20:48
  • Read a few C# books to undestand some basic principles of the language before writing code. In your question, it is not clear waht are the different places? Multiple instances of that form displayed? Do you want to remove the associated data when the user close the form? Does the list is really unique across the application? Relying on global variable can make unit testing harder... – Phil1970 Oct 24 '22 at 21:10

2 Answers2

1

Your variable names are in another language so it is hard to understand but I think you want this

internal class Clanovi
{
    public static List<Clan> Dodaj()
    {
        Global.clanovi.Add(new Clan() { KorIme = textBox1.Text, Lozinka = textBox2.Text });
        // you don't need to return this since it is already global
        return clanovi;
    }
}

public static class Global
{
    public static List<Clan> clanovi = new List<Clan>();
}

public static class Clan
{                                          
    public static string KorIme { get; set; }
    public static string Lozinka { get; set; }
}

Whenever you want to access your global variables, you use the Global class with the static items in it.

Hogan
  • 69,564
  • 10
  • 76
  • 117
0

i suggest to:

  1. change the modifiers property in the properties of the textbox to public.

  2. write this code in the other class :

    Form_name/class_name myTextbox = new form_name/class_name();                       
    

    ex:

    Form1 myTextbox = new Form1();

  3. now u can use in any other class/form: mytextbox.Textbox.text.

    internal class Clanovi
    {
       Clan myTextbox = new Clan();
    
       public static List<Clan> Dodaj()
       {
         List<Clan> clanovi = new List<Clan>();
         clanovi.Add(new Clan() { KorIme = myTextbox.textBox1.Text, Lozinka = textBox2.Text });
         return clanovi;
       }
    }
    
Faylasouf
  • 64
  • 6