Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OOP approach, static method

OOP approach, static method

Scheduled Pinned Locked Moved C#
questioncsharpcsshardwaredata-structures
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raybarg
    wrote on last edited by
    #1

    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?

    C D P 3 Replies Last reply
    0
    • R Raybarg

      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?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Raybarg

        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?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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, your Print method is obviously related to a TheDocument instance so I would remove the static and the parameter. If I wished it to be static for convenience, I would rename it to PrintTheDocument, 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)

        R 1 Reply Last reply
        0
        • D DaveyM69

          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, your Print method is obviously related to a TheDocument instance so I would remove the static and the parameter. If I wished it to be static for convenience, I would rename it to PrintTheDocument, 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)

          R Offline
          R Offline
          Raybarg
          wrote on last edited by
          #4

          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 to using. 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?

          D 1 Reply Last reply
          0
          • R Raybarg

            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 to using. 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?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0
            • R Raybarg

              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?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Why not a static class? TheDocument.Print ( data )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups