C
C#9mo ago
Hob

❔ Why cant i access hungry bool in user control when clicking on vargBTN_click?

tittapadjurskarm cant access hungry boo, why?
13 Replies
Pobiega
Pobiega9mo ago
because hungry is a member of a Form1 instance, not a global thing TittaPåDjurSkärm is unrelated to Form1 the way you have it currently
Hob
Hob9mo ago
how do i reach it?
Pobiega
Pobiega9mo ago
The easiest way is to provide a reference of Form1 to TittaPåDjurSkärm, probably via the constructor but at that point you're hard-coupling this control to that form, which makes me wonder why this is a separate control at all
Hob
Hob9mo ago
im new to c# and the instructor told me to import my code into forms. i barely understand Windows Forms so i dont know how to make it a non separate control
Pobiega
Pobiega9mo ago
Okay, well, lets solve it the easy and ugly way then Add a new argument to your constructor, Form1 mainForm or something like this then save that in a private field
Hob
Hob9mo ago
changing it to this makes another line red instead
No description
No description
Hob
Hob9mo ago
No description
Pobiega
Pobiega9mo ago
yeah, because you need to pass the reference there form should likely be this (assuming that code is in Form1)
Hob
Hob9mo ago
No description
Pobiega
Pobiega9mo ago
no just this
Hob
Hob9mo ago
changed it to "this" only, no errors anymore, but bool "hungry" doesn't change true when i click vargBtn_Click
Pobiega
Pobiega9mo ago
you sure about that? it seems to me as if you have two hungry on inside Form1 and one in Animal
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.