OOP approach, static method
-
Cutting right into my host of questions, here is a crackdown of my class:
class TheDocument { private TheData\[\] \_list; public TheDocument(TheData\[\] list) { \_list = list; } public static void Print(TheData\[\] list) { TheDocument d = new TheDocument(list); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(d.PrintPage); pd.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { // Printing code... } }
Explaining above code, I came to decide that I need no multiple instances of this document-printing class so I made static Print() method which will initialize the instance and does the trick. So from my application I can just call TheDocument.Print(...); and nothing else. The question is, I couldnt find a way to pass around TheData[] array in other way than either trough constructor or after construction setting it to the instance of an object. This is where all my questions start to roam and I find myself a bit off track; This static method is instance of the calling instance? So within new TheDocument instance I have no access to it without reference? All the printing stuff is merely serving as an example. My real world solution will inherit from more robust printing class and only contain the code for a specific type of document. I know theres plenty of 3rd party solutions for document designing and using them to print, but I will have host of different types of "simple list" kind of documents to print and decided to take this brute approach. Coming from VB and C(embedded) programming all this OOP and fancy C# stuff are so overwhelming ;) Feels like I need proper OOP course or something since I dont even know if there is a term to call class with static method which will make instance of the class itself in it. Or if the whole class is static, is there a name for it instead of saying "static class". Another question about OOP I would ask is if I have alot of variables or properties in one object and define new object in it, what is the best way of accessing those variables or properties. Handing out reference to "parent" object kind of makes that approach less encapsulated?
-
Cutting right into my host of questions, here is a crackdown of my class:
class TheDocument { private TheData\[\] \_list; public TheDocument(TheData\[\] list) { \_list = list; } public static void Print(TheData\[\] list) { TheDocument d = new TheDocument(list); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(d.PrintPage); pd.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { // Printing code... } }
Explaining above code, I came to decide that I need no multiple instances of this document-printing class so I made static Print() method which will initialize the instance and does the trick. So from my application I can just call TheDocument.Print(...); and nothing else. The question is, I couldnt find a way to pass around TheData[] array in other way than either trough constructor or after construction setting it to the instance of an object. This is where all my questions start to roam and I find myself a bit off track; This static method is instance of the calling instance? So within new TheDocument instance I have no access to it without reference? All the printing stuff is merely serving as an example. My real world solution will inherit from more robust printing class and only contain the code for a specific type of document. I know theres plenty of 3rd party solutions for document designing and using them to print, but I will have host of different types of "simple list" kind of documents to print and decided to take this brute approach. Coming from VB and C(embedded) programming all this OOP and fancy C# stuff are so overwhelming ;) Feels like I need proper OOP course or something since I dont even know if there is a term to call class with static method which will make instance of the class itself in it. Or if the whole class is static, is there a name for it instead of saying "static class". Another question about OOP I would ask is if I have alot of variables or properties in one object and define new object in it, what is the best way of accessing those variables or properties. Handing out reference to "parent" object kind of makes that approach less encapsulated?
Your method seems to me that it should not be static, because it wants to use the member data of the class instances. Unless you will have a TheData[] on hand, but no TheDocument class, and just want to print it. The easiest way to pander to both is to write a method like this public void Print() { TheDocument.Print(this._list); }
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Cutting right into my host of questions, here is a crackdown of my class:
class TheDocument { private TheData\[\] \_list; public TheDocument(TheData\[\] list) { \_list = list; } public static void Print(TheData\[\] list) { TheDocument d = new TheDocument(list); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(d.PrintPage); pd.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { // Printing code... } }
Explaining above code, I came to decide that I need no multiple instances of this document-printing class so I made static Print() method which will initialize the instance and does the trick. So from my application I can just call TheDocument.Print(...); and nothing else. The question is, I couldnt find a way to pass around TheData[] array in other way than either trough constructor or after construction setting it to the instance of an object. This is where all my questions start to roam and I find myself a bit off track; This static method is instance of the calling instance? So within new TheDocument instance I have no access to it without reference? All the printing stuff is merely serving as an example. My real world solution will inherit from more robust printing class and only contain the code for a specific type of document. I know theres plenty of 3rd party solutions for document designing and using them to print, but I will have host of different types of "simple list" kind of documents to print and decided to take this brute approach. Coming from VB and C(embedded) programming all this OOP and fancy C# stuff are so overwhelming ;) Feels like I need proper OOP course or something since I dont even know if there is a term to call class with static method which will make instance of the class itself in it. Or if the whole class is static, is there a name for it instead of saying "static class". Another question about OOP I would ask is if I have alot of variables or properties in one object and define new object in it, what is the best way of accessing those variables or properties. Handing out reference to "parent" object kind of makes that approach less encapsulated?
Generally, static methods shouldn't be creating instances of the class they 'live' in. If a static method requires access to instance fields/properties/methods then it should probably not be static as it's working on instance data. A general exception is in situations where an instance wouldn't previously exist but will do at the end of the method. A good example is
int.Parse
. This (in it's simplest form) takes a string as a parameter and it creates an int from it. It would make no sense to have this as an instance method in the int struct as until the string is parsed, there is no int. In your situation, yourPrint
method is obviously related to aTheDocument
instance so I would remove the static and the parameter. If I wished it to be static for convenience, I would rename it toPrintTheDocument
, so it's obvious the method is only for printing TheDocument instances, and take an instance of the class as a parameter,public static void PrintTheDocument(TheDocument theDocument)
{
// ...
}though there's no advantage to be had as you could simply call
theDocument.Print();
instead of
TheDocument.PrintTheDocument(theDocument);
. There is another common occaision when a static will create an instance, and this can be very useful. This is when creating a 'singleton[^]'.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Generally, static methods shouldn't be creating instances of the class they 'live' in. If a static method requires access to instance fields/properties/methods then it should probably not be static as it's working on instance data. A general exception is in situations where an instance wouldn't previously exist but will do at the end of the method. A good example is
int.Parse
. This (in it's simplest form) takes a string as a parameter and it creates an int from it. It would make no sense to have this as an instance method in the int struct as until the string is parsed, there is no int. In your situation, yourPrint
method is obviously related to aTheDocument
instance so I would remove the static and the parameter. If I wished it to be static for convenience, I would rename it toPrintTheDocument
, so it's obvious the method is only for printing TheDocument instances, and take an instance of the class as a parameter,public static void PrintTheDocument(TheDocument theDocument)
{
// ...
}though there's no advantage to be had as you could simply call
theDocument.Print();
instead of
TheDocument.PrintTheDocument(theDocument);
. There is another common occaision when a static will create an instance, and this can be very useful. This is when creating a 'singleton[^]'.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Since I got 2 replies already saying its "weird" to have static method like that, I changed it. Now instead of simply calling TheDocument.Print(...); I first have to create instance oobject of it;
TheDocument doc = new TheDocument(TheList); doc.Print();
And I already feel more confident that this is more "proper". Atleast with this approach I can in the future derive from
IDisposable
interface if I ever need, and this approach more easily yields tousing
. I guess. For the sake of making it more readable to "end user of the class" should I consider using a property for "the list", like.WhateverThisListIs = TheList
? -
Since I got 2 replies already saying its "weird" to have static method like that, I changed it. Now instead of simply calling TheDocument.Print(...); I first have to create instance oobject of it;
TheDocument doc = new TheDocument(TheList); doc.Print();
And I already feel more confident that this is more "proper". Atleast with this approach I can in the future derive from
IDisposable
interface if I ever need, and this approach more easily yields tousing
. I guess. For the sake of making it more readable to "end user of the class" should I consider using a property for "the list", like.WhateverThisListIs = TheList
?It depends - not very helpful I know! If they need array elemement level access then probably yes. Imagine if your document was basically a header, body and footer - all strings. Something like this would be a good way of exposing it.
public class TheDocument
{
private string[] theData = new string[3];public TheDocument() : this(new string\[\] { string.Empty, string.Empty, string.Empty }) { } public TheDocument(string body) : this(new string\[\] { string.Empty, body, string.Empty }) { } public TheDocument(string header, string body) : this(new string\[\] { header, body, string.Empty }) { } public TheDocument(string header, string body, string footer) : this(new string\[\] { header, body, footer }) { } // This constructor is used by all constructors private TheDocument(string\[\] data) { if (data.Length == 3) theData = data; else throw new ArgumentOutOfRangeException("data", "Data must have a length of 3"); } public static implicit operator TheDocument(string\[\] data) { return new TheDocument(data); } public static implicit operator string\[\](TheDocument theDocument) { return theDocument.theData; } public string Header { get { return theData\[0\]; } set { theData\[0\] = value; } } public string Body { get { return theData\[1\]; } set { theData\[1\] = value; } } public string Footer { get { return theData\[2\]; } set { theData\[2\] = value; } } public void Print() { PrintTheDocument(this); } // This method handles all the printing public static void PrintTheDocument(TheDocument theDocument) { // ... }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Cutting right into my host of questions, here is a crackdown of my class:
class TheDocument { private TheData\[\] \_list; public TheDocument(TheData\[\] list) { \_list = list; } public static void Print(TheData\[\] list) { TheDocument d = new TheDocument(list); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(d.PrintPage); pd.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { // Printing code... } }
Explaining above code, I came to decide that I need no multiple instances of this document-printing class so I made static Print() method which will initialize the instance and does the trick. So from my application I can just call TheDocument.Print(...); and nothing else. The question is, I couldnt find a way to pass around TheData[] array in other way than either trough constructor or after construction setting it to the instance of an object. This is where all my questions start to roam and I find myself a bit off track; This static method is instance of the calling instance? So within new TheDocument instance I have no access to it without reference? All the printing stuff is merely serving as an example. My real world solution will inherit from more robust printing class and only contain the code for a specific type of document. I know theres plenty of 3rd party solutions for document designing and using them to print, but I will have host of different types of "simple list" kind of documents to print and decided to take this brute approach. Coming from VB and C(embedded) programming all this OOP and fancy C# stuff are so overwhelming ;) Feels like I need proper OOP course or something since I dont even know if there is a term to call class with static method which will make instance of the class itself in it. Or if the whole class is static, is there a name for it instead of saying "static class". Another question about OOP I would ask is if I have alot of variables or properties in one object and define new object in it, what is the best way of accessing those variables or properties. Handing out reference to "parent" object kind of makes that approach less encapsulated?
Why not a static class?
TheDocument.Print ( data )