2

I am trying to get this PID control code into my VB.net project, but am a bit clueless about all the exclamation marks. Can someone explain to me how to implement this code in VB.NET?

Dim Er!, Derivative!, Proportional!

    Static Olderror!, Cont!, Integral!
    Static Limiter_Switch%
    Limiter_Switch% = 1

    Er = setpoint - process

    If ((Cont >= 1 And Er > 0) Or (Cont <= 0 And Er < 0) Or (Integ >= 9999)) Then
        Limiter_Switch = 0
    Else
        Limiter_Switch = 1
    End If
    Integral = Integral + Gain / Integ * Er * deltaT * Limiter_Switch

    Derivative = Gain * deriv * (Er - Olderror) / deltaT

    Proportional = Gain * Er

    Cont = Proportional + Integral + Derivative
    Olderror = Er

    If (Cont > 1) Then
        Cont = 1
    End If
    If (Cont < 0) Then
        Cont = 0
    End If
    Return ()
Dave H
  • 653
  • 12
  • 22
  • 1
    See this question/ answers http://stackoverflow.com/questions/9373135/significance-of-an-ampersand-in-vb6-function-name/9373296#9373296 – Mark Hall Sep 18 '13 at 11:53
  • To add to what the other answers have said, this technique of declaring variable type by a symbol is called a sigil: [https://en.wikipedia.org/wiki/Sigil_(computer_programming)](https://en.wikipedia.org/wiki/Sigil_%28computer_programming%29) – Boann Sep 24 '13 at 15:16

2 Answers2

10

In VB6, certain suffixes can be added to specify a variable type. For instance:

Dim x%

Is the same as:

Dim x As Integer

The suffixes are still supported in VB.NET, but they are widely discouraged. Here is the list of possible suffixes:

  • $ is the suffix for String
  • % is the suffix for Integer
  • & is the suffix for Long
  • ! is the suffix for Single
  • # is the suffix for Double
  • @ is the suffix for Currency (now Decimal in .NET)

VB6 did not provide suffix characters for all of the core data types. For instance, there is no valid suffix character for Boolean, Date, or Short. Even in VB6, many people recommended always using As in all variable declarations, but there were still many people who recommended using the suffixes, where available, because they provided some additional pre-compile type-checking which was often beneficial.

So, to convert that code to .NET, you'd want to replace the suffix symbols in any variable declaration lines with an As ... clause, specifying the equivalent type, for istance, instead of this:

Dim Er!, Derivative!, Proportional!
Static Olderror!, Cont!, Integral!
Static Limiter_Switch%

You'd convert it to this:

Dim Er, Derivative, Proportional As Single
Static OldError, Cont, Integral As Single
Static Limiter_Switch As Integer

And then, where ever a suffix symbol appears when a variable is being used, outside of a declaration line, you can just remove the symbol. For instance, instead of this:

Limiter_Switch% = 1

You'd convert it to this:

Limiter_Switch = 1

Bear in mind, when converting types from VB6 to VB.NET, that the numeric types in VB.NET are larger. So for instance, Integer in VB6 is 16-bit, but in VB.NET, Integer is 32-bit. So, technically, the equivalent in VB.NET, for a VB6 Integer is Short. Typically, it doesn't matter, and you just want to use Integer for Integer, but if the number of bits matters, you need to be careful.

Boann
  • 48,794
  • 16
  • 117
  • 146
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
0

These symbols specified the variable type in older versions of MS Basic, up to VB6. % = Integer, &=Long, !=Single, #=Double, @=Currency, $=string

peterG
  • 1,651
  • 3
  • 14
  • 23