Happy New Year! I hope you enjoyed some time off with family and friends for the holidays. From time to time, I will post some shorter topics that are more like quick tips than full articles. I figured everyone is just getting back to work this week and probably has a lot going on so this is a good time for a short and simple topic. In this article, I will show a simple way to display array values in MsgBox. This is a very handy debugging tool I sometimes use when I need to check array values as a program is running. And best of all, it only requires one line of code.
Before I show how it’s done, I want to mention that this technique only works when the array is one dimensional and holds value data types such as strings, numbers or booleans (true/false). It will not work if the array references object data types such as drawings, geometry or parameters in CATIA.
How it’s done
The Visual Basic language has some very useful built in functions to work with arrays. One of them is called Join and it is used to take all the values in a one dimensional array and join them together into a single string. The function also allows you to specify an optional delimiter character that will get inserted in between each value. The result can then be easily displayed in a MsgBox.
Be smart when choosing delimiter characters
Most of the time, I use either a comma and space together or vbCrLf (stands for carriage return line feed) as the delimiter character depending on the data I am trying to display. For example, if the array holds the three coordinates of a point, I typically use a comma and a space as the delimiter so the three values are comma separated on a single line. On the other hand, if the array holds the position matrix of a product you might choose vbcrlf as the delimiter because that matrix holds 12 values. This way, each value will be on a separate line and might be easier to read.
Assume the coordinates of a point were previously measured and assigned in a 3 value array called varCoords. Maybe your program is not working right or you just need to check those coordinates. Simply insert this line after the point was measured to see the values.
MsgBox "Coordinates of point: " &Join(varCoords, ", "), 0, "Debug"
Notice that I used a comma and a space as the separator characters to produce an easy to read comma separated string. At runtime, you will see a message displayed something like this,
Assume the position of a product in an assembly was obtained and stored in a 12 value array called varMatrix. In case you have never dealt with this sort of position array, this is what each value represents: 0-2 represents the X direction vector, 3-5 represents the Y direction vector, 6-8 represents the Z direction vector and 9-11 is the origin. To see these values at runtime just insert this line of code:
MsgBox "Product position matrix: " &vbCrLf &vbCrLf &Join(varMatrix, vbCrLf), 0, "Debug"
Note that this time, I used a carriage return (vbCrLf) as the separator character to produce a new line between each value. At runtime, you will see a message displayed something like this,
When to use
To be honest, most of the time I write vb code in the built in CATIA VBA editor. In VBA, there is a very capable set of debugging tools that allow you to set break points and use watch and immediate windows to easily check values. If you haven’t tried the VBA editor you are really missing out – if you get some free time, play around with it a bit. I am working on an upcoming article right now discussing the advantages of using the VBA editor in CATIA, so watch for that one soon.
Although I like to work in VBA, there are some situations where it just isn’t a realistic option. In these cases, using a MsgBox as shown can be a very useful debugging technique. A great example is when you develop KBE models using the knowledgeware workbenches in CATIA. Often there is a need to apply vb code inside various relations which get stored in the specification tree. Unfortunately, when you edit your code inside these features, the code editors that are provided have very minimal functionality especially for debugging. This leaves you with few options besides Msgbox to debug your code.
Also, as a side note, keep in mind that the Join function has many other uses in your programming projects besides what I showed here. There is also a closely related function called Split that converts a string having some delimiter into an array. You can really do some powerful operations using these two functions.
Be aware that the Msgbox function can only display up to 1,024 characters and it will also only display a limited number of lines. This means arrays with many values or many characters may get cut off but the above technique should work fine for most arrays you will probably care to display.
Please take a moment to rate this article…Just click the stars up near the title.