question about 'metacode' in C#

Jun 4 2006 11:16 AM
Hi all,
Thanks in advance for taking the time to read through my question.


Background:


So, I'm a bit of a disallusioned DBA/systems Admin, who's wanting to get into programming, and out of database/systems administration. I got a copy of Visual Studio 2005 about 3 months ago, and have been working on a nice SQL/C# powered application which I hope to distribute this summer as shareware or as a product, in the hopes of using it as a portfoloio item to switch jobs. Most of it's been coming along great, and it's mostly working as designed.

As you can imagine, this project of mine is meant to help teach me C# and object-oriented programming. Apparently, I've hit a wall, and am missing some fundamental piece of understanding of how to go about object-oriented programing. It has to do with 'metacode'... code about the C# code itself.

So, I'm trying to implement a couple of new features, which I know how to implement in, say, PHP or Perl, but I don't know how to implement in C#. Basically, the features I'm trying to implement have to do with 'metacode', and treating the C# code itself as a string of text and doing operations on the text of the code. I think it has to do with the differences between interpreted versus compiled languages.

Problem:

So, lets say I'm trying to add an 'Add New Item' button to one of the menu strips in my application. When the user clicks on 'New Item' it should created a new button in the application. Something approximately like this:

private void toolStrip_newButtonItem_click (object sender, EnventArgs e)
{

ExtendedButton buttonName = new ExtendedButton();
this.SplitContainer1.Panel1.Controls.Add(this.buttonName);
this.buttonName.Name = "buttonName";
this.buttonName.Size.Height = this.buttonN.Size.Height;
this.buttonName.Size.Width = this.buttonN.Size.Width;
this.buttonName.Location.X = this.buttonN.Location.X;
this.buttonName.Location.Y = this.buttonN.Location.Y + 30;

}


So. Say I have 5 buttons in a row, and by clicking the 'Add New Item', I want to add a sixth button. In the above code snippit, I'd like the program to replace the term 'buttonName' with 'button6', and 'buttonN' with 'button5'. Basically, 'buttonName' and 'buttonN' are strings of text that I'd like to treat as variables and manipulate. But the compiler won't let me! It complains that I'm mixing types. If I try to create a string variable, and do manipulations on that string, then the compiler tries to compile the object as a string, not as a button object. Grrr. (Apparently, I need to rethink my programming methodology here.)

Now, in PHP or PERL, this would be fairly simple. In PHP you would set 'buttonName' and 'buttonN' as variables, and the rest of the code as a text string, which would then write out to an HTML file and that HTML file would then get interpreted. Similarly with PERL; you'd just set the term 'buttonName' as a variable, ala $buttonName

Another example. Say I want to be able to right click these said buttons which I'm creating, and there should be a context menu with 'Rename' and 'Delete' items.

private void contextToolStrip_renameItem_click (object sender, EnventArgs e)
{

TextBox renameBox = new TextBox();
this.SplitContainer1.Panel1.Controls.Add(this.renameBox);
this.renameBox.Name = "renameBox";
this.renameBox.Size.Height = this.buttonN.Size.Height;
this.renameBox.Size.Width = this.buttonN.Size.Width;
this.renameBox.Location.X = this.buttonN.Location.X;
this.renameBox.Location.Y = this.buttonN.Location.Y + 30;

}


In this example, I want to change the value of 'buttonN' to the name of the button that I right-clicked on, so that I can overlay a text box at that space, so the users can rename the box, etc. etc. blah, blah, blah.

Thing is, maybe I don't want to just replace the string, willy-nilly. I'd like to implement metacode. For instance:

$buttonN
1 < N < 5, button is red; location is here
6 < N < 10, button is green; location is there
10 < N, button is blue; location is here and there

or whatever. All of that is simple enough to code up. But how do I get that metacode to interact with the rest of the code? Normally, I'd do this with shell scripting, or PERL, or PHP. What's the equivalent in C#? Do I need to be extending a class? Using an array? Using sometype of keyword which I don't know about? Using a special syntax? Creating a separate class which stores that code as long strings of text with punctuated variables, ala PHP?

I'm not even sure what to call this problem. Maybe somebody could provide some weblinks? And if what I'm trying to do isn't possible in C#, could somebody suggest some other ideas? (I've toyed with the idea of creating a DataGrid and sticking the buttons in that, as the DataGrid could take care of the layout features and would provide incrementing and enumeration features and the like. But I'm not sure I want to go down that route, as it seems a little ugly.)

I'm sure experienced C# programmers have some type of trick or method for doing this kind of stuff, and I'm just being clueless as to what its called, or where to look.

Anyhow, thanks for any responses!