Structures in vb.net and c#
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:
-
Structure measurement
-
Dim startdate As DateTime
-
Dim enddate As DateTime
-
Dim heattemp As Double
-
Dim heattime As Double
-
Dim overalltime As Double
-
Dim title As String
-
End Structure
It describes a complete measurement-cycle of a heating device. The syntax for using structures is as simple as it gets:
-
Dim meas As new measurement
-
meas.title = "hallo"
Next, it'll need some measurement points. I'll just create a new structure
-
Structure measpoint
-
Dim time as DateTime
-
Dim tempt1 As Double
-
Dim tempt2 As Double
-
Dim tempb1 As Double
-
Dim tempb2 As Double
-
Dim volt As Double
-
Dim currt As Double
-
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:
-
Structure measurement
-
Dim startdate As DateTime
-
Dim enddate As DateTime
-
Dim heattemp As Double
-
Dim heattime As Double
-
Dim overalltime As Double
-
Dim title As String
-
Dim mpoints() As measpoint
-
End Structure
Next, we'll need some evaluation of measurement-values. To do that, we'll include a method into the measurement-structure:
-
Structure measurement
-
Dim startdate As DateTime
-
Dim enddate As DateTime
-
Dim heattemp As Double
-
Dim heattime As Double
-
Dim overalltime As Double
-
Dim title As String
-
Dim mpoints() As measpoint
-
Public Function lowestvolt() as double
-
Dim lv As Double
-
Dim i As Integer
-
lv=mpoints(0).volt
-
For i = 1 To mpoints.Length - 1
-
If lv>mpoints(i).volt Then
-
lv=mpoints(i).volt
-
End If
-
Next
-
lowestvolt=lv
-
End Function
-
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#:
-
public struct measpoint
-
{
-
public DateTime time;
-
public double tempt1;
-
public double tempt2;
-
public double tempb1;
-
public double tempb2;
-
public double volt;
-
public double currt;
-
}
-
public struct measurement
-
{
-
public DateTime startdate;
-
public DateTime enddate;
-
public double heattemp;
-
public double heattime;
-
public double overalltime;
-
public measpoint[] mpoints;
-
public double lowestvolt()
-
{
-
double lv;
-
int i;
-
lv = mpoints(0).volt;
-
for (i = 1; i <= mpoints.Length - 1; i++) {
-
if (lv> mpoints(i).volt)
-
{
-
lv = mpoints(i).volt;
-
}
-
}
-
return lv;
-
}
-
}
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.
Comments
Other Links to this Post
RSS-Feed für Kommentare zu diesem Beitrag. TrackBack URI
By , 13. September 2007 @ 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!
By , 13. September 2007 @ 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..