Thursday, May 12, 2005

Tips # 1

Tip 1 :
Use The Call Stack Window To Set Breakpoints
If you are buried way down deep in your application during a debugging session and you want to quickly go back up the call stack, open the Call Stack window, highlight the function you want to return to and hit your breakpoint key (F9 by default). The Visual C debugger allows you to set breakpoints in the Call Stack window.

Tip 2 :
The Undocumented Pointer Array Expansion Format Code
If a pointer to an array only expands to a single item, the undocumented numerical format specifier will force the expansion. For example, if you have a LPDWORD pointer, pVals, that points to an array of 10 items, you can display all ten by entering "pVals,10" in the watch window. The number after the comma tells the watch window how many items to display.

Tip 3 :
Get Rid of the MSDEV Splash Screen When Using Just in Time Debugging
1. Run REGEDIT and open the key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AeDebug.2. Change the Debugger key from "\msdev.exe -p %ld -e %ld" to "\msdev.exe -nologo -p %ld -e %ld".

Tip 4 :
Populate DataSets with XML Files
Want to populate an ADO.NET DataSet with data from an XML file? It's easy; here's how:

DataSet ds = new DataSet ();
ds.ReadXml (new FileStream ("Credentials.xml", FileMode.Open, FileAccess.Read));

This example passes the XML file's file name (Credentials.xml) to FileStream's constructor, and then passes the resultant FileStream to the DataSet's ReadXml method. What's really cool is that the DataSet will infer a schema from the contents of the XML file and structure itself accordingly. You can even modify the data and write it back out with WriteXml.

Tip 5 :
Building Stand-Alone ATL COM Servers
If you use ATL to build COM servers, you should know that there are two steps required to build a truly stand-alone ATL COM server--that is, one that has no DLL dependencies.
Step 1 is to choose Release Min Dependency as your build type.
Step 2 is to make sure that the preprocessor symbol _ATL_STATIC_REGISTRY is defined. The latter statically links your server to the ATL component registrar and allows it to self-register on machines that don't have Atl.dll installed on them. _ATL_STATIC_REGISTRY is defined by default for Release Min Dependency builds in Visual C 6.0, but not in 5.0.

Tip 6 :
Fix ATL COM Classes That Won't Register
Have you ever written a COM server with the Active Template Library (ATL) and discovered that one or more of the COM classes inside it doesn't register when the server self-registers? If so, check the server's object map for a missing OBJECT_ENTRY statement. A bug in Visual C sometimes prevents ATL COM classes added with the ATL Object Wizard from being added to the object map, and if it's not in the object map, then it won't register.

Tip 7 :
Path Name Parsing Made Easy with Regex
Remember how painful it was in the old days to write code that split a path name into its component parts? If you're writing .NET code, splitting path names is easy thanks to a handy little class named Regex that's one of many thousands of classes in the .NET Framework Class Library. Here's an example:

Regex regex = new Regex (@"\\");
string[] parts = regex.Split (@"C:\Inetpub\wwwroot\AshiSystems");
foreach (string part in parts)
Console.WriteLine (part);

Run this code in a console app and it will display the individual substrings that make up the path name.

Tip 7 :
Move ASP.NET Session State to an External Process
One of the many features that makes ASP.NET better than ASP is the way it manages session state. By default, ASP.NET session state is stored in the same process as ASP.NET applications. But a simple configuration change lets you move session state to an external "state server" process that will survive IIS restarts. You can even run the state server on a separate machine, in which case session state will survive even if the machine hosting the ASP.NET application goes down completely. For beta testers running recent (post-Beta 1) builds of ASP.NET, here's how to make the configuration change:
1) Create a text file named Web.config. Add the following text to it and copy the file to the directory where your ASP.NET application resides:



mode="stateserver"
stateConnectionString="tcpip=localhost:42424"
/>



2) Open a command prompt window and type NET START ASPNET_STATE to start the state server. Your application should now run as before, but if it uses session state, that state in now created in an external process named Aspnet_state.exe.
You can prove that session state stored this way is more robust by starting and stopping IIS. Any data you tucked into session state before IIS was restarted will still be there after the restart. For even greater robustness, you can change "localhost" to an IP address to move session state to a remote machine.


Tip 7 :
Send E-Mail From .NET Applications
Want to send an e-mail message from a .NET app? It's easy using the MailMessage and SmtpMail classes found in the System.Web.Mail namespace. Here's an example written in C#:

using System.Web.Mail;
.
.
.
MailMessage message = new MailMessage ();
message.From = "webmaster@AshiSystems.com";
message.To = "luckywinner@somewhere.com";
message.Subject = "You Win!";
message.Body = "You have won the state lottery! To claim your prize, do a handstand on one hand and "
"count backwards from 1,000,000 to 1";
SmtpMail.Send (message);

Note that this code is valid for Beta 2 of the .NET Framework SDK, but not for Beta 1. Also, you must include a reference to the System.Web.dll assembly when you compile.

Tip 8 :
Use StringBuilder to Build Strings Efficiently
It's easy to build strings on the fly using C# and other .NET programming languages. The following code fragment builds a string that counts from 1 to 99:

string s = "";
for (int j=1; j<=99; j++) {
s += j.ToString ();
s += ", ";
}

This code works, but it's slow. Why? Because in .NET, strings are instances of System.String, and System.Strings are immutable. Each time you use the = operator to append text to a string, a new string is created and text is copied from the old string to the new. To build strings on the fly and to do so efficiently, use the .NET Framework Class Library's StringBuilder class, which is found in the System.Text namespace. The following code fragment produces the same string as the code above, but does so in less than half the time:

StringBuilder sb = new StringBuilder ();
for (int j=1; j<=99; j++) {
sb.Append (j.ToString ());
sb.Append (", ");
}
string s = sb.ToString ();

For an additional performance boost, you can override a StringBuilder's default capacity (enough to hold only 16 characters)
and preallocate the memory required to build the new string by passing an integer value specifying the StringBuilder's capacity to StringBuilder's constructor.

0 Comments:

Post a Comment

<< Home