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
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).
Public mcUIDString As String = Device.mcUID().ToString()
Then in a Shared Event use:
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:
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 :
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.