Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm not sure this is a very understandable explanation of what I am trying todo but here goes.... Using C# is it possible to take a string for example Control.Type.ToString() and then cast another object to that type using the string.

 

So essentially what I want todo is to create a Type, using a string, then cast an object to that type.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Essentially the project I am working is an application that can read a data file (xml), and construct objects from the information it reads in. These objects are user controls created by myself. At the moment the way it works is it will read in a string such as "Matrix", it will then loop through a large Switch statement, find the case for "Matrix", and run code like this...

 

Matrix myMatrix = new Matrix();

 

This works all very well at the moment, but in the future I may wish to create some new controls, when I do this I was hoping to be able to integrate them into my application without having to change the code of the main application. Thus the controls could be like 'plugins'.

Anybody looking for a graduate programmer (Midlands, England)?
Posted
I looked up this command on MSDN and it states that it must be passed a Type. For this reason I'm not understanding how the command would be usefull. All of my controls inherit from a MasterControl, so obviously I could pass that type which would assumably allow me to access the main inherited properties, but each control contains custom properties not implemented in the master control, so surely there would be no way of accessing them using this method.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

Maybe I misunderstood your problem but in the near future I'll have to implement a similar functionality --> saving custom controls to a xml-file and loading them again.

There's an interface (forgot it's name) that let's you easily serialze object to xml format but I was told that it can't handle inherited objects.

My approach will be to have one root object let's call it "document". The "document" will be able to serialize my custom controls and generate objects as well.

 

My custom controls only need one method and one extra constructor:

- the method to serialize that returns their fields in valid XML

- the constructor to generate the object getting the saved fields passed as parameters

 

When saving the document calls the serialize method of each control and to generate the "document" goes through the xml file and calls the constructor for each "control-tag" it finds Of course the "document" needs to know which tag in the xml-file represents a control but thats a matter of using xml properly.

 

Using XPath and XLS you will end up having a more "cleaner" implementation of that functionality than to parse the file manually and use a switch statement.

 

It takes little more time in the beginning but when it comes to adding new controls it pays off.

Debug me...
Posted
I guess I'm not quite understanding how that method works divil. Since all the properties of a control will need setting (using data from the xml) when it is added to my "Document", and some of the controls have properties not implemented in the Master control, how do i then set these properties, without casting to a control of that type.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

Cags.

 

If you do want to implement a number of known controls I have a very simple answer (as I have just had to do this myself). I use a treeview control on the left side of the form and a panel on the right and, dependant on which node gets clicked the relevant control gets displayed (not very neat but works a treat). You would just read your xml instead of using a treeview click event.

 

As I implement each control with the same set of functions it also means that I can loo[ through each control and call the same things each time like LoadSettings(), HasErrors() or SaveSettings etc.

 

The code looks something like this......

 

UserControl CurrentControl = new UserControl();

MyControl1 mc1;

MtControl2 mc2;

 

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)

{

if (e.node.text == "anytext1")

{

if (CurrentControl != null)

CurrentControl.Visible = false;

mc1.Parent = Panel1;

mc1.Dock = DockStyle.Fill;

CurrentControl = mc1;

CurrentControl.visible = true;

}

else if (e.node.text == "anytext2")

{

if (CurrentControl != null)

CurrentControl.Visible = false;

mc2.Parent = Panel1;

mc2.Dock = DockStyle.Fill;

CurrentControl = mc2;

CurrentControl.visible = true;

}

}

Posted

I'm obviously not explaining myself very well because nobody seems to understand what i'm trying todo (either that or I'm not understanding them). My application currently works fine with a known amount of controls, but I would like it to work with an unknown amount. Essentially what I want todo is eliminate the switch statement so that i do not have to change the code of the main application in order to support extra controls. Heres an example of the structure of my application...

 

My control strucure is as follows...

 

MainControl:

Properties:

UniqueID

Heading

MustComplete

 

ChildControl1: Inherits Main Control

Extra Properties:

Count

Labels[]

 

ChildControl2: Inherits Main Control

Extra Properties:

ColumnCount

RowCount

 

And the way I would currently create my "Document" is as follows...

 

private void LoadControls()
{
string sType = ReadXML(Type);

Switch(sType)
{
 Case "ChildControl1":
   ChildControl1 myChild1 = new ChildControl1();
   LoadCommonProperties(myChild1); 
   myChild1.Count = ReadXML(Count); // reads count from xml
   myChild1.Labels = ReadXML(Labels); // reads in the labels from xml
   myChild1.Parent = myPanel;
   break;
 Case "ChildControl2":
   ChildControl2 myChild2 = new ChildControl2();
   LoadCommonProperties(myChild1); 
   myChild1.RowCount = ReadXML(RowCount); // reads count from xml
   myChild1.ColumnCount = ReadXML(ColumnCount); // reads count from xml
   myChild1.Parent = myPanel;   
   break;
}
}

private void LoadCommonProperties(object myControl)
{
 MainControl myTmpControl = new (MainControl)myControl
 myControl.UniqueID = ReadXML(ID);
 myControl.Heading = ReadXML(Heading);
 myControl.MustComplete = ReadXML(MustComplete);
}

 

Although in this example I read in the properties application side, I could obviously just add a method to the control todo this, and just pass it the xml. But the thing thats got me is in order todo this, I first need an object of the type ChildControl1, or ChildControl2, so that it calls the correct LoadProperty method.

 

Any Ideas?

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...