Approaches to Web Development for Bioinformatics

Developing Applications with C#

Previous  Contents  Next 
References

Properties and Indexers

Properties are special methods called accessors that are used to read, write, or compute the values of private fields. Here is an example for sequence data. The code is in file Sequence.cs.


C#

// Class to represent a sequence of biological data (DNA, RNA, or protein)
class Sequence
{
private string annotation, data;

public Sequence(string _annotation, string _data)
{
this.annotation = _annotation;
this.data = _data;
}

public string Annotation
{
get { return annotation; }
set { annotation = value; }
}

public string Data
{
get { return data; }
}

}

// Exercises the Sequence class
class SequenceTest
{

static void Main()
{
Sequence sequence = new Sequence(
"gi|90903230|ref|NM_002111.6| Homo sapiens huntingtin (Huntington disease) (HD), mRNA",
"GCTGCCGGGACGGGTCCAAGATGGACGGCCGCTCAGGTTCTGCTTTTACCTGCGGCCCAGAGCCCCATTC");

// Assigning the Annotation property causes the 'set' accessor to be called.
sequence.Annotation = sequence.Annotation + ". Some additional notes from Alex.";

// Evaluating the properties causes the 'get' accessors to be called.
System.Console.WriteLine("Annotation: " + sequence.Annotation);
System.Console.WriteLine("Data: " + sequence.Data);
}

}

The class Sequence has two properties: Annotation and Data. Annotation is readable and writable. Data is ready-only. The class SequenceTest invokes the getter and setter methods using a syntax that looks like regular assignment. Properties can be declared in both classes and interfaces.

In a set accessor there is an implicit parameter named value. Get accessor should be used to get or compute values. For example, the get accessor may actually get the value from a database or web service. A set accessor may do some validation on the supplied value before saving it into a private field. The output from this program is


>Sequence.exe
Annotation: gi|90903230|ref|NM_002111.6| Homo sapiens huntingtin (Huntington disease) (HD), mRNA. Some additional notes from Alex.
Data: GCTGCCGGGACGGGTCCAAGATGGACGGCCGCTCAGGTTCTGCTTTTACCTGCGGCCCAGAGCCCCATTC

Indexers are similar to properties. They allow instances of a class to be indexed in a similar way to arrays. Here is a simple example to demonstrate the concept.


C#

// Class to demonstrates Indexers
public class SequenceCollection
{
private string[] sequences = new string[100];

public string this[int i]
{
get
{
return sequences[i];
}
set
{
sequences[i] = value;
}
}
}

// Exercises the SequenceCollection class
class SequenceCollectionTest
{
static void Main(string[] args)
{
SequenceCollection sequenceCollection = new SequenceCollection();
sequenceCollection[0] = "GCTGCCGGGACGGGTCCAAGATGGAC";
System.Console.WriteLine(sequenceCollection[0]);
}
}

The this keyword is used to define the indexers. The value keyword is used for the value being assigned in the set accessor. The output of the program is


>SequenceCollection.exe
GCTGCCGGGACGGGTCCAAGATGGAC

Indexers can be declared on interfaces as well. However, in that case they do not have a body.


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