Creating a person life-cycle with C# events

I’ve made a basic simulation. It’s not overly complex, but I think it’s a pretty good use of the C# events. In my tiny simulation people are born, taxed, and die. It’s a bit of a morbid outlook on life, but then I suppose these aren’t real people so they can’t have any greater purpose or profound insight so I can dispose of them with a relatively easy mind.

I’m particularly proud of this because in my admittedly basic system everything basically handles itself. Each person class its own life-cycle which runs on its own background thread.

public Person(int PassedTaxes)
{
     Taxes = PassedTaxes;
     t = new Thread(LifeCycle);
     t.Start();
}

public void LifeCycle()
{
     PersonBorn(this, new EventArgs());
     Thread.Sleep(5000);
     PersonTaxed(this, new EventArgs());
     Thread.Sleep(5000);
     PersonDied(this, new EventArgs());
}

This means that when it comes time to managing each of these people in my main program they can all be added or removed from the big list of people whenever the ‘PersonDied’ event had been triggered.

This is an admittedly simple example of using events, but since the person life-cycle is now essentially self contained, I can spend a lot more time fiddling with the more fun aspects of the program. For example, now that everything runs on its own clock and I don’t have to implement constant checks I can continually alter the the rate at which people are produced since I’m soundly assured that individual life-cycles are accounted for. I could probably implement variable lifespans too.

One of the best bits is that I can now add new functionality to every ‘person’ in my simulation by just adding new events to that life-cycle. For example, I’ve just implemented a 50/50 chance that my person will divorce somewhere between taxation and death (not the happiest 15 second life I know, maybe I’ll throw in a birthday party event if I start feeling altruistic).SimpleSimulationInterface

My simple simulator’s interface

As I say, hardly the most complex bits of software out there, but using my own life-cycle is probably something I’m going to do a good deal more of in future projects.

Advertisements

Simple note keeper (my recurring project)

A few years ago I received a very nice Android Tablet as a Christmas present. I’ve had a lot of fun using it normally, but eventually I decided that I’d quite like to run my own software on it.

The first program I could think to make was something that could communicate data back to a server. I’m not entirely sure why I’ve decided on this but the idea of communicating from a central device back to a server which does the real work has a certain appeal to it.

The first time I did this I tried to communicate back a list of people and associated information. This worked but the GUI for both the client and server were horrible. Additionally this was my first run-in with Android programming and I developed a deep-seated loathing for Eclipse (something I’ve found I can readily extend to Android Studio).

The second time I tried this I decided that I wouldn’t make an android specific program, but instead it’d be all browser-based. The server would spit out the webpage the webpage would send back the data to the server, and then on the server-side cut down the post message to retrieve the data.

Today’s version of the program is a simple note keeper which once again involves and Android-based client (interface below).

SimpleNoteKeeperInterface

The goal for this program is for the application to create a simple file on the device and for the user to be able to send this file to the server when it’s accessible. Its a very simple design from the user’s end: set the note’s title in the top box, set the note’s contents in the larger box below, and add the note to the list of notes with the topmost note button.

Files will be transferred if possible when the user clicks on a Send button (noticeable absent in the above image). This will send the entire list of notes to the server. In order to make the networking aspect somewhat justified in my mind, the client will contact the server when it opens and give the user to download any notes that are not present on the client. I suppose this networking aspect is just present to prevent data loss in the event of some monumental crash.

Now that I’ve created a record of it I’ll hopefully have some kind of mental motivation to finish the thing.

Thanks for reading.

A first blog

After a few years of being told that it would be a good idea to setup an online space to document the type of work you do I finally decided to do it. Last year.

I may as well start this blog with a recap of exactly what went wrong the last time. I decided that it would be a good idea to have my own online space. It would be interesting to have my own domain and to dictate how it was used. Since I enjoy creating things I then decided to make my own webpage powered by AJAX and PHP. I used this to fetch descriptions of the webpages for when the user hovered over the pages button, and to build a search function based on the user’s input.

My website worked, but, as is often the case when you ignore a perfectly good solution and run off with your own nut-ball idea, it didn’t work particularly well. It didn’t scale well to different screen sizes and I’d used fonts that weren’t available on android devices. It was a mess from a user interface point of view.

This side-project cost a not insignificant amount of cash so this time I’ve decided that someone else can shoulder the cost of what could generously be described as ‘my work’ (thanks WordPress).

Hopefully this blog will serve as a useful tool where someone can find something useful. If not it’ll at least serve as a useful reminder of the work I’ve done and stop me making the same program over again (yep!).

Hope you enjoy.