Posts Tagged ‘object’

C# .NET Programming Tip: Types

Thursday, February 21st, 2008

Figuring out a variable’s type has become more important since now variables can be boxed by their parent class(s) (Where all can be “Object”). It’s nice because it allows for one generalized function to work with many types that perform an action on a common attribute, or first figure out what the object is and then perform the action.

That is one of the instances where figuring out a variables type is important.

.NET has a built-in function called GetType() which figures out what type a variable is.

//loop through the controls in the panel anf figure out which of the checkboxes are checked
foreach (Control panelControl in newCheckListQuestionPanel.Controls)
{
   //only continue if this control is a checkbox
   if (panelControl.GetType().ToString().Equals("System.Windows.Forms.CheckBox") == true)
   {
       if (((CheckBox)panelControl).Checked == true)
       {
           //do something here if the control is checked
       }
   }
}

Is object orientated scripting worth the effort?

Sunday, January 27th, 2008

I’m currently creating a movie review website in PHP an SQLite. I’m taking a very object orientated approach.

For example, all database access goes through an object. Adding a review is as simple as calling a function with the proper data.

Here is the function header:

addReview($assocArrayReviewData, $assocArrayReviewBlockData)

Each parameter is an associative array that corresponds to fields in the database. While I don’t want to get into too much detail, the second variable is actually an array of arrays, where each one holds a block of review information (eg: “What the box says”, “funny quotes”, “My thoughts”).

The best thing about is is that once I create the administration interface I can just call that one function with the correct parameters and bam, a new review.

The class also has a built-in search function (that could be used on the internal or external site).

My next class that I created for primarily for the administration area. It’s an html tag class object. You might me thinking, what’s the point of mimicking html tags? Well in my never ending quest to separate html from code, this object can be instantiated instead of writing html dispersed through the script code. Please forgive me on the naming. I’m not quite sure what to call the class yet. I first called a “form” class, then a “control” class, and then once I realized it could be used for anything… the “htmlTag” class. Well anyways, it does what I want it to do regardless of what it is called.

Here is a quick example of code that would call it:

$test = new HtmlTag('test', 'form');
$test->setAttribute('method', 'post');
$test->createSubControl('button1', 'input');
$test->subControl('button1')->setAttribute('type', 'button');
$test->subControl('button1')->setAttribute('value', 'Click Here!');
echo $test->toHtml();

Any idea what it does?

It creates this:


Pretty cool, eh? The whole class is a beefy ~220 lines. haha
Now I can create forms programmatically that can have an unlimited number of parent/children/sub-children to them. So a form can hold a select box, that holds option tags… etc.

Here is a bit of the class file:

class HtmlTag
{
    //strings:
    private $controlType; //stores the type of control (this should correspond to an actual html tag)
    private $innerHtml; //hold any text or whatever inside the control's start and end tags
    
    //associative arrays:
    private $attributes; //hold all of the attributes of the control (eg: onsubmit='' onclick='' style=''...)
    
    private $subControls; //holds sub controls (eg: form has controls inside it, select control has options inside it)

..................

 function &subControl($name)
 {
     return $this->subControls[$name];
 }

}
    

The subControl function is key to accessing children and sub-children of the main object. I wanted to keep the array subControls private, so that function is necessary.

Now that I have that class created I will start designing the administration interface, that now, is just a shell that accesses these two classes.





 

 
Stock Photo Website
Tech Learning Site

Popular Article Tags

Recent Article Comments

Archives