C
cmo63126
I'm not sure if this is bad design or not. It seems flawed, but I'm not
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.
public class Article
{
int articleID;
string title;
//Constructor: Load Data from DataAccess Class
public Article(int articleID)
{
this.articleID = articleID;
//Set properties of Article Object with values from Database
DataAccess.GetArticleData(this, articleID);
}
public void Update()
{
/*
IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS
(OriginalArticle) WITHIN THE CLASS ITSELF(Article)?
*/
Article OriginalArticle = new Article(this.articleID);
//check to see if the new and origianal values match
if(this.title != OriginalArticle.Title)
{
Response.Write("<br>Title doesn't match current Title in
Database...Perform Important Operation Here.");
}
else
{
Response.Write("<br>Title hasn't changed.");
}
}
public string Title
{
get{return title;}
set{title = value;}
}
}
public class DataAccess
{
public static void GetArticleData(Article article, int ArticleID)
{
//load database data, populate Article object from parameter
article.Title = "Original Title from Database";
//article.Field1 = ...
//article.Field2 = ...
//etc...
}
}
public class _Default : Page
{
private void Page_Load(object sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write("<br>MyArticle.Title: "+MyArticle.Title);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Update();
}
}
results:
MyArticle.Title: Original Title from Database
Title doesn't match current Title in Database...Perform Important
Operation Here.
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.
public class Article
{
int articleID;
string title;
//Constructor: Load Data from DataAccess Class
public Article(int articleID)
{
this.articleID = articleID;
//Set properties of Article Object with values from Database
DataAccess.GetArticleData(this, articleID);
}
public void Update()
{
/*
IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS
(OriginalArticle) WITHIN THE CLASS ITSELF(Article)?
*/
Article OriginalArticle = new Article(this.articleID);
//check to see if the new and origianal values match
if(this.title != OriginalArticle.Title)
{
Response.Write("<br>Title doesn't match current Title in
Database...Perform Important Operation Here.");
}
else
{
Response.Write("<br>Title hasn't changed.");
}
}
public string Title
{
get{return title;}
set{title = value;}
}
}
public class DataAccess
{
public static void GetArticleData(Article article, int ArticleID)
{
//load database data, populate Article object from parameter
article.Title = "Original Title from Database";
//article.Field1 = ...
//article.Field2 = ...
//etc...
}
}
public class _Default : Page
{
private void Page_Load(object sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write("<br>MyArticle.Title: "+MyArticle.Title);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Update();
}
}
results:
MyArticle.Title: Original Title from Database
Title doesn't match current Title in Database...Perform Important
Operation Here.