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.

<strong>101</strong> TECH TIPSFor <strong>Visual</strong> <strong>Studio</strong> DevelopersVB4/32, VB5, VB6, VBSLevel: IntermediateCreate a Duplicate RecordsetThe Clone method doesn’t quite fit the bill when you want to createa duplicate recordset—it gives you two pointers to the samerecordset, so any updates or deletes you make in one will bereflected in the other. This isn’t always desirable.You can create a true duplicate recordset easily using ADO 2.5(or later) while avoiding a second trip to the server. Use the Streamobject as an intermediary to hold the necessary XML and pass thatXML back into a second Recordset object:Dim rs<strong>One</strong> As New ADODB.RecordsetDim rsTwo As New ADODB.RecordsetDim oTempStream As New ADODB.Stream'Assumes rs<strong>One</strong> has been populated with datars<strong>One</strong>.Save oTempStream, adPersistXMLrsTwo.Open oTempStreamThe catch: The second Recordset will be a client-side cursor.If you want to commit any changes back to your database, youmust establish a new connection, set it on the Recordset, and callUpdateBatch.VB.NETLevel: Beginning—Larry JohnsonInstantiate an Object InlineYou can instantiate a new instance of an object inline, making yourcode more compact. This example shows both versions:Imports SystemPublic Class AuthorPrivate fName As StringPrivate lName As StringPublic Sub New(ByVal fName As String, ByVal lName As _String)me.fName = fNameme.lName = lNameEnd SubPublic ReadOnly Property FullName() As StringGetReturn fName & " " & lNameEnd GetEnd PropertyEnd ClassPublic Class TestPublic Shared Sub Main()'This is the more verbose methodDim author As Author = _New Author("Jon", "Goodyear")Console.WriteLine(author.FullName)'This is the less verbose methodConsole.WriteLine( _New Author("Jon", "Goodyear").FullName)End SubEnd Class—Jonathan Goodyear, Orlando, Fla.VB3, VB4, VB5, VB6, VBA, VBSLevel: BeginningLock Windows 2000 InstantlyLocking an NT workstation has never been easy. Windows 2000has a new function, LockWorkStation, that can lock the machineinstantly with a single API call:Private Declare Function LockWorkStation Lib _"user32.dll" () As LongCall LockWorkStationIn fact, because this function requires no parameters, you canreduce the code to a single line, as well as make it callable from 16-bit code, by invoking it through rundll32:Call Shell("rundll32 user32.dll,LockWorkStation", _vbNormalFocus)The workstation locks instantly when this line is executed. Here’sthe equivalent VBS code:Dim WshSHellSet WshShell = CreateObject("WScript.Shell")WshShell.Run("rundll32 user32.dll,LockWorkStation")VB4, VB5, VB6Level: Beginning—Brian Abernathy, Marietta, Ga.Enable and Disable FramesI’ve had problems enabling and disabling frame controls in <strong>Visual</strong>Basic. If a frame is set to disabled, all the controls within the frameare disabled—but they still look enabled. So I created a simplefunction that loops through all controls, locating those on aspecific frame, and enables or disables them as requested:Private Sub EnableFrameControls( _fra As Frame, ByVal Enabled As Boolean)Dim ctl As ControlOn Error Resume NextFor Each ctl In Me.ControlsIf ctl.Container Is fra Thenctl.Enabled = EnabledEnd IfNext ctlfra.Enabled = EnabledEnd SubVB5, VB6Level: Beginning—Chris O’Connor, Wantirna South, Victoria, AustraliaSkip Object Declaration Using “With” KeywordIf you want to use an object in the middle of a routine and avoiddeclaring the object in the routine, simply use this syntax:With New .End WithThis is the equivalent of:Dim X as New With X.End WithSet X = nothing—Kevin Alons, Salix, Iowa18 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> SEPTEMBER 2001

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

Saved successfully!

Ooh no, something went wrong!