11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For even more tricks and tips go towww.vbpj.com or www.vcdj.comVB5, VB6Level: Advanced✰✰✰✰✰ Five Star TipEnd IfExit FunctionUse Argument Arrays With CallByNameVB6 introduced a new built-in function, CallByName(), as a memberof VBA.Interaction. It lets you reference an object’s method orproperty by passing its name as an argument. The syntax is:result=CallByName(Object, ProcName, _CallType [,ParamArrayArgs])Unfortunately, this function has several restrictions: It’s accessiblefrom VB6 only; when an error is raised in an ActiveX procedurecalled with the CallByName( ) function from a client, theclient always gets “error 440” regardless of the original errornumber being raised (for details, see Microsoft Knowledge Basearticle Q194418 – PRB: CallByName Fails to Return the Correct ErrorInformation); and the type of the last argument is ParamArray, soyou can’t create a dynamic list of arguments into one statement.For example, the first and second Call statements of this codedon’t work:Dim x(1)x(0) = 1: x(1) = 2'--(1) Error (dynamic list):Call CallByName(Me, "xx", VbMethod, x)'--(2) Error:Call CallByName(Me, "xx", VbMethod, Array(1, 2))'--(3) OK:Call CallByName(Me, "xx", VbMethod, 1, 2)Function xx(x1, x2)'--do something here--End FunctionHowever, you can build your own CallByName function usingTypeLib information (TLBINF32.dll) for VB5/6 applications withoutpointed restrictions:' Required for use in VB5!Public Enum VbCallTypeVbMethod = 1VbGet = 2VbLet = 4VbSet = 8End EnumPublic Function CallByNameEx(Obj As Object, _ProcName As String, CallType As VbCallType, _Optional vArgsArray As Variant)Dim oTLI As ObjectDim ProcID As LongDim numArgs As LongDim i As LongDim v()On Error GoTo HandlerSet oTLI = CreateObject("TLI.TLIApplication")ProcID = oTLI.InvokeID(Obj, ProcName)If IsMissing(vArgsArray) ThenCallByNameEx = oTLI.InvokeHook( _Obj, ProcID, CallType)End IfIf IsArray(vArgsArray) ThennumArgs = UBound(vArgsArray)ReDim v(numArgs)For i = 0 To numArgsv(i) = vArgsArray(numArgs - i)Next iCallByNameEx = oTLI.InvokeHookArray( _Obj, ProcID, CallType, v)Handler:Debug.Print Err.Number, Err.DescriptionEnd FunctionYou must use this syntax to call the CallByNameEx( ) function:Call CallByNameEx(Me, "xx", VbMethod, x)Call CallByNameEx(Me, "xx", VbMethod, Array(1, 2))Result=CallByNameEx(Me, "xx", VbMethod, x)x is an array containing the same number of elements as the calledprocedure has parameters. The CallByNameEx( ) function returnsa real error number from a calling procedure. For VB5, you mustdefine the VbCallType Enum used by the third parameter ofCallByNameEx( ), or use ordinary integers in place of the Enum.VB3, VB4, VB5, VB6Level: Beginning—Vladimir Olifer, Staten Island, N.Y.Select Case EnhancementIn the January 2000 issue of <strong>Visual</strong> Basic Programmer’s Journal, RonSchwarz wrote a nice article on VB Masonry (“VB Masonry:Applying Mortar to the Bricks”). I have found the need to domultiple tests on dissimilar variables and objects with any failingtest causing an action. Multiple embedded If...Then...ElseIf...EndIfstatements are awful to look at and troubleshoot. I found that usingSelect Case does the trick and is easy to read. Consider testingseveral items before continuing (whether to check during entry orafter is another subject). Try this:Private Function okToPost() As Boolean' Assume it's safe to post.okToPost = TrueSelect Case False' Assume you want your tests to be True' Any tests that evaluate to False will' trigger the case code.Case (lvDist.ListItems.Count > 0)' Any items in a listview control?MsgBox "No Items Selected", _vbInformation, "Post"okToPost = FalseCase IsNumeric(fvCheckNumber)' Did the user enter a valid number?MsgBox "Invalid Check Number", _vbInformation, "Post"okToPost = FalsefvCheckNumber.SetFocusCase (fvInvoiceAmount = fvCheckAmount)' Does this balance?' More case statements can follow that' evaluate to true or falseEnd SelectEnd Function—Timothy P. Sullivan, Fort Wayne, Ind.SEPTEMBER 2001 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> 19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!