Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Baker's Dozen Doubleheader: 26 Productivity Tips for Managing Data (Part 2 of 2) : Page 2

The conclusion of a two-part article on managing data with Visual Studio 2005 and SQL Server focuses on .NET generics, provides an introduction to the new ObjectDataSource class, and demonstrates some additional new capabilities in T-SQL 2005.


advertisement
Tip 2: Using Generics to Store a Custom Class
I can use the List class to store custom classes as well. The following code shows a simple custom class called BaseballClass, with properties for team name and player name.

using System; using System.Collections.Generic; using System.Text; namespace GenericsDemo{ public class BaseballClass { private string name; public string Name { get { return name; }} private string team; public string Team { get { return team; } } public BaseballClass(string name, string team) { this.name = name; this.team = team; } public override string ToString() { return team + ", " + name; } } }

Here's the code for a function (PopulateBaseballClass) that creates an instance of the List class to store instances of the BaseballClass.

List<BaseballClass> PopulateBaseballClass() { List<BaseballClass> oBaseball = new List<BaseballClass>(); oBaseball.Add(new BaseballClass("Kenny Rogers", "Tigers")); oBaseball.Add(new BaseballClass("Michael Young", "Rangers")); oBaseball.Add(new BaseballClass("Ken Griffey, Jr.", "Reds")); oBaseball.Add(new BaseballClass("Tom Glavine", "Mets")); oBaseball.Add(new BaseballClass("David Ortiz", "Red Sox")); oBaseball.Add(new BaseballClass("Derek Jeter", "Yankees")); oBaseball.Add(new BaseballClass("Roger Clemens", "Astros")); oBaseball.Add(new BaseballClass("Roy Oswalt", "Astros")); return oBaseball; }

Finally, this code creates an instance of the Baseball class, calls the PopulateBaseballClass function shown above, and then iterates through the collection.



private void TestBaseballClass() { List<BaseballClass> oBaseball = new List<BaseballClass>(); oBaseball = this.PopulateBaseballClass(); string cResults = ""; foreach (BaseballClass oRecord in oBaseball) cResults += oRecord + "\r\n"; MessageBox.Show(cResults); }

The ObjectDataSource may not handle every situation of a browser-based database application. But learn the capabilities and the limitations of the ObjectDataSource and use it as a starting point.
Tip 3: Sorting with .NET Generics
The generics List class exposes methods for sorting and searching operations. You can even implement your own custom sorting and searching logic. Let's take a look at both.

The code below demonstrates sorting by player name and then by player name within a team. In both instances, I am using a new C# language feature called anonymous methods, to specify the sorting logic in-line.

private void TestSortBaseballClass() { List<BaseballClass> oBaseball = new List<BaseballClass>(); oBaseball = this.PopulateBaseballClass(); string cResults = ""; // Anonymous delegate to sort on name oBaseball.Sort (delegate(BaseballClass f1, BaseballClass f2) { return f1.Name.CompareTo(f2.Name); // Anonymous delegate to sort on Team, then Name oBaseball.Sort (delegate(BaseballClass f1, BaseballClass f2) { return f1.Name.Insert(0, f1.Team).CompareTo(f2.Name.Insert(0, f2.Team)); }

Anonymous methods allow you to create code blocks that can be encapsulated in a delegate, and written in-line where you would otherwise reference a method name. You can reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method. Specifying a code block in the place of a delegate can be useful in a situation when creating a method might seem an unnecessary overhead.

The syntax of anonymous methods can initially appear daunting. However, in this example, anonymous methods save me from the work of adding an additional class.

// In-line anonymous method that compares 2 // incoming values oBaseball.Sort(delegate(BaseballClass f1, BaseballClass f2) { return f1.Name.CompareTo(f2.Name); }); // A second anonymous method, inserts team // in front of name, to sort on two columns oBaseball.Sort(delegate(BaseballClass f1, BaseballClass f2) { return f1.Name.Insert(0, f1.Team).CompareTo( f2.Name.Insert(0, f2.Team)); });



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap