Category: vb.net

SharpDevelop 2.1

authorOmar | 2. April 2007

The latest version of SharpDevelop was released nearly a month ago bringing with it some nice features i’ve been waiting for. My main interest is the support of the Compact Framework.

Anyone trying to develop .net-applications will normally start off by using Microsoft’s free (but stripped down) IDE MS Visual Studio Express. Initially I was astonished to hear that Microsoft released the Express edition even for commercial use (!!) without many strings attached.

Later, once i found out about SharpDevelop I was looking for a method to develop applications for my pda-like xda. Unfortunately Microsoft has reserved Compact Framework support for professional users of Visual Studio. Now with the release of SharpDevelop 2.1 it’s possible to create applications for use in Microsoft’s Windows Mobile platform.

Structures in vb.net and c#

authorOmar | 22. März 2007

Ever since i 'discovered' how to use structures in vb.net, I've been fascinated by the abilities they give to developers. Structures are sort of mini-classes, that can have properties and methods. Take for example the following structure:

VB.NET:
  1. Structure measurement
  2.     Dim startdate As DateTime
  3.     Dim enddate As DateTime
  4.     Dim heattemp As Double
  5.     Dim heattime As Double
  6.     Dim overalltime As Double
  7.     Dim title As String
  8. End Structure

It describes a complete measurement-cycle of a heating device. The syntax for using structures is as simple as it gets:

VB.NET:
  1. Dim meas As new measurement
  2. meas.title = "hallo"

Next, it'll need some measurement points. I'll just create a new structure

VB.NET:
  1. Structure measpoint
  2.     Dim time as DateTime
  3.     Dim tempt1 As Double
  4.     Dim tempt2 As Double
  5.     Dim tempb1 As Double
  6.     Dim tempb2 As Double
  7.     Dim volt As Double
  8.     Dim currt As Double
  9. End Structure

This structure describes exactly one measurement point. In it we find the current time stamp, four temperature values, a current and a voltage value. Every measurements has an undefined number of measurement-points. To include them into the main measurement-structure, we use an array:

VB.NET:
  1. Structure measurement
  2.     Dim startdate As DateTime
  3.     Dim enddate As DateTime
  4.     Dim heattemp As Double
  5.     Dim heattime As Double
  6.     Dim overalltime As Double
  7.     Dim title As String
  8.     Dim mpoints() As measpoint
  9. End Structure

Next, we'll need some evaluation of measurement-values. To do that, we'll include a method into the measurement-structure:

VB.NET:
  1. Structure measurement
  2.     Dim startdate As DateTime
  3.     Dim enddate As DateTime
  4.     Dim heattemp As Double
  5.     Dim heattime As Double
  6.     Dim overalltime As Double
  7.     Dim title As String
  8.     Dim mpoints() As measpoint
  9.     Public Function lowestvolt() as double
  10.         Dim lv As Double
  11.         Dim i As Integer
  12.         lv=mpoints(0).volt
  13.         For i = 1 To mpoints.Length - 1
  14.             If lv>mpoints(i).volt Then
  15.                 lv=mpoints(i).volt
  16.             End If
  17.         Next
  18.         lowestvolt=lv
  19.     End Function
  20. End Structure

The method 'lowestvolt' iterates through mpoints and finds the lowest voltage. That's it!

Using the free IDE sharpdevelop we can now translate the code into c#:

C#:
  1. public struct measpoint
  2. {
  3.     public DateTime time;
  4.     public double tempt1;
  5.     public double tempt2;
  6.     public double tempb1;
  7.     public double tempb2;
  8.     public double volt;
  9.     public double currt;
  10. }
  11. public struct measurement
  12. {
  13.     public DateTime startdate;
  14.     public DateTime enddate;
  15.     public double heattemp;
  16.     public double heattime;
  17.     public double overalltime;
  18.     public measpoint[] mpoints;
  19.     public double lowestvolt()
  20.     {
  21.         double lv;
  22.         int i;
  23.         lv = mpoints(0).volt;
  24.         for (i = 1; i <= mpoints.Length - 1; i++) {
  25.             if (lv> mpoints(i).volt)
  26.             {
  27.                 lv = mpoints(i).volt;
  28.             }
  29.         }
  30.         return lv;
  31.     }
  32. }

So, why still use classes, when there is something as nice as structures? Well, structures can have properties, methods and even constructors but the main difference to classes is that structures are sterile, they can't inherit from other structures. Also, there are no private properties/methods in structures as there are in classes. Other than that, there is no major difference other that that structures are much easier to use.. Since I don't really use inheritance much, structures are my first choice for organizing data.