❔ Why cant i access hungry bool in user control when clicking on vargBTN_click?
tittapadjurskarm cant access hungry boo, why?
13 Replies
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 currentlyhow do i reach it?
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 allim 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
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 fieldchanging it to this makes another line red instead
yeah, because you need to pass the reference there
form
should likely be this
(assuming that code is in Form1)no just
this
changed it to "this" only, no errors anymore, but bool "hungry" doesn't change true when i click vargBtn_Click
you sure about that?
it seems to me as if you have two
hungry
on inside Form1
and one in Animal
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.