Approaches to Web Development for Bioinformatics

Developing Applications with C#

Previous  Contents  Next 
References

Types in C#

C# is a strongly typed language. This is contrary to scripting languages like Perl, PHP, and JavaScript where the type is determined internally by the interpreter and it can change dynamically. Data types in C# are either value types, which store the values or reference types, which store references to data. Value types include numeric types, Boolean types, and struct types. Assigning one value type to another value type copies the data.

A reference type can be either a class, an interface, or a delegate.

What you might want to do in a web application is to get data from a database and process it in C#. Having done that you might pass this to an ASP page to present the results. The kind of class that you might need in carrying data from the database to the web page might look something like GeneInfo in the example below. There is also a test class to exercise the GeneInfo class printing the gene information out to the console. The source code is in GeneInfo.cs.


C#

using System;

// Encapsulates a gene information record
public class GeneInfo
{
int id;
string symbol, name;

public GeneInfo( int id, string symbol, string name )
{
this.id = id;
this.symbol = symbol;
this.name = name;
}

public override string ToString()
{
return "Gene, id: " + id + ", symbol: " + symbol + ", name: " + name;
}
}

// Exercise the GeneInfo class.
class TestGeneInfo
{

public static void Main()
{
GeneInfo[] geneInfos = new GeneInfo[2];
geneInfos[0] = new GeneInfo(3064, "HD", "huntingtin (Huntington disease)");
geneInfos[1] = new GeneInfo(6736, "SRY", "sex determining region Y");
for (int i=0; i<2; i++)
{
Console.WriteLine(geneInfos[i]);
}
}
}

This example demonstrates the class fields (id, symbol, and name). These values are set in the constructor, a method with the same name as the class used to initialize an instance of the class.

The method ToString() is defined to help in printing out the field values. The base ToString() method is defined on Object to provide a convenient method to convert a value to a string. This also demonstrates the use of the override keyword, which is a feature that Java does not have. If the override keyword were not used I would not get the behavior I am looking for - a convenient method to print out information about the object.

In C# inheritance is specified via an : operator in the class definition. For example, public class Tiger:Animal means that the class Tiger extends the class Animal. I do not have much use for inheritance but you will see it in examples elsewhere in the literature.

Interfaces specify the methods and properties of classes. Implementation of C# interfaces is also specified via an : operator.

A delegate is a reference type used to encapsulate a named or anonymous method. They are similar to function pointers in other languages but they have a type associated with them. They are used extensively for events in C#.

C# has built-in reference types for object and string. A variable of type object can take the value of from a value type. For example,


object start;
start = 1;

In this case, the variable start is said to be "boxed".

A string is a sequence of Unicode characters. The == operator compares strings and the + concatenates them. Individual characters are accessed with the [] operator. Here is an example that demonstrates a few of the concepts just discussed. The code is in file RNAValidator.cs.


C#

using System;

// Interface to validate a string supplied by a user or another program
public interface Validator {

bool isValid(string inputString);

}

// Validates that a string is a valid sequence of RNA nucleotides
public class RNAValidator : Validator
{

public bool isValid(string inputString)
{
for (int i=0; i<inputString.Length; i++)
{
if (inputString[i] != 'a' && inputString[i] != 'g' &&
inputString[i] != 'c' && inputString[i] != 'u')
{
Console.WriteLine("Illegal symbol: {0}.", inputString[i]);
return false;
}
}
return true;
}

// Exercises the RNAValidator class
static int Main(string[] args)
{
if (args.Length != 1) {
Console.WriteLine("Useage: RNAValidator rna_string");
return -1;
}
Validator validator = new RNAValidator();
bool ok = validator.isValid(args[0]);
if (ok)
{
Console.WriteLine( "{0} is valid.", args[0]);
}
else
{
Console.WriteLine("{0} is not valid.", args[0]);
}
return 0;
}
}

This program defines a Validator interface. The intent of this interface is to check whether the given string is valid from some, unspecified point of view. This could be used, for example, in a web form where we want to validate user input but don't want to tie the presentation logic to the science logic. The RNAValidator class implements the Validator interface to check whether the given string is a valid RNA string. Starting execution in the Main method the program the program declares a variable validator of type Validator. Notice the declaration using the interface. The program then instantiates an object of type RNAValidator. If we truly wanted the code making use of the Validator to remain oblivious of the implementation we would use some kind of plug-in or factory pattern to instantiate it somewhere else.

The idea of iterating through each character in a string and checking its validity with an if statement is tedious and will not work for more complex examples. Let's look at a better approach using regular expressions.


Previous  Contents  Next 
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies