Sorry about this week folks

I’m feeling a little under the weather and just dont feel up to writing more posts

Hopefully this will pass soon enough

FWIW IF there is a topic you’d like me to cover drop me a comment or email

Overriding

Sometimes when you create a subclass you want to your subclass to do something different than the class it inherits from. But you want to retain the API or reuse the method name because its perfectly suited.

When you do this and add a method that has the EXACT SAME signature as the one in the superclass you are overriding the method. The signature includes the method name, parameters and their types but excludes the return type.

This can be very useful if you have a class that has many specialized subclasses. For instance, the framework has several subclasses of TCP Sockets; HTTPSockets, SMTPSockets and POP3Sockets. What an HTTP Socket does when you call it Connect method may be different than what the SMTP Socket does when you call its Connect method and also different from what the POP3 Socket does when you call its connect method.

Suppose you have classes like :

Class BaseClass
	Sub Foo()
	  System.debuglog CurrentMethodName
	  
	End Sub
End Class

Class SubClassA
Inherits BaseClass
	Sub Foo()
	  System.debuglog CurrentMethodName
		  
	End Sub
End Class

Class SubclassB
Inherits BaseClass
	Sub Foo()
	  System.debuglog CurrentMethodName
		  
	End Sub
End Class

And code that uses these classes as


Dim c As BaseClass

c = New SubClassA

c.foo

c = New SubclassB

c.foo

What you will see in the debuglog is something like this on macOS.

10:09:20 PM : My Application Launched
              SubClassA.Foo
              SubclassB.Foo
10:09:27 PM : My Application Ended

Whats going on ?

Methods are, unlike properties, virtual. What that means is that the correct method to call will be determined NOT at compile time, but at runtime and based on the actual instance and its method list. This is really handy in lots of ways as I already noted.

You can, as we did in our code, declare the type as the most generic – like we did – since all subclasses are instance of the subclass AND whatever the superclasses that its inherited.

In our code above an instance of SubClassA is both a SubclassA AND a BaseClass.

There are some glitches that you can still run into. For instance this bug report notes one spot where the correct method to call is NOT based on the runtime type but the compile time type (which is really counter intuitive)

That said, overriding is very useful in a class hierarchy.

Have fun with this.

Faking named parameters

Xojo doesnt support named parameters. And I dont expect they are likely to add it any time soon. But, you can kind of fake it for yourself with a bit of work.

Xojo supports both variadic parameter lists, ones that take an unbounded list of parameter, and a declarative syntax for creating pairs as literals.

You can combine this into faking named parameters.

If you define a method as

Sub foo( paramarray namedParams as Pair )

You can call it like

Foo( "param1":1, "param2":"2" )

and the method will get an array of pairs named namedParams that is composed of pairs. Each pair in the list will, for this example, have its left property holding the name and the right property holding he parameter value.

You can iterate over the array and pull out the relevant values into local variables and the method behaves as if you support “named” parameters like :

dim param1 as integer
dim param2 as integer

for each param as pair in namedParams
  select case true
    case param.left = "param1"
      param1 = param.right.integervalue
    case param.left = "param2"
      param2 = param.right.stringvalue
  else   
     // here ypou cane do whatever you want 
     // but I chose to raise an exception
     Raise new UnsupportedOperationException
  end select
next

You could take this further and add an overload that supports positional parameters like

Sub foo( param1 as integer, param2 as string )

and if you do this you probably want to make sure the named parameter version validates that the types of values for the named versions are consistent with the positional version.

And now you can call with named params or positional params and you can decide whether internally the named one calls into the positional param one or vice versa so you dont have to write the same code twice – just put a different API on it.

Thanks !

Flat out I’ll say Thanks to Xojo for the 2.1 decision to revert the event name changes. Of all the changes that one was probably the hardest one to deal with as there’s no easy way to #if around an event definition or event handler.

Properties may still cause some grief if you start a project in 2019r2 or r2.1 since they don’t retain the “old” values” so going back to an old version can be tricky if you start in 2019r2 or 2.1. At least this can be worked around by starting projects in 2019r1.1 or older and its fairly simple to make it so every project behaves like this by using a template that overrides the normal built in one.

WOW !

All I can say is WOW !

The Eagles reuniting was the first time I saw hell freeze over

And now I’ve seen it a second time

You’ll see what I mean

All I can say is OMG THANK YOU !!!!!!!!!

Just wait. You’ll see what I mean in the not too distant future.

Downgrade 2019.R2 to 2019.R1.1

It had to happen sooner or later. But now questions about how to do this are appearing. And the answer is “it depends”.

ALWAYS use a copy or have a backup copy of your entire project JUST in case something goes wrong.

IF you have a license that permits you to save as text (Xojo Project) save your project as a text project. You then need to open the single file called projectname.xojo_project

At the very beginning of this file you will see entries like the following :

Type=Desktop
RBProjectVersion=2019.02
MinIDEVersion=20190200
OrigIDEVersion=20190200

Change the number after MinIDEVersion to 20070100 and save. You can now open the project in whatever version you want.

In an XML project you would see

<?xml version="1.0" encoding="UTF-8"?>
<RBProject version="2019r2" FormatVersion="2" MinIDEVersion="20190200">
<block type="Project" ID="0">

Again change the number following MinIDEVersion to 20070100. Make sure you keep the quotes around the value. And save.

Binary is a lot tougher because it is a proprietary format. And to edit that you WILL need a licensed version of Arbed. Once you have that you can open a binary project and select the top entry which represents the project and change the OriginalVersion to 20070100 and the ProjectSavedInVers to 2007.01 and save.

And now you can open your project in old versions again.

Prepared Statements

Often you’ll hear people say you should use prepared statements to avoid sql injection issues. They’re not wrong. But there can be other reasons to use them.

In some db’s when you create a prepared statement and use it over & over you can avoid a fair amount of a speed hit.

When you initially create a prepared statement many db engines will actually do some work to figure out what the optimal mechanism, or query plan, is to access the data in a table.

And by reusing this over & over you can avoid that computation to determine that optimal query plan.

Not ALL db’s do this, and in some it wont matter if you use a prepared statement because they simply recompute the query plan every time anyways.

But, even if the db engine DOES recompute the query plan over & over you still get all the benefits of avoiding sql injections issues by using them.

I’d say they definitely fall into the category of “best practice”.

A little off the usual

With all the hoopla going on about R2 I have been reading some things that are NOT programming related. Not my usual sort of thing to read.

Stuff like this.

And as I read the article I kept saying to myself Uh huh, Yup and Dead On. And the initial observation he made about

Customers switch because the first company let them down. Customer switch because switching is personal. Customers switch because of how you make them FEEL. Maya Angelou said it best: “People will forget what you said, people will forget what you did, but people will never forget how you made them feel.”

and thinking this is so true.

Much of what I’ve read has expressed how disappointed people are talk of the dismissal of their concerns. This has been said in more than one messaging system I participate in and on blogs I read.

How this is all going to fall out for Xojo I have no idea.

But there are many who have expressed they are done and departing as they just can’t take being ignored any longer especially when it puts their businesses at risk.