Too Much Cookies Network

Structures in vb.net and c#

Donnerstag, 22. März 2007, 13:29

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.

2 Kommentare

Kommentar von

Made Donnerstag, 13 of September , 2007 at 18:11

Thanx for showing me how to build a Structure, but how do I use it? I need to populate a form based on a selection from a DDBox that was populated using a Collection. How do I do that?

Thanx!

Kommentar von

Made Donnerstag, 13 of September , 2007 at 18:19

@Myron: a structure can be used like any other variable. You can simply set the variables (struct.var=”hello”) or read it. Of course you can have an array of a structures (see Structure measurement) so you can populate it from a collection.. If you could specify your question, perhaps i could help..

Kommentar hinterlassen

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About

Seit November 2004 berichtet 'Too Much Cookies Network' live und radikal aus der Parallelgesellschaft. Die Themenwahl ist willkürlich, der Sprachstil filigran und der Gegner unklar. Zum Netzwerk gehören weiterhin folgende Seiten: