Journey into C#
Being a moderatly to quite skilled programmer since 15 years, the past four years PHP exclusive, I have for various reasons decided to take up C#.
This blog is for sharing my ups and downs with fellow C# coders, beginners and pros alike.
tisdag, augusti 24, 2004
Custom attributes
Today I got through the section about custom attributes. At first I didn't really understand why you would want to use them, it just seemed like a cumbersome way to declare variables but afterwards I understand more about them, although I'm not completely sure about a practical use for them at the moment. As usual.
The author mentioned that you could use custom attributes in a database connection scenario, declaring the table and columns in attributes, in order to avoid coding up custom logic for each table and column you want to access. Admittely I am a bit tired now but I'm not sure how this would work. Perhaps like this?
[AttributeUsage(AttributeTargets.Property, AllowMultiple = False)]
public class TableNameAttribute : Attribute
{
private string tableName;
public TableNameAttribute(string tableName)
{
this.tableName = tableName;
}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = True)]
public class ColumnAttribute : Attribute
{
private string columnName;
public ColumnAttribute(string columnName)
{
this.columnName = columnName;
}
}
[TableName("Customers")]
[Column("Name")]
[Column("Address")]
[Column("Phone")]
public bool Update()
{
// What the heck do I do here to use the attributes?
// Do I need to use GetCustomAttributes()
// and if so, how do I point it to
// this class?
}
All in all it seems like a clumsy way of doing things. Why not just do like this:
public bool Update(string tableName, array columns)
{
// Use the parameters passed to the class to update DB.
}
Or am I missing something?
onsdag, augusti 18, 2004
Dictionaries
I've just finished reading about dictionaries, something that might be unique to C#. Then again, I haven't coded in any modern OOP language before this so what do I know?
At first I was unimpressed with dictionaries, all it seemed to be was a way to use strings to index an array. Big deal, I can do that in PHP without breaking a sweat. I changed my mind after reading the chapter though.
Now they seem like something taken out of The Matrix; not only resizing themselves as needed but also using magic prime number based algorithms coded by Microsoft to grab the key, which can be any object of your liking, and search through the load for the actual object stored god knows where. If you screw up your own
GetHashCode() and Equals() methods, your data can get lost forever in the vast darkness of cyberspace, never to be found again.
I'm sure they're quite clever and very versatile but right now I have a hard time seeing why they are more powerful than PHP's arrays. Or perhaps PHP is using dictionaries as well?
Still, it's knowing about these kind of things that makes me more confident that I one day will actually be able to write proper C# code.
måndag, augusti 16, 2004
Sorry for the lack of updates
There has been other projects taking up my time recently, one of them is C# related but, believe it or not, even more secret than the one I'm about to write for the school I work for so I can't talk about that one at all. At least not yet.
Things are moving along though, I have two of my friends helping out on the schoolrelated project. They too are learning C# as we go along although one of them has studied Java and OOP recently so he's got a leg up on the rest of us.
Hopefully I'll be back tomorrow with a proper update.
söndag, augusti 08, 2004
My thoughts on delegates
The more I read the more I learn about exciting new concepts in C#. One of these concepts is delegates. In short, IIRC, delegates are unique to C# and is a way of referencing methods allowing for your code to apply these methods from within another method, even though the metod referenced otherwise might be hidden from the particular method referencing it.
Wow, that was easy to follow wasn't it?
Anyhow, delegates are quite useful and helps immensely in many situations. There is this one thing that irks me though. As Simon Robinson puts it:
With classes there are two distinct terms - 'class', which indicates the broader definition, and 'object' which means an instance of the class. Unfortunately with delegates there is only the one term. When you create an instance of a delegate, what you have created is also referred to as 'a delegate'.How hard could it have been to come up with two terms? I say we help Microsoft out here and create the other term for them.
My thinking here is that 'delegate' should be analogous to 'object' so what we need is a term analogous to 'class', but for delegates. But that's just my view on things, perhaps there is a good term for an instance of a delegate?
torsdag, augusti 05, 2004
Under the weather
I seem to talk a lot about weather in my posts, don't I? Unfortunately I'm not talking about the sun this time but about the cold I've gotten recently. I've had a sore throat the entire week but yesterday I got a lot worse.
I'm actually looking forward to coming home and reading more about C#, and that's a great feeling, but when I get home work and this cold has drained me from pretty much all my energy so I've done nothing more than watching TV the past days. 8(
As soon as I finish this post I'll sit down and start reading though, before I get to tired. In a worst case scenario I'll just have to re-read it when my head's cleared from all the snot I suppose. 8)
tisdag, augusti 03, 2004
Inheritance and exceptions
I've been quite tired the past days so I only got through another 35 pages last night, covering some more of inheritance. I'm kinda puzzled though how inheriting from interfaces works. I know multiple inheritance isn't allowed in C# but what if I want to inherit functionality from a class and the functionality from an interface? Is there any way to combine that? Hopefully further reading will reveal this.
I've read another cople of sections today, this time about exceptions and although they seem a bit... tedious, in spite of a better word, they do seem to implement some very useful functionality I'm not used to at all considering my programming background. It seems like you need to really think through when to use them though.
måndag, augusti 02, 2004
The weather takes it's toll
Sorry for the lack of updates but the weather has been quite brilliant the past week, something you really can't say for the rest of the summer. So I have been spending much of my time outside, instead of in front of the computer. I hope you don't mind (and if you live in Sweden hopefully you've been outside too while it's not raining).
I'll get back to reading some more later tonight as I want to have a bit more knowledge below my belt before I start off with a bigger project. It's been a while since I left school though so it's tough to sit down and study like this.
