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

Bonnie Berent's Tips
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Summary
Great tips for .NET developers.
Description

Implementing a PEMSTATUS() method
Old FoxPro programmers love their PEMSTATUS function. For those reading this who do not know FoxPro, let me explain what this function does. It returns a true if the existing Property, Event or Method exists in an object and a false if it doesn't. In .NET, we can use reflection to implement this functionality.
public bool PemStatus(object o, string name, string PEM)
{
	switch (PEM.ToUpper())
	{
		case "P" :
			System.Reflection.PropertyInfo pi = o.GetType().GetProperty(name);
			if (pi == null)
				return false;
			else
				return true;
		case "E" :
			System.Reflection.EventInfo ei = o.GetType().GetEvent(name);
			if (ei == null)
				return false;
			else
				return true;
		case "M" :
			System.Reflection.MethodInfo mi = o.GetType().GetMethod(name);
			if (mi == null)
				return false;
			else
				return true;
		default :
			return false;
	}
}
This would then be used as follows:
if (this.PemStatus(oObject, "SetFilter", "M"))
{
	// If this method call requires any parameters, 
	// they need to be passed in as an array of objects.
	object[] parms = new object[1];
	parms[0] = "MyParm";
	MyFilter = (string)oObject.GetType().GetMethod("SetFilter").Invoke(oObject, parms);
}

// or

if (PemStatus(oObject, "SetFocusAtAdd") && _
 (bool)oObject.GetType().GetProperty("SetFocusAtAdd").GetValue(oObject, null))
  oObject.Focus();

I should also mention that this type of functionality can be handled with Interfaces as well:
if (oObject is ISetFocus && ((ISetFocus)oObject).SetFocusAtAdd == true)
  oObject.Focus();

Using Interfaces is more OOP-ish, but I know that for plenty of programmers porting over old VFP code, sometimes just finding a .NET substitute for a function that they already know how to use can be easier.

So, choose your poison. =0)

from a solution provided by Bonnie Berent in Message #1004878 and Message #1005454

Which OleDb Provider is Installed?
Here's a quick and easy method for determing which OleDb Providers are installed:
System.Data.OleDb.OleDbEnumerator o = new System.Data.OleDb.OleDbEnumerator();
System.Data.DataTable dt = o.GetElements();

The GetElements() method returns a table containing a row for every OleDb Provider installed. The Column "SOURCES_NAME" contains the name of the OleDb Provider.

from a solution provided by Einar Kvandahl in Message #1167855

ViewState at Design Time?
At design time, ViewState (and Session state as well) is not available. If you have code that depends on getting it's initial values from ViewState, you should bracket that code with an "if (DesignMode)" so that the ViewState isn't evaluated at design time.

However, the problem is that ASP.NET does not support a DesignMode property on the Page class. Simple workaround for this problem:
bool DesignMode = (HttpContext.Current == null);

For more info, check out two of Rick's blogs:

http://west-wind.com/weblog/posts/189.aspx and http://west-wind.com/weblog/posts/302.aspx

from a solution provided by Rick Strahl in Message #1037709

Prevent Resizing a Windows Form
This is a pretty easy Tip, but sometimes the obvious is easy to overlook. To prevent a Windows Form from resizing, just set the FormBorderStyle property to one of the Fixed settings, either in the Form's Property sheet or in code:
this.FormBorderStyle = FormBorderStyle.FixedSingle;

from a solution provided by Éric Moreau in Message #1130342

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, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
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, 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