.comment-link {margin-left:.6em;}
Hey there, I'm Rowan. I'm currently studying Software Engineering at UTM, and I'm also an IT Entrepreneur. This is my programming blog - you'll find loads of C# stuff in here, as well as MySQL and C tutorials with source. Comments are most welcome.
My Other Blog(s) and Site(s)Friend sites and blogsMy Recent Posts
Tutorials
Best ArticlesMy C-Sharp IdeasArchivesBlog Directories |
Tuesday, July 26, 2005Free Gmail Invites
Yup, 50 free invites
[rowy loves you all, even if you don't C#] I managed to set up Google Ads on my blog. Wasn't easy to make firefox and ie agree on a common template. While strolling on the beach (yeah, I live at the beach. See my other blog), I've been cooking up some other really interesting C# projects (well, I'm still designing the recipe in my head). I was a bit pissed off about my console thingy - something like that already exists (thanks to Peter Jausovec for letting me know) but hey, there still are interesting things to do :D Btw, for you guys out there... I'm giving out free Gmail invites. It's been nearly a year that I'm on Gmail, and I'm kinda tired of seeing all those invites... If you're interested, please leave a comment, or mail me at: hehehe at gmail.com [replace "hehehe" by "rowan.rishi" and "at" by "@"] [you'll get the invite if you're smart enough to do that simple text replace]
Sunday, July 24, 2005An Interesting C# Project
Missing the good ol' DOS console?
Let's revive the past! I'm pretty nostalgic of MS-DOS... I was quite young when I first saw and used MS-DOS, which came with Basic. I used to copy examples from a Basic book, and then test them. That's when I began coding. I was five years old. Languages have greatly evolved since Basic. C, C++, Pascal... and many many others. We're at a time when it'd take only a few months to develop a *really* cool console. However, when I say "cool", I don't mean a console using a single line Edit box, and a Multi line Edit box with a background on it. Hell, no. I want a simple console, which is displayed on a real .Net console, with only a few inbuilt commands. The console would have a "Commands" directory, which would contain compiled .Net assemblies. The console, upon starting, would reflect inside of those dll's, and list out the commands available within them. Of course, all commands would have to implement a "Command" interface, with an Execute method, which would accept as parameters an array of string (the list of arguments which the command should process), a Help method accepting a string as parameter and returning a string (which would provide help on a certain parameter), and a Description read-only property. Does that sound too ambitious? There are loads of problems that might arise during development of such a program. For example: what happens if there are two commands of the same name that exist? Example: Search.dll implements a Delete command Files.dll Implements a Delete command Could this issue be solved just like .Net deals with ambiguous names? Does Search.Delete "expression" sound weird? Hmmm... it's something to look into. But for the moment, let me try to reflect inside assemblies, which aren't my own programs. That might be useful.
Saturday, July 23, 2005Explicit Typecasts
Or also known as tranny-casts
[intermediate] Implicit Typecasts are casts which occur when the compiler cannot directly treat one data type as another, and require "user intervention". By user intervention, I mean, user code :) Trying to typecast a StreamReader into an ArrayList would be a bad idea. That's a typecasting that's not "meant to be" (would raise an InvalidCast Exception), and that's why the user should kick in his/her own code for it to happen. As usual, I made another one of my useless classes, for the purpose of this tutorial. The Data class simply holds a name (string) and a DataValue (int). Also, I kicked in a few lines of code which would allow the data class to be typecasted as string (thus returning the name), or typecast as int (returning the DataValue). Check it out below:
The only part of this code that interests us is:
Note that those two methods are static, and marked with the explicit and operator keywords. The first method simply contains code that is going to be executed if the user decides to typecast into a string. Note that the return type (i.e. the operation of the casting) is a string. The value in parameters is a Data class, i.e. the data class that's being typecasted into a string. The next lines of code are pretty easy. The name of the Data class is extracted by looking into the Data class passed into parameters, and this value returned (it's a string anyway). And that's all there is to explicit typecasting. The second method does pretty much the same, but allows typecasting into an int (notice: the return type's an int). Here's how I explicitly typecasted my Data class into a string, and then an int.
Yup... and it works :) The complete source code can be found below:
A few typeof(System.Random) thoughts
I thought up of the advantages of coding vs. going out with someone:
Why is coding better than going out with someone? 1. Your programs use only your processing time. 2. Your programs use only your pc's resources. 3. Problems with a program can be solved by debugging. 4. Programs can be terminated any time you wish. 5. Programs don't mind when they execute with other programs. 6. Programs carry out all the commands you give them. Without bitching you. 7. Programs don't lie. 8. Big programming mistakes at worse end up with a system reboot or re-install. I just went through shit to fix my blog's template. I added a few meta tags, and suddenly decided that maybe I should try viewing it in Internet Explorer. Yeah, well, hehe, I use Firefox. The template was totally fucked up, with my menu below the main posts... Jesus Christ... spent about half an hour trying to find a balance between "good looking", "IE Compatible" and "Firefox Compatible". Well, template's fixed, and it all looks fine. I even managed to stretch the orange seperator... I got a few ideas for new tutorials. I wonder when I'll have time to write them down though. I'll write about chaining constructors, implicit and explicit typecasts, some beginner's stuff about Interfaces, and maybe Datasets. Ah, yeah. Datasets. Would that be beginner or intermediate? Hmm... Maybe I can cook up several tutorials on datasets - one for beginners and one for intermediates. Also, I wanted to write something about NUnit, and good programming practises. Hmm... I'll do that soon enough. Anyway, I've been reading (or trying to read) the CSharp 2.0 Specs document from Microsoft. I know, that's kinda old - I heard that CSharp 3.0 specs are coming out soon. I think that the new features of C# have been throughly detailed in many places throughout the web, but there's something I'd love to comment on, especially about the Generic types, and the Nullable types. I believe that C# code's really understandable, and pretty neat as well. However, with the introduction of the "<", ">", "?" and also "::" signs, the face of C# as we all know it is going to change. What I mean by "the face of C#" is how general C# code looks like. C# code with Generics and nullable types look pretty weird. Okay, maybe that's because I'm not used to see that kind of C# code... but there's something telling me that C# code might look very nasty in Whidbey. On the whole, the CSharp 2.0 specs come round with loads of good news. Here's a quick summary of a few of the interesting things I saw: 1. Generics I think that's one of the most publicized things about Whidbey. An example of the use of Generics could be an Arraylist, that's forced to accept only one kind of object, through the use of Generics. I.e. You can create an arraylist that accepts only strings, or integers. Writing generic classes, however, might get a little bit confusing. 2. Nullable types Well, simply put, a nullable type is a data type which has, apart from its value, a boolean value indicating whether the value on it has been set or not. So you can perform checks on variables, to see whether they've been initialized or not, through the HasValue property. 3. Easier implementation of IEnumerable Those of you who think that it's troublesome to implement IEnumerable (like me, for example), will be overjoyed to hear that there's now a much easier method to do it, thanks to the yield newly-introduced yeild break and yeild return keywords. Hmm... well... there's more to Whidbey than what I've described here, namely anonymous methods and so on, but seriously, it's getting kinda late here. I think I'll go to sleep for a change. Have a nice day, and may the code be with you.
Friday, July 22, 2005Situation of Programmers in MauritiusEvery little kid out of high school wants to study computer science and engineering in university. However, many of them are just into comp. science because it's something popular to study. Yes, it's a "popular" subject. I wouldn't recommend computer science and engineering to people who just don’t know what they’re going to do later. You’ll end up hating the coding, and coding is what you’ll spend your time doing. Personally, I eat, drink, sleep and live with code. I don’t think I’ll get bored of coding someday. Coding… or designing software / classes. But you might. And you’ll hate your work, the company you work for, and your life in the end. Anyway, you might also turn out to be a great coder, and actually enjoy coding. Who knows? But don’t rush into computer science and engineering just because it’s the only best thing to study right after agriculture. Now, a little update for you guys. As soon as you’re out of university, you won’t be earning a lot. Nope. A certain company in Good programming practices are what will make of you a better coder. You can either learn the hard way – i.e. code, code, and keep coding. You’ll eventually understand why people use try’s and catch’es. You’ll understand why people put comments in their code, why people use NUnit. You could also start learning about good programming practices right now. There are tips and hits everywhere on the net As someone previously pointed out to me, the education system in here is all about spoon-feeding. That’s why most Mauritian programmers would back off as soon as they hear that they got to implement a shortest-path algorithm on a graph. “Hell, isn’t there a method to do that in C#?” Jesus fucking Christ. C# does have a lot of methods, but they’re here to make your life easier, not to do all the work. What, did you expect to find a method WankMe()? Well, FuckYou(). You need to work on your coding skills. Else, That said, let us not forget that there really are some good coders in this country. People who are really motivated to learn more, sharpen their skills, and become good and better coders. It’s all about the code.
Sex and C#
Single 19 (soon to be 20) year old male, looking for female of the same age range for friendship or better, preferably living in Mauritius, sharing same passions about C#. Must understand polymorphism, be able to use enumerated types, must know how to chain constructors and use indexers, must have a vague idea (or better) of what MSIL could be and must know how to use different threading models in C#. Preference will be given to candidates who have knowledge of ASP.Net and to candidates who have big breasts.
Any intimate encounter will occur in XML.
Yet another cool and useful feature
The Params keyword
[Beginner] The params keyword could be one of the coolest features of C-Sharp, if you haven't heard of it yet. Simply put, it allows you to enter multiple parameters of a same type. An alternative to this could be the use of an array - i.e. create a method that accepts an array of a certain kind of objects, but that's really troublesome. Here, check the code out - it's pretty simple and straightforward:
What really interests us is the AddAll method. Let's have a closer look:
There's nothing particular in that method, apart from the "params" keyword used in the parameters. It's pretty strightforward. The use of the params in the parameters allows users to pass multiple values (in this case, multiple int's) of the same type - they all get kicked into an array of that object type (in this example, an array of int, which I named "numbers"). Using this function gets really easy now. All you have to do is to call the AddAll method with as much number of integer parameters as you want! Et voilà! Rien de plus façile! I called the AddAll method, and passed in a few numbers - and called the ToString() method. Now, think of your own ways to make a good use of the params keyword ;)
Thursday, July 21, 2005A few basic things
Decimal Places
[Beginner] There are quite a few things to know before really getting the hang of C#, and I believe that learning about how to format numbers that need to be output is something pretty basic, that many programmers disregard. However, here's a basic tutorial about controlling the number of decimal places that a decimal type displays. Let's move on directly to the code. It's simple. Nothing complicated to understand. Check it out:
Notice that I've included System.Globalization in the usings. This is a namespace that is required in order to use the NumberFormatInfo class. What I did was basically to instantiate a NumberFormatInfo class, and play with the NumberDecimalDigits property (which I kept incrementing). Watch the results on a console :) Also, note the use of the overloaded ToString() method, in which I passed two parameters, namely a string ("N"), and the NumberFormatInfo class that I instantiated. Hope this little code snippet helps!
C# News
The graph below shows the number of times programmers of different languages got dates or were turned down while asking for a date.
C# programmers enjoy a more sexually-active life. Experts can only agree. After an interview with Jack (fake name), an application optimizer (he uses ASM), we found out the real truth behind the lives of programmers who made bad choices. "I was under a lot of stress at home. I kept having fights with my wife, Tabitha (fake name). I knew it was because of it [the programming language I used]. I had to change, but I wasn't very sure which one. I went for VB. That, I think, was the worse decision of all my life." Like Jack, many programmers suffered the choices of their language. Ben (fake name), a C++ programmer earning US$4000 per month, was caught by his wife using Java at home. "She couldn't believe it. She sent me the divorce papers one week later," confides Ben, still trying to recover from his loss. Some, however, seem to have understood what a programmer's life was all about. Sonya (fake name), a 24 year old hottie, agreed to share information on her programming past with us. "Before, I couldn't even get laid. Guys wouldn't even look at me. I was a Java programmer." Her sad expression reflects her unhappy past. "Fortunately, a friend of mine introduced me to C#. I easily got the hang of it, and found the language very comfortable to use." We had doubts about Sonya's claims on her inactive sexual life, when she explained to us: "I couldn't believe it. After two months of using C#, my breasts started to grow larger, and my butt and thighs more firm. It was incredible!". Sonya now enjoys a peaceful life with Brandon (fake name), also a C# programmer. They're expecting a baby this November. "If it's a boy, we're gonna call him Whidbey," the happy couple told us.
Indexers
Indexers in C#
Intermediate I've been up to nothing good on C#, and I thought I'd post something about indexers on C#. Being able to provide a collection class with an indexer could be a real cool thing, if you know how to do it :) Indexers allow your class(es) to be treated like arrays - an "index" is passed in between square brackets, and an object is returned. Just like an arraylist. Or an array :) To begin my second C# tutorial, please take a look at the utterly useless Item class:
Well, as you can see, the Item class doesn't do anything particular. Of course, it's just a dummy class, created for the purpose of this tutorial. What would interest you would be the ItemHolder class - a class that can actually accept a collection of items. Check out the code below:
Do take a closer look at the indexer:
This is what we're interested in. Disregard the code - it's just exceptions I couldn't resist throwing just in case the wrong type's being used (I'm forcing the use of Item types of objects inside the collection). The indexer doesn't get more complicated than
Where the first DataType Now, for the code inside the indexer - Notice that my indexer directly returns an Item (saves you the typecasting, heh), given an int - which represents the index value of the Item inside of my collection. When the user tries to get an item, the get part of the accessor is executed. I made a little check on the value of the index being passed. Passing -1 would most certainly raise an exception - as well as passing a value outside of the bounds of the collection. In the set accessor, I had to carry out even more checks - apart from the bounds checking, I also made a little check on the type of value being set. I modified the code a little bit, because using "is" creates more solid code than using GetType() (see below). The is keyword checks for parent objects, and the GetType() only checks for the type. Below is the code that I replaced:
Instead of:
The set accessor first checks whether the index passed resides within the collection's bounds (more than -1, less than the count of number of items), and then checks for the type being set; after modification of the code, an object derived from Item will also be accepted in the Indexer. Note that I also implemented an AddItem method, which allows users to kick in Items into the collection (without which, this whole class would be useless), and a Count property, which allows users to get the number of total items inside of the class. Of course, I could've elaborated, implemented a Delete method and so on, but that's up to you. Below is the code that I used to make the Item and ItemHolder classes work:
Here's the complete code for the tutorial:
Thursday, July 14, 2005IComparer Interface
Sorting out your problems
[Intermediate] I made a small but cute class for Person - there's Name, which identifies the person's name and there's Age, which represents the person's age. Creating multiple instances of the persons class would require me to kick them all into an ArrayList - which I did. I had a precise idea in mind: I wanted the ArrayList to sort my list of Persons by either name or age. This is when I learnt about the IComparer and IComparable interfaces. However, in this tutorial, we're going to focus on the IComparer interface. Please note that the IComparer interface requires the use of System.Collections. Below is the code for my Person class.
A class implementing the IComparer interface can be passed onto the ArrayList.Sort method to provide the latter with a means of comparing two values. Why compare two values? Simple, because the QuickSort algorithm (which the arraylist obviously implements) works by consecutively comparing two values. Any quicksort algorithm can easily be adapted on a set of data, given the comparing method. What's left to do is to create a class which implements the IComparer method. Also, we might wish to allow for "customization" of that class, i.e. allow different means of sorting. To do this, I created an enumerated type which would hold the different types of sorting. See the code below:
The code above contains an enumerated type and a SortPersons class, which implements the IComparer interface. The SortPersons class has a SortPersonsBy property, which allows the user to choose between sorting by name and sorting by age. When the ArrayList.Sort() method is called with a SortPersons class, the Compare method is executed numerous times. First of all, my Compare method checks for the types of objects - i.e. whether they are persons or not. If one of them isn't a Person class, the Compare method returns 0. If they both are Person classes, the type of sorting is checked. If the type of sorting is set to "byName", the string's CompareTo method is called using the two person's names; if the type of sorting is set to "byAge", the int's CompareTo method is called using the two person's ages. And that's it! Here's the full source code of my app:
Introduction
Do You See Sharp?
Welcome to my Blog! It's not exactly a blog, it's more of a tutorial-oriented consecutive series of html pages organised into a blog. I don't really have time (nor the necessary funds :( ) to buy webspace - but hey! Blogger's here to help :) Anyway, let me post this and move on to the first C# tutorial! Rowy
|