Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Auric__ submitted this idea :
GS wrote: For clarity... Firstly, my reply wasn`t meant for you! Sorry.., my bad! Secondly, I wasn't stating a hard rule, only my opinion! In hindsight it would have been better had I started out with... "I suggest to always use a variant...because..." However, be it that you are absolutely correct in your example, there are uses that VB[A] will convert for us. As programmers we would know when/where. I was trying to convey to the many non-programmers here that using a variant obviates any and all chances of a type error being raised. Ok, that makes sense. I withdraw my argumentative post. ;-) Perhaps my MsgBox example wasn`t a good choice since it happens that VB does convert that too.<g In the case of InputBox a string is returned. In the case of MsgBox an Integer (contrary to my claim of Long) is returned. Actually, it's kind of interesting. This: MsgBox VarType(MsgBox("test", vbYesNoCancel)) ...tells me that the return type is Long (vartype 3)... but the definition in VBAEN32.OLB is like so: short _stdcall MsgBox( [in] VARIANT* Prompt, [in] VARIANT* Buttons, [in] VARIANT* Title, [in] VARIANT* HelpFile, [in] VARIANT* Context); On Windows, a C "short" is usually 16 bits, a.k.a. Integer. So it looks like Windows is taking that short and putting it into a 32-bit var before returning it (which I guess is typical for Windows), then VB(A) does its usual magic and makes the result fit where it's needed. I suppose it's left over from the 16-bit versions of VB. (In VB4 16-bit, the above vartype line says that MsgBox is indeed an Integer, vartype 2.) This is more or less what I was eluding to when I said VB[A] converts returns to fit the function. I think it's really great that you made the extra effort to better explain some of the nuances most VBA users wouldn't be aware of since (typically) they do not have the programming background we do. I usually make the vartype match the definition of the function, especially with API calls. If the function is declared Long, chances are I'm assigning it to a Long, not a Variant. (Assuming I'm catching the return value at all, of course.) I do the same, usually. The exception would be if the calling procedure expects/needs a variant for further processing the return. It's entirely dependant on the 'nature-of-the-beast', IMO! Well... I can't say I've ever expected a string from a msgbox. ;-) Well.., that wouldn't make sense and so I concur<g! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Copy Worksheets to New Book | Excel Programming | |||
copy worksheets to new book without linking to original book | Excel Discussion (Misc queries) | |||
VB Scrpit to input search results in Excel work book | Excel Programming | |||
Copy Worksheets from one book to another? | Excel Programming | |||
copy from one book to another | Excel Programming |