top of page

Making your VB6 code scriptable

There is a function/method available in VB6 that very few programmers know about.

But like most techniques, it's usually dismissed because no one tells us "when" to use them and therefore we just go on writing the same old way.

Today I'll share with you this incredible method called "CallByName" and give you example on how to use it and then "when" you might want to use it.

Of course, just because you know something doesn't mean you should use it all the time. The key is knowing "when" to use a technique and this one is something that is good to have in your bag of tricks.

Here's a very basic example of "how" to use CallByName:

Nothing fancy here, the first use of CallByName was to "get" the width and height of the form.

CallByName accepts 3 required parameters with 1 optional.

The 1st is the object --- it could be a Form, a Control, even an instance of a class that you created (example, Person, Employee, etc).

The 2nd is the property or method that you want to use. In the first call, we used both "width" and "height".

The 3rd is related to the 2nd parameter because it specifies what type of "call" you want to perform. You can use vbGet, vbLet, vbSet, vbMethod. Since we're "getting" a value, we used vbGet

On the 2nd call sets the form caption. This is similar to doing "Me.Caption =" but we're here to learn about CallByName so we won't do that.

Note that this call now has the optional 4th parameter --- in this case it is the value we are assigning to the "caption"

Before you read further, I highly encourage you to try the code above so you can really get the hang of it ---- don't worry it's practically just 3 lines of codes :)

Now let's see "when" would we use such a "cool" trick...

If you know CSS, you would have a better appreciation of the example I'd be giving you.

In my example, I'm using this technique to "dynamically" format the textbox and even set its content.

I know we could have just done it the old fashion way but again, if you don't need this on your project then don't use it. Just keep reading below and maybe you could come up with your own cool idea :)

Okay, so essentially I've defined the attributes inside a Dictionary. The "key" is the property of the textbox that I want to decorate and the "value" is the corresponding property value.

Next, I looped through the Dictionary and "dynamically" changed the attributes or properties of the textbox (Text1).

So, "when" do we do this?

Well, instead of hardcoding the attributes and values, you can put them in a database table.

Or, maybe you prefer using INI files. Maybe an XML file to store them.

Then, we can easily make our user interfaces "configurable" without writing additional codes because we can just read the properties and apply them dynamically! Cool, right? :)

If you're new to Dictionary or you got some questions, just post on the comment section below.

Thanks!

bottom of page