How to print some details on RichText from an external class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wael Slaman
    New Member
    • Nov 2010
    • 8

    #1

    How to print some details on RichText from an external class

    Hi

    Hope someone can help me here. I need your help urgently.

    I have MainForm which contains some buttons , textboxes, and one RichTextBox.

    I have also my own classes (Person , Employee , Engineer , Manager).

    When clicking on Print Button - I need to print all details and the values of the member variables of "Employee Class" into the RichTextBox.

    The problem that 'Employee class' does not know the RichTextBox control or even the MainForm , and that is why I cannot have a reference to it.

    Maybe someone can tell me how to do that with an example??!!

    Thank you
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Why don't you override the Employee class ToString method. Then all you have to do is something like this.

    Code:
    Employee e = new Employee();
    
    //load up the data for the Employee
    
    richTextBox1.Text = e.ToString();

    Comment

    • Wael Slaman
      New Member
      • Nov 2010
      • 8

      #3
      Thank you hype261

      i am afraid you did not understand me.

      Here is my class:


      Code:
      namespace FinalProjectSharp
      {
          class Worker:Person
          {
      
      
              //  Constant value for worker hour rate 
              const int hourRate = 30;
      
              
              //Constructor
              public Worker()
              {
              }
      
              // Instance constructor that has parameters.
              public Worker(string givenName , string familyName , int idNum , int birthYear , int startWorkYear)
              {
                  this.FirstName = givenName;
                  this.LastName = familyName;
                  this.ID = idNum;
                  this.BirthYear = birthYear;
                  this.StartYear = startWorkYear;
              }
      
      
              //Member methods 
      
              //Inserting personal details
              public override void InsertPersonalDetails()
              {
                  //Calls the constructor to allocate and initialize person object
              }
      
      
              //Printing personal details
              public override void [B]PrintPersonalDetails[/B]()
              {
                 
                  
              }
      
      
              //Getting back Salary Proto
              public override int GetSalaryProto(int numWorkHours)
              {
                  //ClaculateSalary()
                  return 0;
              }
      
      
              //Getting back Salary Neto
              public override int GetSalaryNeto()
              {
                  return 0;
              }
        
          }
      }

      I want to implement PrintPersonalDe tails method.

      Worker class do not have any ability to have acess to RichTextBox , which is part of the MainForm controls.
      So I guess your suggestion does not work

      Thank you

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Why do you have to implement that method?

        Hype's suggestion is actually quite appropriate, either directly or indirectly, and accomplishes the same goal. I'm guessing you want your main form to output the details of the person in response to... something. Well, having the Worker class provide an output string is typically more appropriate than having the Worker class output itself directly.

        Consider this: Right now you want to output using a RichTextBox control. Lets say you were hell bent on using the PrintPersonDeta ils method, such that you passed a parameter to that method in order to accomplish this. Inside PrintPersonDeta ils you take the appropriate steps to output it to the RichTextBox.

        Now lets say that sometime in the future, your requirements change. Suppose you no longer want to output these details as text because people can copy/paste from a RichTextBox and now you want to output them to an image. To do this, not only will you have to change your main form but you'll also have to update your Person class as well.

        By contrast, by using the ToString method to provide a string output of your class (or providing a PersonDetails property of type string) you don't need to make any modifications whatsoever to the Person class to accomplish this. All you need to do is make the appropriate call to the property and/or ToString method (or just attempt to use the object as a string).

        It's your project and you're certainly welcome to do it how you like, but hopefully that gave you some insight into the advantages of having your class provide a string that can be used for output instead of directly controlling the output itself.

        Comment

        • Wael Slaman
          New Member
          • Nov 2010
          • 8

          #5
          Thank you
          I agree with you totally , and exposing public property which holds or return my string is good suggestion , however there is still a problem.

          Lets stay with the RTB (Ric Text Box), which is a control in my form. How to return that string to RTB. Does my Form know something about my class Worker??

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Well, at some point you would have called PrintPersonalDe tails from your main form and expected output to happen. It would be at that point that you would instead write the string to the RTB on your form.

            I can't tell you whether or not your main form has a reference to your Worker... you have your code so you'd be able to answer that better than I can ;)

            Comment

            • Wael Slaman
              New Member
              • Nov 2010
              • 8

              #7
              Thank you .
              Got your point. That helped :-)

              Comment

              Working...