Help please with a function

  • Thread starter Thread starter David Smith
  • Start date Start date
D

David Smith

Im trying the following code: (pardon the indentations...weird newsreader)

The obvious problem here is that the editPanel object is defined within the
blocks, and out of scope outside of the switch statement where I want to set
some basic parameters before diplaying it (Its a usercontrol).

Anyway, Im trying to avoid creating a "base" object to inherit from in order
to use this code, and I cant cast the object since the later code has no
idea what type it is. Any suggestions?

Thanks

David

private void ShowEditPanel(string editPanelName)

{

switch (editPanelName)

{

case "General":

{

Vortex.UC_AccountGeneral editPanel;

editPanel = new UC_AccountGeneral();

break;

}

case "Numbers":

{

Vortex.UC_AccountNumbers editPanel;

editPanel = new UC_AccountNumbers();

break;

}

case "Address":

{

Vortex.UC_AccountAddress editPanel;

editPanel = new UC_AccountAddress();

break;

}

}



// Set location of panel

editPanel.Account = this.Account;

editPanel.Location = new System.Drawing.Point(128, 64);

editPanel.Width = this.Width - panel2.Width - 35;

editPanel.Height = this.Height - 110;

editPanel.Anchor =

(((

System.Windows.Forms.AnchorStyles.Top |

System.Windows.Forms.AnchorStyles.Bottom)|

System.Windows.Forms.AnchorStyles.Left) |

System.Windows.Forms.AnchorStyles.Right);

Controls.Add(editPanel);

editPanel.Show();

editPanel.BringToFront();

}
 
It does not seem to be a very good approach. The problem that you mentioned
can be dealt with though. You will need to iterate through all the controls
on your form and try to cast all three types, if you do not hit the error,
then it is the one that you need. But this is very cumbersome and will slow
down your code. You also can use Tag property or Name property of this
custom control to catch it. But again, you will have to iterate through
controls on your form.
I think that better way would be to declare all three of them using
different names and make them available in your form. Then you can create a
function that will check wich one of them is not null, and use it
accordingly.
 
Back
Top