C
C#14mo ago
Lars Jovars

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

tittapadjurskarm cant access hungry boo, why?
13 Replies
Pobiega
Pobiega14mo 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
Lars Jovars
Lars JovarsOP14mo ago
how do i reach it?
Pobiega
Pobiega14mo 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
Lars Jovars
Lars JovarsOP14mo 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
Pobiega14mo 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
Lars Jovars
Lars JovarsOP14mo ago
changing it to this makes another line red instead
No description
No description
Lars Jovars
Lars JovarsOP14mo ago
No description
Pobiega
Pobiega14mo ago
yeah, because you need to pass the reference there form should likely be this (assuming that code is in Form1)
Lars Jovars
Lars JovarsOP14mo ago
No description
Pobiega
Pobiega14mo ago
no just this
Lars Jovars
Lars JovarsOP14mo ago
changed it to "this" only, no errors anymore, but bool "hungry" doesn't change true when i click vargBtn_Click
Pobiega
Pobiega14mo ago
you sure about that? it seems to me as if you have two hungry on inside Form1 and one in Animal
Accord
Accord14mo 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.
Want results from more Discord servers?
Add your server