Author Topic: How do Variables work?  (Read 215 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
How do Variables work?
« on: July 20, 2016, 12:25:59 pm »
I am having a hard time understanding how Variables work.

I want to use the device UID for the MQTT publish string (to identify the device publishing). I can do that by defining
Code: [Select]
Dim mcUIDString as String = Device.mcUID().ToString()in each Shared Event (although this may not work, as I had to do it as a two step thing earlier, ie dim an integer, then convert to string).

I thought, OK just define this as a class variable, then use it in each Shared Event, no need to duplicate. Cannot figure this out. Very frustrating >:(. Have read the manual, but the very few examples are obscure and not very helpful.

I would think that you could define the variables as Public or Private Class variables (like many OO languages).

Code: [Select]
Public mcUIDString As String = Device.mcUID().ToString()
Then in a Shared Event use:

Code: [Select]
Lplan.Publish("MCThings/" + mcUIDString + "/Temperature", Temppayload)
No. Error is "Cannot refer to an instance method of a class without an explicit instance of the class". Hmm, add the class name?  ("Temperature.mcUIDString"?). No. "Variable is not a shared member". Add reference to the current instance ("Me.mcUIDString"?). No. same error as first try. I just can't figure out how to use Private or Public like a static variable. Must involve the use of New somehow. Shared variables only take a literal as an initializer, so...

The thing that worked (and is very odd) is:

Code: [Select]
Shared mcUIDString As String
    Shared Event Boot()
        mcUIDString = Device.mcUID().ToString()
    End Event

Now I can use  mcUIDString as a string in my class for the MQTT publish topic, like this :

Code: [Select]
Lplan.Publish("MCThings/" + mcUIDString + "/Temperature", Temppayload)
Is it really this hard? what am I missing? How do you define class variables that you can use in any class sub/function/event like a static variable? I cant define variables outside a class. How do you define a variable that is not class instance specific but common to all instances of that class?

I am familiar with C/C++ and python, not VB.NET.

Any help/explanation is appreciated.

Share on Facebook Share on Twitter


mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: How do Variables work?
« Reply #1 on: July 20, 2016, 01:36:00 pm »
Public and private variables are class scope based. An event does not have a scope so it cannot use Private or Public variables, just like Shared (Static in c#) methods.
To create class scope you have to instantiate (construct) an object. See the example in the attachment:

The class Events (line 1-6) has all events which don't have any scope.
The Measure event (line 2-5) just constructs the Work Class and calls the Measure method.

Because the class Work is instantiated you can define Public and Private variables
The shared method Test (line 19) and event myEvent (line 22) cannot use the variables v1 and v2 because they can be called without instantiating the class. You still can create local variables in shared methods and events like is shown in line 23

If you want to save the class Work between events you have to make it shared and create it in the Boot Event (see second example of the Events Class)

This behavior very standard for all OO languages like C# and Java, but because they are not events based its less prevalent.

Hope this helps

-John-




mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: How do Variables work?
« Reply #2 on: July 20, 2016, 01:37:05 pm »
First example did not load.

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: How do Variables work?
« Reply #3 on: July 20, 2016, 02:43:10 pm »
OK,

I have re-written like this:

Code: [Select]
// This example logs the mc Unique IDentifier (mcUID), Temperature and battery Voltage
// to MQTT.
//

Class Events
    Shared Event Publish() RaiseEvent Every 1 Minutes
        LedGreen = True
        Dim Sensors As Values = New Values
        Lplan.Publish("MCThings/" + Sensors.mcUIDString + "/Uptime", Sensors.GetUptime)
        Lplan.Publish("MCThings/" + Sensors.mcUIDString + "/BatteryVoltage", Sensors.GetBattVoltage)
        Lplan.Publish("MCThings/" + Sensors.mcUIDString + "/Temperature", Sensors.GetTemp)
        LedGreen = False
    End Event 
End Class

Class Values   
    Public mcUIDString As String = Device.mcUID().ToString()
   
    Public Function GetTemp() As ListOfByte
        //LedGreen = True
       
        Dim TempC As Float = TempSensor.GetTemp
        Dim TempString As String = TempC.ToString()
        Dim Temppayload As ListOfByte = New ListOfByte()
       
        Temppayload.Add(TempString)
        Return Temppayload
        //Lplan.Publish("MCThings/" + mcUIDString + "/Temperature", Temppayload)
       
        //LedGreen = False
    End Function
   
    Public Function GetUptime() As ListOfByte
        //LedRed = True
       
        Dim uptime As Integer = Device.Uptime()
        Dim uptimeString As String = uptime.ToString()
        Dim Uptimepayload As ListOfByte = New ListOfByte()
       
        Uptimepayload.Add(uptimeString)
        Return Uptimepayload
       
        //Lplan.Publish("MCThings/" + mcUIDString + "/Uptime", Uptimepayload)
       
        //LedRed = False
    End Function
   
    //Shared Event CheckVoltage() RaiseEvent Every 2 Days
    Public Function GetBattVoltage() As ListOfByte
        Dim BattVolt As Short = Device.BatteryVoltage
        Dim BattString As String = BattVolt.ToString()
        Dim Battpayload As ListOfByte = New ListOfByte()
        Battpayload.Add(BattString)
        Return Battpayload
        //Lplan.Publish("MCThings/" + mcUIDString + "/BatteryVoltage", Battpayload)
        //If BattVolt < 2200 Then
        //Lplan.IFTTT("YOURIFTTTKETHERE", "ProductionRoomBattery/YOURTOPICHERE")
        //Else
       
        //End If
       
    End Function
End Class

This works - sort of (still getting unexplained resets every minute or so).

Good thing you don't need to be a programmer to figure this out, or we would all be stuck.