首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • [2008/07/01]code [已结贴,结贴人:only_endure]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 09:58:35 楼主
    Save TreeView Nodes Expansion
    C# code
    using System; using System.Collections.Generic; using System.Web; using System.Web.UI.WebControls; public class TreeViewState { public void SaveTreeView(TreeView treeView, string key) { List<bool?> list = new List<bool?>(); SaveTreeViewExpandedState(treeView.Nodes, list); HttpContext.Current.Session[key + treeView.ID] = list; } private int RestoreTreeViewIndex; public void RestoreTreeView(TreeView treeView, string key) { RestoreTreeViewIndex = 0; RestoreTreeViewExpandedState(treeView.Nodes, (List<bool?>)HttpContext.Current.Session[key + treeView.ID] ?? new List<bool?>()); } private void SaveTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list) { foreach (TreeNode node in nodes) { list.Add(node.Expanded); if (node.ChildNodes.Count > 0) { SaveTreeViewExpandedState(node.ChildNodes, list); } } } private void RestoreTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list) { foreach (TreeNode node in nodes) { if (RestoreTreeViewIndex >= list.Count) break; node.Expanded = list[RestoreTreeViewIndex++]; if (node.ChildNodes.Count > 0) { RestoreTreeViewExpandedState(node.ChildNodes, list); } } } }
    0  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 10:07:041楼 得分:0
    1)Generics in .Net 2.0 made simple
    SavingsBank.cs
    C# code
    using System; using System.Collections.Generic; using System.Text; namespace GenericImplementation { class SavingsBank { protected string name; protected int age; private const float MIN_AMT_LMT = 10000; private float accBalance; public SavingsBank(string AccName, int age, string opType, float balanceAmt) { this.Name = AccName; this.Age = age; if (opType.ToLower().Equals("credit")) { this.DoCredit(balanceAmt); } else { this.DoDebit(balanceAmt); } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public float TotalBalance { get { return accBalance; } set { accBalance = value; } } public void DoCredit(float credBal) { TotalBalance = TotalBalance + credBal; } public void DoDebit(float debBal) { if (TotalBalance - debBal > MIN_AMT_LMT) { TotalBalance = TotalBalance - debBal; } else { throw new Exception("Balance should not be less than minimum amount " + MIN_AMT_LMT.ToString()); } } public override string ToString() { return " Account name :" + this.Name + " Age:" + this.age.ToString() + " Balance : " + this.TotalBalance.ToString(); } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 10:08:232楼 得分:0
    2)Generics in .Net 2.0 made simple
    Current Bank A/C:
    C# code
    using System; using System.Collections.Generic; using System.Text; namespace GenericImplementation { class CurrentBank { protected string name; protected int age; private float accBalance; private const float MIN_AMT_LMT = 20000; public CurrentBank(string AccName, int age,string opType,float balanceAmt) { this.Name = AccName; this.Age = age; if (opType.ToLower().Equals("credit")) { this.DoCredit(balanceAmt); } else { this.DoDebit(balanceAmt); } } public string Name { get { return name; } set { name = value; } } public float GetCreditLimit { get { return MIN_AMT_LMT; } } public int Age { get { return age; } set { age = value; } } public float TotalBalance { get { return accBalance; } set { accBalance = value; } } public void DoCredit(float credBal) { TotalBalance = TotalBalance + credBal; } public void DoDebit(float debBal) { if (TotalBalance - debBal > MIN_AMT_LMT) { TotalBalance = TotalBalance - debBal; } else { throw new Exception("Balance should not be less than minimum amount " + MIN_AMT_LMT.ToString()); } } public override string ToString() { return " Account name :" +this.Name + " Age:" +this.age.ToString() + " Balance : " + this.TotalBalance.ToString(); } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 10:09:223楼 得分:0
    3)Generics in .Net 2.0 made simple
    Now create a Generic bank account collection which can hold the collection of either Savings Bank account or Current bank account.

    Generic Bank Collection:
    C# code
    using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace GenericImplementation { class GenericBankCollection<AccountType> : CollectionBase { public void Add(AccountType GenericObject) { InnerList.Add(GenericObject); } public void Remove(int index) { InnerList.RemoveAt(index); } public AccountType Item(int index) { return (AccountType)InnerList[index]; } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 10:10:334楼 得分:0
    Usage:
    Now we can use the Generic bank collection to hold either the savingsBankAccount or CurrentBankAccount collection.
    program.cs:

    C# code
    using System; using System.Collections.Generic; using System.Text; namespace GenericImplementation { class Program { static void Main(string[] args) { GenericBankCollection<SavingsBank> sbAccs = new GenericBankCollection<SavingsBank>(); sbAccs.Add(new SavingsBank("Sriram",34,"credit",1000)); sbAccs.Add(new SavingsBank("Saik",30,"debit",1000)); GenericBankCollection<CurrentBank> curAccs = new GenericBankCollection<CurrentBank>(); curAccs.Add(new CurrentBank("Mohan", 34, "credit", 1000)); curAccs.Add(new CurrentBank("Krishna", 30, "credit", 1000)); System.Console.WriteLine("Savings Accounts"); System.Console.WriteLine("========="); foreach (SavingsBank savingsBank in sbAccs) { System.Console.WriteLine(savingsBank.ToString()); } System.Console.WriteLine("Current Accounts"); System.Console.WriteLine("========="); foreach (CurrentBank CurrentBank in curAccs) { System.Console.WriteLine(CurrentBank.ToString()); } System.Console.ReadLine(); } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 16:35:415楼 得分:0
    Using IComparable and IComparer to compare objects
    IComparable
    C# code
    public class Person : IComparable { public Person() { } // private fields private string _FirstName; private string _LastName; private int _Age; public Person(string FirstName, string LastName, int Age) { this.FirstName = FirstName; this.LastName = LastName; this.Age = Age; } //Properties public string FirstName { get { return _FirstName; } set { _FirstName = value; } } public string LastName { get { return _LastName; } set { _LastName = value; } } public int Age { get { return _Age; } set { _Age = value; } } //Interface method public int CompareTo(object o) { Person Temp = (Person)o; if (this.Age < Temp.Age) { return 1; } if (this.Age > Temp.Age) { return -1; } else { return 0; } } }

    C# code
    public class Room { protected ArrayList oList; public Room() { oList = new ArrayList(); } //Indexer public Person this[int index] { get { return (Person)oList[index]; } set { oList[index] = value; } } //Properties public int Count { get { return oList.Count; } } //Methods public void AddNewPerson(Person o) { oList.Add(o); } public void SortByAge() { oList.Sort(); } public void ReverseByAge() { oList.Reverse(); } }

    C# code
    class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); // TODO: Implement Functionality Here Person me = new Person("Bejaoui","Bechir",29); Person myFather = new Person("Bejaoui","Massinissa",65); Person myGrandFather = new Person("Bejaoui","Shishaq",95); //Add some persons to the room Room LivingRoom = new Room(); LivingRoom.AddNewPerson(me); LivingRoom.AddNewPerson(myFather); LivingRoom.AddNewPerson(myGrandFather); LivingRoom.SortByAge(); for(int i = 0; i<LivingRoom.Count;i++) { Console.WriteLine( "First name : " + LivingRoom[i].FirstName + Environment.NewLine ); Console.WriteLine( "Last name : " + LivingRoom[i].LastName + Environment.NewLine ); Console.WriteLine( "Age : " + LivingRoom[i].Age + Environment.NewLine ); } Console.WriteLine("*** After reversing according to age ***" + Environment.NewLine); LivingRoom.ReverseByAge(); for(int i = 0; i<LivingRoom.Count;i++) { Console.WriteLine( "First name : " + LivingRoom[i].FirstName + Environment.NewLine ); Console.WriteLine( "Last name : " + LivingRoom[i].LastName + Environment.NewLine ); Console.WriteLine( "Age : " + LivingRoom[i].Age + Environment.NewLine ); } Console.WriteLine("Done"); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); Console.Beep(); } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • only_endure
    • 等级:
    发表于:2008-07-01 16:39:206楼 得分:0
    [续]Icomparer
    C# code
    /*Given this design of person type: */ public class Person { public Person() { } // private fields private string _FirstName; private string _LastName; private int _Age; public Person(string FirstName, string LastName, int Age) { this.FirstName = FirstName; this.LastName = LastName; this.Age = Age; } //Properties public string FirstName { get { return _FirstName; } set { _FirstName = value; } } public string LastName { get { return _LastName; } set { _LastName = value; } } public int Age { get { return _Age; } set { _Age = value; } } }

    C# code
    Say that we want to sort objects person by name now. First we ought to define a kind for comparer object that implements the IComparer interface: /// <summary> /// CompareByName is an object that enables us compare /// two person objects, it implements the IComprer interface. /// </summary> public class CompareByName : IComparer { public CompareByName() { } //Implementing the Compare method public int Compare(object object1, object object2) { Person Temp1 = (Person)object1; Person Temp2 = (Person)object2; return string.Compare(Temp1.FirstName, Temp2.FirstName); } }

    C# code
    Then we implement the Room container as follow: public class Room { protected ArrayList oList; public Room() { oList = new ArrayList(); } //Indexer public Person this[int index] { get { return (Person)oList[index]; } set { oList[index] = value; } } //Properties public int Count { get { return oList.Count; } } //Methods public void AddNewPerson(Person o) { oList.Add(o); } public void SortByAge() { CompareByName Comparer = new CompareByName(); oList.Sort(Comparer); } public void ReverseByAge() { oList.Reverse(); } }


    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved