Timezone: Europe/London

.

AboutContactMicropythonESP32Links
Moonlight Systems

A very basic problem...

Not Python related, but I thought it merited a place here, if only to demonstrate the perils of complacency.
I have been writing code in various programming languages since the 1990s. I have written in Dbase, SQL, 4GL, PHP, C, C++ to name a few.
Possibly, I've written the most in Visual Basic, and today I found out something I was unaware of all that time.
As I am self taught, I absolutely own up to the fact I have mis-read or mis-interpreted the documents at some point, but in thousands of lines of code over 25 years, what I am talking about has never been a problem, not become evident until recently.

By now, if you are still reading, I suspect you are thinking either 'Ah ha, this person has discovered the VB anomaly we all knew about', or 'I wonder if it’s something I assumed too?'. So here it is.

When is a string not a string? When it's a variant, apparently.

I learnt VB programming from several books, and the help and examples from Microsoft. I always declare my variables at the beginning of the procedure, and as stated somewhere in the docs, you can declare multiple variables on a line. So I got into the habit of keeping all variables of the same type on a single line.

The above code works just fine.

If you have got this far, and know what the problem is, there is not much point in continuing except to confirm you were right, but maybe there will be somebody reading this thinking “that all looks OK to me”. 99% of the time, coding like this would work as expected, the only downside, which is not often a problem anyway, is the code would take up more space in memory.
The problem occurs when you pass these variables to functions. Consider the following.

In the preceding code, change the line fullname = firstname & " " & lastname to fullname = Join_names (firstname, lastname)

When you try to run the code, up pops this error.

I’m ashamed to say, it took me quite a while to get to the bottom of this. As I saw it, the function required strings, and I was passing it strings.
But it turns out I wasn’t.

VBA has a data type ‘Variant’, and this can hold any kind of data type, Int, String, Long, etc and I believe objects as well. As far as I know VB is the only language that has this, and I am not really sure if it is a good idea or not. I for one always steered clear of explicitly declaring Variants. What I did not know, was to me, the entirely illogical way VB declares variables on a line. Dim Var1, Var2, Var3 As String, I would logically expect to have declared 3 strings, but no.
Var3 is a string, because it appears directly before the As keyword.
Var1 and Var2 are Variants, because there is no As keyword for them.

So in the above example I would be passing one string and one variant to the function rather than two strings, and therein lies the problem. It is also demonstrates why, especially in a language where data types have to be explicitly declared, it seems strange to include a 'wild card'. Absolutely, if you pass the wrong data type somewhere in your code, you are going to encounter a problem which normally appears as a run time error, and in this case, the compiler picks it up easily. But within the same procedeure, it doesn't, and I can think of several situations where this could cause problems.

Links

IDE editors for programming ESP boards.
Micropython editors

Micropython functionality and libraries.
Micropython.org

Find the uPython firmware for your board.
uPython firmware for development boards.

ESP32 uPython modules and function examples.
Quick reference for the ESP32

Finding the MAC address

Scanning and connecting to multiple SSIDs

Using an SD card reader breakout board

ESP32 Real Time Clock

The Deepsleep function

Storing variables in the RTC

RTC Tuning

OTA Updating running software without interuption

Hassle free web communication with urequests.py.

A very basic problem...

Future articles

Communication between ESPs