Visio Guy

Visio Discussions => Programming & Code => Topic started by: perry59 on March 01, 2015, 05:50:55 AM

Title: when to use resultstr
Post by: perry59 on March 01, 2015, 05:50:55 AM
I've been working on a macro which, in part, gets info from various cells in the shapesheet.
for the last hour I've been going nuts because a simple line of code was not working. I have a string variable called designator that I want to fill with the value from a cell as such:

Designator = visShape.Cells("Prop.GADesignator")

but this always returned "0", even though there was a string in the cell. Then for the heck of it I tried this:

Designator = visShape.Cells("Prop.GADesignator").ResultStr(Visio.VisUnitCodes.visNoCast)

and got the value I was expecting! the first example, without the resultstr works fine on pretty much all the other cells I'm getting data from, just not on the "designator" cell.
can anyone tell me why this is behaving this way?
should I just make it a habit to always use the resultstr version?
thanks!
Title: Re: when to use resultstr
Post by: Paul Herber on March 01, 2015, 08:53:38 AM
It's a bit of badly implemented object handling in the internals of VBA, the item
visShape.Cells("Prop.GADesignator")
is really only a reference to a cell, it shouldn't return the cell contents.
As you've noticed, to get the cell contents us
visShape.Cells("Prop.GADesignator").Result
visShape.Cells("Prop.GADesignator").ResultStr
or
visShape.Cells("Prop.GADesignator").ResulIU
and maybe others I've forgotten about.
Title: Re: when to use resultstr
Post by: perry59 on March 01, 2015, 03:45:37 PM
However, the first version (without the .resultstr) does work in many cases. It is often shown in Microsoft's own examples!
So then is it safe to say that one of the .resultstr versions should typically be used?
Title: Re: when to use resultstr
Post by: wapperdude on March 01, 2015, 04:29:55 PM
Interesting.

Fundamentally, to get a result, it is my understanding you always need the appropriate ".result".

Can you post one of the links that you refer to?

Wapperdude
Title: Re: when to use resultstr
Post by: AndyW on March 02, 2015, 08:07:35 AM
visShape.Cells("Prop.GADesignator")

should be the same as

visShape.Cells("Prop.GADesignator").ResultIU

as ResultIU is the default method of the Cell class.
Title: Re: when to use resultstr
Post by: wapperdude on March 02, 2015, 04:17:19 PM
@AndyW:  Default method.  ???   Had no idea! 
Title: Re: when to use resultstr
Post by: AndyW on March 02, 2015, 04:29:09 PM
It is a VB thing, you can mark a method as the default via the Tools, Procedure attributes.

You can also do this in VBA but requires export of class, edit with text editor to add the following line to the method that is to be the default.

Attribute Value.VB_UserMemId = 0

And then re-import the class.
Title: Re: when to use resultstr
Post by: wapperdude on March 02, 2015, 05:00:12 PM
That's more about VB than I ever need to know!   ::)   :D

To go along with the "default" point, here's this link for those whom might be as uninformed as myself:  https://msdn.microsoft.com/en-us/library/ms368327(v=office.12).aspx (https://msdn.microsoft.com/en-us/library/ms368327(v=office.12).aspx)

While I appreciate the convenience of "defaults", and, perhaps it has merit for full-time programming on a large scale, in general, I think "un-declared" defaults are "bad form"...just personal opinion.  Defaults would just seem to add to confusion of understanding the code, much like lack of comments.  But, I don't do it for a living, some might look at my code and say I don't do it all  :(, so, no offense meant to full time "coders".

Wapperdude
Title: Re: when to use resultstr
Post by: Yacine on March 02, 2015, 06:02:53 PM
That's why a control "myText" can be assigned directly a value, whilst the strict way would be to assign it to "myText.value".
And that's also why real programmers turn up their nose at VB.
Title: Re: when to use resultstr
Post by: perry59 on March 02, 2015, 07:44:02 PM
then I guess I should always use the .resultstr if I am looking to get a string!
Title: Re: when to use resultstr
Post by: wapperdude on March 02, 2015, 08:25:05 PM
@Perry:  Yes.  Good habit is to use explicit call, that way, you and any code viewer, know exactly what you're getting now and into the future (when memories fade and the question comes to mind...What did I do?  What was I thinking.)

@Yacine:  Ouch!   :o   :D  Well, it is possible to carry over good programming habits, even if VB doesn't require them.  Plus, the macro recorder gives you VBA, so, it does kinda push you into the VBA realm.   ::)
Title: Re: when to use resultstr
Post by: Yacine on March 02, 2015, 08:50:12 PM
As long as it works ... isn't it?
On the other hand, the more you code, the more you discipline yourself. You learn that debugging is so much more time intensive than the actual coding, that you try to do it right the first time. (my codes now alway begin with option explicit)
;)
Title: Re: when to use resultstr
Post by: AndyW on March 03, 2015, 10:07:33 AM
I wasn't implying that it was a good feature, just that it is there and how it works.

I used to turn my nose up a VB when using real programming languages, well in fact anything Microsoft when I was also using real operating systems. Unfortunately I have spent the last 10 years using VB/VBA. Although Visio turns out to be great (which is why the 10 years VB), but then that wasn't an MS product to begin with!!!
Title: Re: when to use resultstr
Post by: Yacine on March 03, 2015, 03:54:48 PM
poor M$. ;)
Guess it is not that the products are so bad, it's rather their dominant position that makes them intrinsically bad.