-1

I was wondering if there was a way executing code for an element contained in a string. To be more clear, here is an example :

string elementToHide = "welcomePanel";
elementToHide.Visible = false;

What I'm doing here is simple, I have an element stored in a string, and I want to hide this specific element contained in the string : here I want to hide a panel called "welcomePanel" for example.

Is this possible ? Maybe not with a string but with something else ?

Thanks !

Edit : I solved this by using the following code :

string elementToHide = "welcomePan";
((Panel)this.Controls.Find(elementToHide, true)[0]).Visible = false;
  • You didn't store an element in a string. You stored the ID of an element in a string. There's a difference. – mason Apr 22 '21 at 19:10
  • @PeterDuniho Are you sure this is Windows Forms? Could be Web Forms. Or potentially Blazor I guess. – mason Apr 22 '21 at 19:10
  • @mason: Could be anything, but the general approach is the same. I've also added the more general-purpose duplicate that discusses using reflection or dictionaries to accomplish the same thing. Fact is, it's not a useful question, as no matter the context it's already been answered dozens of times, if not hundreds. Whatever additional details the OP provides, we'll be able to find the right duplicate, if these don't suit them. – Peter Duniho Apr 22 '21 at 19:12

1 Answers1

0

You can store the elements in a Dictionary<string, Control> (or whatever is the base class for your elements instead of Control), then do elements["welcomePanel"].Visible = false; or similar.

AKX
  • 152,115
  • 15
  • 115
  • 172