Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Summary
Great tips from the .NET developer community compiled by Bonnie Berent.
Description

Inheritance and Constructors
Here's a common scenario: say you have a base class that does some work (in the example I'll show, it is a generic class to fill a DataSet from SQL, based on the TableName property of the class). Each sub-class of this base class will provide it's own TableName property, because each sub-class will be filling it's own table.

First attempt at creating these two classes, looks like this:
public class BizObj
{
   protected string TableName="";
   private DataSet oData;

   public BizObj()
   {
      LoadData();  
   }   

   public void LoadData()
   {
      SqlConnection oConn = new SqlConnection("...");
      SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM "+this.TableName,oConn);
      oAdapter.Fill(oData);

   }
}

public class AuthorBizObj: BizObj
{
   public AuthorBizObj()
   {
      this.TableName="Authors";
   }
}
This does not work. Why? Because the BizObj (base class) constructor fires before the AuthorBizObj (sub-class) constructor. And then it's too late to set the TableName at that point.

The trick to fixing this is to use a virtual Property, rather than a member, and to override that Property in each sub-class:
public class BizObj
{
   public string m_TableName="";

   protected virtual string TableName
   {
      get {return this.m_TableName;}
      set {this.m_TableName = value;}
   }

   private DataSet oData;

   public BizObj()
   {
      LoadData();
   }

   public void LoadData()
   {
      SqlConnection oConn = new SqlConnection("...");
      SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM "+this.TableName,oConn);
      oAdapter.Fill(oData);

   }
}

public class AuthorBizObj: BizObj
{
   protected override string TableName
   {
      get {return "Authors;}
   }
}

from a solution provided by Bonnie Berent in Message #1088776

Disabling DataSet Constraints
When working with DataSets that have constraints (such as a UniqueConstraint or a ForeignKeyConstraint), at times it is necessary to disable these constraints temporarily. Set the DataSet.EnforceConstraints to true or false as needed.

I know this seems like such a basic idea that only a Noobie could love, but I've seen the question asked many times, so I thought it warranted mentioning.

from a solution provided by Einar Kvandahl in Message #1179791

SSL
If you're running Windows Server 2003/IIS6 (or later) you can use the SelfSSL tool to generate your own certificate for SSL:

http://www.visualwin.com/SelfSSL/

from a solution provided by Al Doman in Message #1076816

Remove the "X" that Closes the Application
In order to do this sort of thing, we must use the Windows API. Perry points us to a link to an article, with an example written in VB:

http://www.dotnet4all.com/dotnet-code/2004/10/hiding-forms-close-controlbox-button.html

In order to test this, I converted it to C#.
[DllImport("user32")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, int bRevert);
[DllImport("user32")]
public static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32")]
public static extern int DrawMenuBar(IntPtr hWnd);
[DllImport("user32")]
public static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
		
public const int MF_BYPOSITION = 0x400;
public const int MF_REMOVE = 0x1000;

public void DisableTheX(Form oForm)
{
	IntPtr hMenu;
	int n;    
	hMenu = GetSystemMenu(oForm.Handle, 0);    
	if (hMenu.Equals(IntPtr.Zero) == false)
	{
		n = GetMenuItemCount(hMenu);        
		RemoveMenu(hMenu, n - 1, MF_BYPOSITION | MF_REMOVE);
		RemoveMenu(hMenu, n - 2, MF_BYPOSITION | MF_REMOVE);
		RemoveMenu(hMenu, n - 3, MF_BYPOSITION | MF_REMOVE);

		DrawMenuBar(oForm.Handle);
	}
}

from a solution provided by Perry Forman in Message #1099658

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers