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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

For even more tricks and tips go towww.vbpj.com or www.vcdj.comVB3, VB4, VB5, VB6Level: BeginningVB6Level: Advanced✰✰✰✰✰ Five Star TipManage Errors With Sparse Line NumberingYou might have used line numbering to track error locations, butthis technique can be a pain (and ugly) to use for every line. Sparseline numbers help you locate code sections generating errorsthrough the intrinsic Erl (error line) property.Erl captures the most recent line number so you can pinpointerror locations with whichever precision you desire. This can helpyou determine what to do in the error handler (download this code).For example, do you need to roll back the database transaction?VB3, VB4, VB5, VB6Level: Intermediate—Bob Hiltner, SeattleReplace All Occurrences of <strong>One</strong> StringWith Another StringAll programmers—especially database programmers—require afunction that replaces all occurrences of one substring with anotherstring. For example, they need to replace the single quotesin strings passed to an Oracle database with two single quotes.Using recursion in this algorithm limits its usefulness slightlybelow that of VB6’s native Replace function, as the time requiredincreases greatly in relation to the length of the searched string:Public Function strReplace(ByVal strString As _String, ByVal strToBeReplaced As String, ByVal _strReplacement As String, Optional ByVal _intStartPosition As Integer = 1) As StringDim strNewString As StringDim intPosition As IntegerOn Error GoTo ErrorHandler' intStartPosition will be one initiallyintPosition = InStr(intStartPosition, _strString, strToBeReplaced)If intPosition = 0 Then' Nothing more to do so return final stringstrReplace = strStringElsestrNewString = Left$(strString, intPosition _- 1) & strReplacement & Mid$(strString, _intPosition + Len(strToBeReplaced))' Recursively call strReplace until there are' no more occurrences of the string to be' replaced in the string passed in. We now only want' to process the remaining unprocessed part of the' string so we pass a start position.strReplace = strReplace(strNewString, _strToBeReplaced, strReplacement, _intPosition + Len(strReplacement))End IfExit FunctionErrorHandler:' Place error handler code hereEnd Function—Patrick Tighe, Eastwall, Dublin, IrelandSerialize Data Using a PropertyBagYou can serialize your data quickly by placing it into a PropertyBagobject, then reading the PropertyBag’s Contents property. Thisproperty is really a Byte array that is a serial representation of thedata in your PropertyBag object. You can use this byte array formany purposes, including an efficient means of data transmissionover DCOM:Private Function PackData() As StringDim pbTemp As PropertyBag'Create a new PropertyBag objectSet pbTemp = New PropertyBagWith pbTemp'Add your data to the PB giving each item a'unique string keyCall .WriteProperty("FirstName", "John")Call .WriteProperty("MiddleInitial", "J")Call .WriteProperty("LastName", "Doe")'Place the serialized data into a string'variable.Let PackData = .ContentsEnd WithSet pbTemp = NothingEnd FunctionTo retrieve the serialized data, simply create a new PropertyBagobject and set the serialized string to its Contents property.Convert the string into a byte array before assigning it to theContents property:Private Sub UnPackData(sData As String)Dim pbTemp As PropertyBagDim arData() As Byte'Convert the string representation of the data to'a Byte arrayLet arData() = sData'Create a new PropertyBag objectSet pbTemp = New PropertyBagWith pbTemp'Load the PropertyBag with dataLet .Contents = arData()'Retrieve your data using the unique keyLet m_sFirstName = .ReadProperty("FirstName")Let m_sMiddleInitial = _.ReadProperty("MiddleInitial")Let m_sLastName = .ReadProperty("LastName")End WithSet pbTemp = NothingEnd SubVS.NETLevel: Beginning—Mike Kurtz, McKees Rocks, Pa.Clear a Picture Property at Design TimeTo clear the picture property of a control at design time, right-clickon the icon next to the entry in the Properties window and selectReset from the popup menu.—Bill McCarthy, Barongarook, Victoria, AustraliaSEPTEMBER 2001 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> 29

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

Saved successfully!

Ooh no, something went wrong!