-1

string Go_Page .... string variable contains name of instance userControl. I am trying to manipulate instance userControl by name (string Go_Page ) . help pls

            string Go_Page;


            if (!FrmMain.panelMain.Controls.Contains(Go_Page.Instance))
            {
                FrmMain.panelMain.Controls.Clear();
                FrmMain.panelMain.Controls.Add(Go_Page.Instance);
                Go_Page.Instance.Dock = DockStyle.Fill;
                Go_Page.Instance.BringToFront();
            }
            else
            { Go_Page.Instance.BringToFront(); }

i get this Error :

Error CS1061 'string' does not contain definition for 'Instance'

  • https://stackoverflow.com/questions/24865628/string-does-not-contain-a-definition-for-error-in-c-sharp – Salah Akbari Jul 29 '19 at 02:51
  • 1
    https://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls – Salah Akbari Jul 29 '19 at 02:51
  • Possible duplicate of ['string' does not contain a definition for/ Error in C#](https://stackoverflow.com/questions/24865628/string-does-not-contain-a-definition-for-error-in-c-sharp) – MarkusEgle Jul 29 '19 at 04:17
  • `panelMain` looks to be some sort of GUI control. What framework are you using? [tag:winforms]? [tag:wpf]? [tag:xamarin.forms]? Something else? Also, your code doesn't seem like it would compile. Should the code starting with `if (UC_Vars_PROTECT == "PS")` be outside the `switch` statement? Can you please [edit] your question to clarify? – dbc Jul 29 '19 at 05:36
  • i'm using winforms framework, and yes if statement should be outside the switch statement. But I'm still confused how should I make it work. I have to check if the instance inside panelMain , if not panelMain.Controls.Add(instance) – bennaya marwen Jul 29 '19 at 17:41
  • Well in that case see [Get control by name, including children](https://stackoverflow.com/q/8737091) or [Find control by name from Windows Forms controls](https://stackoverflow.com/q/3898588/3744182). You would use those answers to find a child control of `panelMain` with the specified name, then if not present add it. Does that answer your question? – dbc Jul 29 '19 at 18:01
  • Also, is `UC_Vars_PSNP` the name of the control, or the name of the **c# type** of the control? Your question text says the latter but the title says the former. – dbc Jul 29 '19 at 18:07
  • it's custom user_control . like a frame . UC_Vars_PSNP the name of the control – bennaya marwen Jul 29 '19 at 23:09
  • OK then what happened when you tried the solutions from [Get control by name, including children](https://stackoverflow.com/q/8737091) and/or [Find control by name from Windows Forms controls](https://stackoverflow.com/q/3898588/3744182)? Did those work? If not, can you share a [mcve]? – dbc Jul 30 '19 at 01:10

2 Answers2

1

Try this method to convert string to class instance:

If user control class in current assembly,use:

Assembly assembly = Assembly.GetExecutingAssembly();

else:

Assembly assembly = Assembly.LoadFile("AssemblyPath");

Class Instance:

object obj = assembly.CreateInstance("ClassFullName"); //contains namespace

Or Try:

Type type = Type.GetType("ClassFullName"); //contains namespace
object obj = Activator.CreateInstance(type);
melody zhou
  • 148
  • 4
  • Good answer, but you need a public default constructor to use this `Activator.CreateInstance` overload. There is also an [overload](https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8#System_Activator_CreateInstance_System_Type_System_Object___) which accept constructor parameters. – vasily.sib Jul 29 '19 at 03:39
  • Yes,```assembly.CreateInstance``` also can accept constructor parameters by args ```CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)``` – melody zhou Jul 29 '19 at 05:44
  • The bigger problem with this answer is that it's entirely possible that the OP is trying to manipulate an _existing_ instance, e.g. a singleton implementation, of the `UserControl` in question. Creating a _new_ instance is useless in that scenario. – Peter Duniho Jul 29 '19 at 19:25
  • 1
    yes its a singleton so I'm trying to manipulate an existing instance by name witch is this `string UC_Vars_PSNP`. – bennaya marwen Jul 30 '19 at 00:36
  • If set control's fixed name,you can try ```panelMain.Controls.ContainsKey("control's name")``` – melody zhou Jul 30 '19 at 01:57
0
if (!panel.Controls.Contains(UCMangerReport.Instance))
            {
                panel.Controls.Add(UCMangerReport.Instance);
                UCMangerReport.Instance.Dock = DockStyle.Fill;
                UCMangerReport.Instance.BringToFront();
            }
            else
            {
                UCMangerReport.Instance.BringToFront();
            }

Error=>Error3'appReport.UCMangerReport' does not contain a definition for 'Instance'
  • 1
    You should generally accompany your answer with an explanation of what you fixed/changed so that it is clear for readers – sshashank124 Dec 30 '19 at 07:29