By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

C# Object Design

P: n/a
I am new to OOP and C#, and I am just a beginner to learn those stuff.

Hopefully someone can give me some guidance about object design.

Let say, I want to create a class called Book. This class will have a public
method to create new book.

public class Book {
public int New (String ISBN, String BookTitle) {
...
...
}
}

My question is, can we create this method with static modifier? Like:

public class Book {
public static int New (String ISBN, String BookTitle) {
...
...
}
}

So, if we created this method with static modifier, we don't even need to
instantiate an object when we want to create a book, as we can just use the
static method. May I know which way is the correct way when we design a
class with above siatuation? What is the pros and cons?

Thanks
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What you want is a Constructor. There's an excellent article describing
them here:
http://www.yoda.arachsys.com/csharp/constructors.html

"Alan Kordy" <al********@kordina.com.sg> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am new to OOP and C#, and I am just a beginner to learn those stuff.

Hopefully someone can give me some guidance about object design.

Let say, I want to create a class called Book. This class will have a public method to create new book.

public class Book {
public int New (String ISBN, String BookTitle) {
...
...
}
}

My question is, can we create this method with static modifier? Like:

public class Book {
public static int New (String ISBN, String BookTitle) {
...
...
}
}

So, if we created this method with static modifier, we don't even need to
instantiate an object when we want to create a book, as we can just use the static method. May I know which way is the correct way when we design a
class with above siatuation? What is the pros and cons?

Thanks

Nov 15 '05 #2

P: n/a
The pro's to having a static member are that you don't have to create an instance of an object. So, using what you have below - you could run this statement:

Book.New("blah","book title");

The downside is want to keep along those lines, every single class member would need to be static as well. With that, you could really only have the equivalence of one 'instance'. If that's your intent, it would probably be easier to use the "singleton" "design pattern", which let's you create an instance of a class, but always assures there is never more than one. And if you try to create a new one, it will return you the one instance that exists.

The thing you might be missing out on, is it is many times GOOD to have multiple instances of a class. Because each instance is seperate from each other (except for static members). So, from below - you could have a Books (note: plural) class, which could base off of a collection or array and use an indexer.. and it could hold mulitiple Book (note: singular) objects. So in code you could do something like this:

string strTitle = Books["0735614229"].Title; // Books uses the ISBN as the indexer

-or-

Book objNewBook = Books.AddNew("0735614229","Applied Microsoft ..NET Framework Programming");
// Create a new Book object, and return it into a new local variable..

Just food for thought.. hth

"Alan Kordy" <al********@kordina.com.sg> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
I am new to OOP and C#, and I am just a beginner to learn those stuff.

Hopefully someone can give me some guidance about object design.

Let say, I want to create a class called Book. This class will have a public
method to create new book.

public class Book {
public int New (String ISBN, String BookTitle) {
...
...
}
}

My question is, can we create this method with static modifier? Like:

public class Book {
public static int New (String ISBN, String BookTitle) {
...
...
}
}

So, if we created this method with static modifier, we don't even need to
instantiate an object when we want to create a book, as we can just use the
static method. May I know which way is the correct way when we design a
class with above siatuation? What is the pros and cons?

Thanks
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.