0

I have a problem with the combobox.SelectedIndex function. The problem is that in void and static void it just returns a -1. Now before anyone comes and says if there is anything in it at all, yes it is, I've debugged the code and found that it only returns a -1 in static void and void , now I'm curious as to why it doesn't work, actually a very simple code.

        void writecombo()
        {
            int CURRENT_LOG = combo.SelectedIndex; //< -1 
            Globals.LG[CURRENT_LOG] += "Hello!" + Environment.NewLine; //Crash, because it is -1
            ...
        }

Where it works:

        private void textbox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (GetAsyncKeyState(Keys.Enter) < 0)
            {
                int CURRENT_LOG = combo.SelectedIndex; // Not Null
                Globals.LG[CURRENT_LOG] += "Hello!" + Environment.NewLine;
                ...
            }
        }

I would be happy to get help and also if someone could explain to me why this is the case :)

EDIT: The problem only comes when I want to access this void from a static void, I wanted to use it to access the objects in form1 in a static. (var from = new Form1) MRE

  • Please provide [mcve] – NineBerry Nov 18 '22 at 20:50
  • Most likely you see a different `combo` variable than the one that is the actual combo box in your visible user interface – NineBerry Nov 18 '22 at 20:51
  • While making the MRE, i realized, that the code actually doesn't work because i use var from = new Form1(); For a better explaining, i'll upload the MRE into the question – TheFrieber Nov 18 '22 at 21:00
  • Does this answer your question? [Accessing Form's Controls from another class](https://stackoverflow.com/questions/12983427/accessing-forms-controls-from-another-class) – NineBerry Nov 18 '22 at 21:01
  • Sadly, not, that's even the post, where i got the code from, now afterall, Well, I have to reach the objects through the static void, strangely "var from = new Form1();" does not make this, even tho im calling another void with it – TheFrieber Nov 18 '22 at 21:07
  • You haven't copied the concept. Look at the `form` variable and how it is used. Generally, wanting to access the controls in a form from outside is a sign for a bad design. Please explain in more detail what you are trying to achieve to get better ideas on how to implement it. – NineBerry Nov 18 '22 at 21:12
  • So, I want a form2 to access from1 and run its code to write the logs to its form. I can't access form1 directly because of static. If I don't use static I can't access form1's function from form2 at all. – TheFrieber Nov 18 '22 at 21:28
  • Do you open Form2 from Form1 or are the two forms opened independently from each other? – NineBerry Nov 18 '22 at 21:35
  • Yes, the Form1 is a logger and an executor, the Form2 gets started from an command in the textbox keydown event, and the Form2 later calls an function from Form1 to make Form1 log text into itself(Form2 f2 = new Form2(); f2.Show();) – TheFrieber Nov 18 '22 at 21:40
  • See https://stackoverflow.com/questions/60407037/how-to-access-winforms-parent-form-controls-from-a-child-form – NineBerry Nov 18 '22 at 21:44

2 Answers2

0

void vs static void isn't exactly your problem. It's a static member trying to access the non-static members of its class. That barrier cannot be crossed.

When a member is static, there's only one instance of it, and it's attached to the class; it can only access members of the class that are also marked with the static keyword.

Members that are not marked static can access all the members of the class, including its static members. Each object you create from the class using new essentially gets its own copy of the non-static members.

In your code sample, you're trying to access a non-static member from a static member, and the runtime is telling you that can't be done.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
  • The void still gets executed, but can't access the form1 contents, it acts like an static then.. It accesses it actually, but doesn't get the right values – TheFrieber Nov 18 '22 at 21:31
  • The `void` method you're showing is an event handler on a `Textbox`. Is the combo box on the same form? – Mike Hofer Nov 18 '22 at 21:34
  • Yes it is, the void accepts and sees that those exist, but it doesn't get the values of the items – TheFrieber Nov 18 '22 at 21:37
  • Change the accessibility of the method you are trying to invoke to `public`. Right now, `writeCombo` is internal by `default`, and the event handler is `private`. – Mike Hofer Nov 18 '22 at 21:52
  • "it can only access members of the class that are also marked with the static keyword" is completely wrong (although oft repeated). A `static` method has no `this` parameter, so *unqualified* access is only possible to other static members, but by naming any instance of the class, qualified access is perfectly possible. Consider the very common case of operator overloading: `static Fraction operator+(Fraction left, Fraction right) => new Fraction(left.num * right.den + left.den * right.num, left.den * right.den)` which most certainly accesses non-static members `num` and `den` – Ben Voigt Nov 18 '22 at 22:09
  • Now, the issue is i think the "var from = new Form1();", in the debugging, you can see it's almost null and empty, it still can call other functions that are not static, however all instances like combo is null – TheFrieber Nov 18 '22 at 22:15
0

First of all, thanks to everyone tried to help me. It seems like accessing/calling an static function, to access with that the Form1 contents is a complicated and hard thing to do.

So, i found another way, instead trying to call a function, i am going to use a timer to check a global boolean value, if it is true, it should execute the code, very simple, it's not good, but at least a way to deal with it.

Still, i am opend for answers if anyone found a good and easy way to access the contents of a form with a static function in it.

  • Try calling the code from the UI thread. Even if it does not seem that you are calling it outside the UI thread, you may call it from outside the UI thread, link: https://stackoverflow.com/questions/39513650/accessing-ui-from-outside-thread-in-winforms. If this still does not help it may be that the program is not compiled correctly. I know it may sound foolish, but WinForms applications are not stable at all, and when weird things without explanation happen, is good to clean the solution and recompile it, even multiple times. – teodor mihail Nov 22 '22 at 20:18