创建对象
Insus.NET不管以前做网管,还是现在开发写程序,都购买了很多书。因此家中到处会看到书的影子。因为Insus.NET想起要看书时,能随手能拿到书来看。
现就想以书来做一个创建对象的例子。书就是一个对象,它有很多属性,如ISBN,书名,作者等......
因此,下面写成一个Class:
Book
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Book
/// </summary>
namespace Insus.NET
{
public class Book
{
private string _primaryKey;
private string _ISBN;
private string _BookName;
private string _Author;
public string PrimaryKey
{
get { return _primaryKey; }
set { _primaryKey = value; }
}
public string ISBN
{
get { return _ISBN; }
set { _ISBN = value; }
}
public string BookName
{
get { return _BookName; }
set { _BookName = value; }
}
public string Author
{
get { return _Author; }
set { _Author = value; }
}
public Book()
{
//
// TODO: Add constructor logic here
//
}
public Book(string primaryKey, string isbn, string bookName, string author)
{
this.PrimaryKey = primaryKey;
this._ISBN = isbn;
this._BookName = bookName;
this._Author = author;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Book
/// </summary>
namespace Insus.NET
{
public class Book
{
private string _primaryKey;
private string _ISBN;
private string _BookName;
private string _Author;
public string PrimaryKey
{
get { return _primaryKey; }
set { _primaryKey = value; }
}
public string ISBN
{
get { return _ISBN; }
set { _ISBN = value; }
}
public string BookName
{
get { return _BookName; }
set { _BookName = value; }
}
public string Author
{
get { return _Author; }
set { _Author = value; }
}
public Book()
{
//
// TODO: Add constructor logic here
//
}
public Book(string primaryKey, string isbn, string bookName, string author)
{
this.PrimaryKey = primaryKey;
this._ISBN = isbn;
this._BookName = bookName;
this._Author = author;
}
}
}
直到上个星期,妻子说,家里书这样多,不如购买一个书柜回来整理整理。结果在星期天去家具城买了一个大书柜。
书柜就是一个大容器,可以把书放进去,取出来。现在Insus.NET把它当作为一个物件。
Bookcase
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Bookcase
/// </summary>
namespace Insus.NET
{
public class Bookcase
{
private SortedList<string, Book> _SL = new SortedList<string, Book>();
public Bookcase()
{
//
// TODO: Add constructor logic here
//
}
//把书放入书柜
public void Add(Book book)
{
this._SL.Add(book.PrimaryKey, book);
}
//管理书柜的书
public void Edit(Book book)
{
if (this._SL.ContainsKey(book.PrimaryKey))
{
this._SL[book.PrimaryKey] = book;
}
}
//把书从书柜取出来
public void Delete(Book book)
{
this._SL.Remove(book.PrimaryKey);
}
//书柜里所有书
public virtual IList<Book> BookDatas
{
get { return this._SL.Values; }
}
//书柜某一本书
public Book this[string primaryKey]
{
get
{
if (!this._SL.ContainsKey(primaryKey))
return null;
else
return this._SL[primaryKey];
}
set { this._SL[primaryKey] = value; }
}
//书柜所有书的总数
public virtual int Count
{
get { return this._SL.Count; }
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Bookcase
/// </summary>
namespace Insus.NET
{
public class Bookcase
{
private SortedList<string, Book> _SL = new SortedList<string, Book>();
public Bookcase()
{
//
// TODO: Add constructor logic here
//
}
//把书放入书柜
public void Add(Book book)
{
this._SL.Add(book.PrimaryKey, book);
}
//管理书柜的书
public void Edit(Book book)
{
if (this._SL.ContainsKey(book.PrimaryKey))
{
this._SL[book.PrimaryKey] = book;
}
}
//把书从书柜取出来
public void Delete(Book book)
{
this._SL.Remove(book.PrimaryKey);
}
//书柜里所有书
public virtual IList<Book> BookDatas
{
get { return this._SL.Values; }
}
//书柜某一本书
public Book this[string primaryKey]
{
get
{
if (!this._SL.ContainsKey(primaryKey))
return null;
else
return this._SL[primaryKey];
}
set { this._SL[primaryKey] = value; }
}
//书柜所有书的总数
public virtual int Count
{
get { return this._SL.Count; }
}
}
}
开始整理书,并放入书柜中,由于在创建Book对象时,有两个构造函数,因此也有两种实现的方法,方法一:
View Code
//给每本书一个唯一的编号
string pk = Guid.NewGuid().ToString();
Book objBook = new Book();
objBook.PrimaryKey = pk;
objBook.ISBN = this.txtISBN.Text.Trim();
objBook.BookName = this.txtBookName.Text.Trim();
objBook.Author = this.txtAuthor.Text.Trim();
//放入书柜中
objBookcase.Add(objBook);
string pk = Guid.NewGuid().ToString();
Book objBook = new Book();
objBook.PrimaryKey = pk;
objBook.ISBN = this.txtISBN.Text.Trim();
objBook.BookName = this.txtBookName.Text.Trim();
objBook.Author = this.txtAuthor.Text.Trim();
//放入书柜中
objBookcase.Add(objBook);
方法二:
View Code
//给每本书一个唯一的编号
string pk = Guid.NewGuid().ToString();
Book objBook = new Book(pk, this.txtISBN.Text.Trim(), this.txtBookName.Text.Trim(), this.txtAuthor.Text.Trim());
//放入书柜中
objBookcase.Add(objBook);
string pk = Guid.NewGuid().ToString();
Book objBook = new Book(pk, this.txtISBN.Text.Trim(), this.txtBookName.Text.Trim(), this.txtAuthor.Text.Trim());
//放入书柜中
objBookcase.Add(objBook);
实现从书柜把书取出来:
View Code
string pk = GridViewInsusNETBookList.DataKeys[e.RowIndex].Value.ToString();
objBook.PrimaryKey = pk;
objBookcase.Delete(objBook);
objBook.PrimaryKey = pk;
objBookcase.Delete(objBook);
管理书柜的书:
View Code
string pk = GridViewInsusNETBookList.DataKeys[e.RowIndex].Value.ToString();
GridViewRow gvr = GridViewInsusNETBookList.Rows[e.RowIndex];
string isbn = ((TextBox)gvr.FindControl("txtISBN")).Text.Trim();
string bookName = ((TextBox)gvr.FindControl("txtBookName")).Text.Trim();
string author = ((TextBox)gvr.FindControl("txtAuthor")).Text.Trim();
objBook = objBookcase[pk];
if (objBook != null)
{
objBook.PrimaryKey = pk;
objBook.ISBN = isbn;
objBook.BookName = bookName;
objBook.Author = author;
objBookcase.Edit(objBook);
}
GridViewRow gvr = GridViewInsusNETBookList.Rows[e.RowIndex];
string isbn = ((TextBox)gvr.FindControl("txtISBN")).Text.Trim();
string bookName = ((TextBox)gvr.FindControl("txtBookName")).Text.Trim();
string author = ((TextBox)gvr.FindControl("txtAuthor")).Text.Trim();
objBook = objBookcase[pk];
if (objBook != null)
{
objBook.PrimaryKey = pk;
objBook.ISBN = isbn;
objBook.BookName = bookName;
objBook.Author = author;
objBookcase.Edit(objBook);
}
最后,就是需要把书柜的书存入电脑的数据库中:
View Code
protected void Button1_Click(object sender, EventArgs e)
{
//循环GridView 或是 循环objBookcase物件,然后插入至数据库。
}
{
//循环GridView 或是 循环objBookcase物件,然后插入至数据库。
}
运行时的效果:
从上面简单的例子,你也可以创建你的对象,如购物车等,或者是可以实现让用户创建多笔记录之后,再一次性存入数据库中。
下面是演示代码:
http://download.cnblogs.com/insus/ASPDOTNET/Object.rar