2009-10-16

Reference types

Quick reminder: Any object you have to instantiate (e.g. create with the new keyword) is what’s called a Reference Type. It means that, when used as a method parameter, it will always be treated as a reference parameter (ref in C#, ByRef in VB).

In other words, this is redundant:

  1: void ModifyData(ref DataSet data)
  2: {
  3:     //....
  4: }

Because DataSet is a reference type, the ref keyword in the above examples serves no use. 

Opposite to reference types are Value Types, like an int or a string. They have to specify the ref keyword in the parameter if they want to be treated as such.

Here’s a good article on reference and value types.

See ya.

2009-10-14

Setting an enum value from a string

In a piece of code I was reviewing I saw that they wanted to set the value for an enum from a string, and what they did was to create a large method with a big switch statement, setting the correct enum value for each case. Something like:

  1: enum Operators
  2: {
  3:    GreaterThan,
  4:    Equals,
  5:    LessThan
  6: }
  7: 
  8: Operators GetOperatorFromString(string s)
  9: {
 10:    switch(s.ToUpper())
 11:    {
 12:       case "GREATERTHAN":
 13:          return Operators.GreaterThan;
 14:       case "EQUALS":
 15:          return Operators.Equals;
 16:       case "LESSTHAN":
 17:          return Operators.LessThan;
 18:    }
 19: }

Now, this could be kind of simple if your enum doesn’t have many values, but if we’re talking about very large lists, then we have a problem…

Well, the solution for this is actually very simple, by using System.Enum.Parse, which has the following signature:

object Enum.Parse(System.Type enumType, string value, bool ignoreCase);

So, you can write the following:

  1: enum Operators
  2: {
  3:    GreaterThan,
  4:    Equals,
  5:    LessThan
  6: }
  7: 
  8: // ...
  9: 
 10: Operators o = (Operators)Enum.Parse(typeof(Operators), "greaterthan", true);

Notice that, since we have the ingnoreCase parameter set to true,  we can use the string in any kind of case. If the string value doesn’t match any of the enum values, an exception of type ArgumentException will be raised.

2009-10-06

Quick Windows tips

This has nothing to do with programming, and probably many people already know it, but what the hell. It might be a nice thing to teach your end users.

First, to get a screenshot of just the active window, instead of your whole screen, just press the Alt and PrintScreen keys together. That way, you’ll copy the screenshot to your clipboard, but you’ll not get your task bar, and you wouldn’t have to go to Paint to chop your image.

Second, to get the text of a message box shown by an app, just make sure that box is active, and press Ctrl + C. Windows will automatically copy the title, message and buttons text to the clipboard. You’ll get something like this:

---------------------------
Exit
---------------------------
Are you sure you want to quit?
---------------------------
Yes   No  
---------------------------