adamkdean

software engineering

Simple abstract classes explained

By Adam K Dean on

Blogular activity has suffered a little here; but setting up a workshop and devising plans to build steam engine in said workshop will do that to you. Today some simple code, written to simplify showing abstract classes. Every tutorial I see they throw in stupid code that isn't really needed to show the primal functionality of abstract classes. So here is an example that has nothing more than what it needs.

class Program
{
    static void Main()
    {
        // here we create a happy and pass it to print mood to screen
        // happy is of type happy but also of type mood
        Happy happy = new Happy();
        PrintMoodToScreen(happy);

        // again, we create a mood, this one is a sad mood but still
        // a mood nonetheless
        Sad sad = new Sad();
        PrintMoodToScreen(sad);

        Console.ReadKey();
    }

    // here we take an object of the type Mood, which means we don't require
    // one method for sad, one method for happy and one for all others
    // and it means we dont have to box them into objects either
    static void PrintMoodToScreen(Mood mood)
    {
        mood.PrintMood();
    }
}

As you can see we pass the objects to a single method taking a Mood type, not Sad or Happy. Think about your local vets, you may have a GermanShepherd or a Poodle, but they are all derived from the Dog class. If that isn't enough, think about coffee, beautiful coffee, we have Coffee, then we have InstantCoffee and GroundCoffee, and then we have KenkoInstantCoffee and NescafeInstantCoffee, each would be a child of the other, Coffee -> InstantCoffee -> KenkoInstantCoffee etc. You could write a method to only accept instant coffee and then takes water and milk and makes a nice quick 'normal' brew.

abstract class Mood
{
    // protected means it's accessible to Mood, and to anything derived
    // from it, i.e. sad, happy, etc
    protected string CurrentMood { get; set; }

    // here we write a method to print the mood, we only need to code
    // it once, that's one reason why abstract classes exist
    public void PrintMood()
    {
        Console.WriteLine(CurrentMood);
    }
}

// we create the happy class and derive from mood
class Happy : Mood
{
    public Happy()
    {
        // we set the protected property, which is accessible to us
        // but not to anyone using a mood object
        CurrentMood = "Current Mood is Happy :)";
    }
}

class Sad : Mood
{
    public Sad()
    {
        // cheer up Sad class!
        CurrentMood = "Current Mood is Sad :(";
    }
}

If more explaining is needed, then I'm really not sure if there's any hope for you.

Good luck!

Dynamic CSS with ASP.NET - in one line of code

By Adam K Dean on

Whether you want a separate sheet for each browser, or you want to track when people print a page from your site (as is my case), how do you get code to run in a CSS file? You can't. How do you get an ASPX file to serve up CSS? Well, you can't. But like House MD, I have the answer and I'll tell you eventually.

What you want to do is create an ASPX file, and in the Page_Load event, write whatever tracking or logic code you want, then simply redirect the page without any output.

protected void Page_Load(object sender, EventArgs e)
{
    // code here
    Response.Redirect("style.css", true);
}

And there you have it, Dynamic CSS with ASP.NET - in one line of code.

Get Average using SQL

By Adam K Dean on

More SQL again today, to say I'm not a fan, I'm doing a lot of it lately.

Todays snippet is quite cool, I was inspired by this post: http://www.dreamincode.net/forums/topic/190437-floating-point-division/

Basically, you return the average by counting up the price of every item in a table, then divide it by the number of rows in said table. All in SQL.

SELECT (SELECT SUM(Price) FROM t_Items) /
    (SELECT COUNT(*) FROM t_Items)
        AS Expr1

Enjoy!

Indexers - make your classes act like arrays

By Adam K Dean on

Indexers are great, I've only really started using them consciously today, but they're a great discovery I'll be sure to incorporate into more of my code from now on.

As you can see here, we have a class, but we access a value as if it was an array, or a Hashtable. Just think of how you could use this!

VarClass vc = new VarClass();
vc["key"] = "value";
Console.WriteLine(vc["key"].ToString());

The code is really simple too, it's simply like a property with an input, here we just put a layer on top of a Hashtable, you can of course add something more useful, but this shows you how it works:

class VarClass
{
    private Hashtable ht = new Hashtable();

    public object this[object key]
    {
        get
        {
            return ht[key];
        }
        set
        {
            ht[key] = value;
        }
    }
}  

As you can see, you take the object they put within the square brackets as a parameter, and you make a property but as the name you put the keyword this, referring to the actual class object.

I just think it's so simply, yet so awesome..

I actually came across this whilst writing a simple Hashtable clone for someone over at Dream.In.Code. For the sake of your dying curiosity I will post the awesome class here:

class HashtableEx
{
    private List<object> keys = new List<object>();
    private List<object> values = new List<object>();

    public object this[object key]
    {
        get
        {
            int index = keys.IndexOf(key);
            if (index == -1) return null;
            else return values[index];
        }
        set
        {
            if (keys.Contains(key))
            {
                int index = keys.IndexOf(key);
                values[index] = value;
            }
            else
            {
                keys.Add(key);
                values.Add(value);
            }
        }
    }

    public object Get(object key)
    {
        int index = keys.IndexOf(key);
        return values[index];
    }

    public void Add(object key, object value)
    {
        keys.Add(key);
        values.Add(value);
    }

    public void Remove(object key)
    {
        int index = keys.IndexOf(key);
        keys.RemoveAt(index);
        values.RemoveAt(index);
    }

    public void Clear()
    {
        keys = new List<object>();
        values = new List<object>();
    }
}

Use it like so:

static void Main()
{
    HashtableEx ht = new HashtableEx();

    ht["hello"] = "world";

    Console.WriteLine("Value of \"hello\": {0}", ht["hello"]);
    Console.WriteLine("Type of \"hello\": {0}", ht["hello"].GetType());

    Console.ReadKey();
}

Which gives you the output:

Value of "hello": world
Type of "hello": System.String

Sql threesomes.. Two foreign keys, one primary key

By Adam K Dean on

So here I am, coding my own business, being a law abiding coder, when out of the blue comes a rather bothersome little problem which really did one on me. I had laid out an awesome plan for my awesome top secret project, a truly awesomely smart database design with relationships to link a rows in a table to rows in the exact same table, one way relationships from A->B. All I needed was a way to cascade them on delete, in other words, make sure when a row is deleted, every relationship (in the relationship table) is deleted along with it.

Are things ever that simple? MSSQL decided to tell me that because one field had a foreign key (FK) to the primary key (PK), the other field couldn't also have an FK to the PK, and that's not okay (OK). That's !OK ..

Luckily, SQL has things called triggers, and they're actually pretty useful. The below SQL creates a trigger that deletes any rows from the second table, that have the same id as the row being deleted from the first table:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==============================================
-- Author: Adam K Dean
-- Website: http://imdsm.blogspot.com/
-- Create date: 12/09/10
-- Description: Deletes relationships on deletion
-- of row in the main database. Replaces the need
-- for two foreign keys on the same primary key
-- which is unfortunately impossible.
-- ==============================================
ALTER TRIGGER DeleteRelationships ON t_Sites AFTER DELETE
AS
BEGIN

 DELETE FROM t_Relationships 
 WHERE ReferrerId IN (SELECT Id FROM Deleted)
  OR TargetId IN (SELECT Id FROM Deleted)

END
GO

Somehow I managed to make this work, finding snippets and testing them, because lets face it, the SQL examples have so many options it's like a wall of text has just hit you at 150 pages per second, not really as easy to use as looking at some chinese hackers code and picking out the bits you need.

It seems the Deleted object refers to the deleted row, and well the rest is pretty simple.

Hope this helps you, or future me, if you're suffering the same Jeremy SQ-yle problems as I was!