Feed Icon  

Contact

  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics

Tags

Open Source Projects

Archives

Ads

As makes things simpler

Posted in Sql and Xml | General at Friday, July 02, 2004 2:59 AM Pacific Daylight Time

Just read Scott's post about how useful the using statement is. I agree 100%. I remember when I “discovered” the using statement and realized how much simplier it makes things. I'm happy to point out that XmlWriters and XmlReaders will support being used in whidbey. (Also using is coming to VB.Net)

I would also like to point out another one of my favorites: as. as is a very useful keyword which I find makes a lot of code simplier. For example, loading up an object with values from a DataReader.

Without as:

private static MyObject LoadMyObjectFromDataReader(IDataReader dr)
{
  MyObject myObj = new MyObject();
  if (dr["Name"] != DBNull.Value)
  {
    myObj.Name = (string)dr["Name"];
  }
  if (dr["Address"] != DBNull.Value)
  {
    myObj.Address = (string)dr["Address"];
  }
  return myObj;
}

With as:

private static MyObject LoadMyObjectFromDataReader(IDataReader dr)
{
  MyObject myObj = new MyObject();
  myObj.Name = dr["Name"] as string;
  myObj.Address = dr["Address"] as string;
  return myObj;
}

Of course this only works with non-value types so you can't do this with ints. So there you go, as simplifies your code.

 

Comments are closed.