?
Solved

Window class and ordinary class, how to work together?.

Posted on 2003-11-09
8
Medium Priority
?
321 Views
Last Modified: 2010-04-16
I have window class which contains mostly private everything that control window form including textbox.

I have ordinary class (orginally using Console.Writeline method), which contains cutdown version of orginal code:-

using System;
namespace CBuffer{
public class CBR{      
public CBR(){}
...
....
public static void CBStart() {
uint loop=0;
while (loop<=100)
{
Console.WriteLine("CBLoop={0} : CBLoop2={1}",loop,(loop*loop))
// ********* How to transfer data into Form1 class so that private method can be used to display data within Textbox1.
loop++;      
} }

Above code is encapsulated and mostly private but will turn into public only for using Window Form1.

Looking into Window Form1

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CBuffer;                                                            
namespace Form1 {
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox TextBox1;
.........

Textbox1 is the RichTextbox, where I like to put data into that form rather than Console.Writeline. This method is private hence restricted to this class.

(1) It is correct to say that CBuffer has no way to use TextBox1.
(2) The code within CBuffer need to use public class within Form1 that access to TextBox1
(3) Would Interface or Polymorphism solve the problem, please demostrate. I have not use this before.
(4) What is the best way to deal the above especially where I have more classes to add in latter.

Thanks
0
Comment
Question by:riscy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 6

Expert Comment

by:purpleblob
ID: 9711116
1. Yes it is correct to say that with the current design CBuffer has no way to access TextBox1. You could obviouly alter the private visibility to public or you could add a property or method to your class to get the TextBox1 or you could make TextBox1 visibility internal but this assumes all the code for CBuffer and Form1 reside in the same assembly (DLL or EXE).

2. Sorry, not sure what you mean here, I think I may have answered this in 1. if not feel free to clarify.

3. You could create an interface and have a property or method public or internal (as described in 1.) but the main use of an interface is in reuse, i.e. interacting with diffierent types of objects through a known interface. With regards polymorphism well you could derive a new class from Form1 and then have a public or internal property/method (as described in 1.) but this seems a little over the top when you could just add the single property.

As an example of the interface use - if you really wanted to leave the Form1 as it is, yes you could create an interface thus:

interface IMyInterface
{
   System.Windows.Forms.RichTextBox TextBox
   {
      get;
   }
}

Obviously you'd need to derive your Form from this interface and implement the method

4. Well the best way is probably through adding a public property, however if you feel that it would be more beneficial to interact with the Form through an interface, i.e. maybe the interface will be reused elsewhere or you wish to abstract the Form code from the classes that use it then the interface is a good route.

Hope this helps
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9711303
Oh sorry, there's me talking about visibility, you should probably prefix the interface example above with the keyword public :-)
0
 

Author Comment

by:riscy
ID: 9712945
Thank purpleblob,

Could you demostrate how the interface be used based on above program used, I still struggle to understand how to implement interface, once demostrated how it used, it might become make sense to me.

In CBR.cs, can interface call a method located in Form1.cs, where at CBR.cs able to transfer data into Form1.cs which has role of displaying data (combination of ref and value, if interface is allows to modify value via ref (more like pointer)).

I was wondering what happen to polymorphism?. Do they have role in this task.

Thank again

Riscy.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:brabchev
ID: 9713427
I don’t see a point of using interface here.
All you have to do is create a public property for class Form1 that returns the RichTextBox and use that one in the CBStart method.

Another thing is that the Form1 should be in the same assembly with CBuffer or Form1 should be compiled as DLL so you can reference it in CBuffer. Also you need to reference System.Windows.Forms.dll in CBuffer.

using System;
namespace CBuffer
{
      using Form1;
      class CBR
      {
            private static Form1 form;

            public CBR(){}
            
            public static void CBStart()
            {
                  uint loop=0;
                  if (form == null)
                        form = new Form1();
                  while (loop<=100)
                  {
                        form.TextBox.Text += "CBLoop="+loop+" : CBLoop2="+(loop*loop)+"\n";
                        loop++;    
                  }
                  form.ShowDialog();
            }

            [STAThread]
            static void Main(string[] args)
            {
                  CBStart();
            }
      }
}
------------------------------------------------------------------------------------------------------------------------------------

namespace Form1
{
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.RichTextBox TextBox1;

            public Form1(){}

            public System.Windows.Forms.RichTextBox TextBox
            {
                  get {return TextBox1;}
            }
      }
}
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9713582
Okay an example for using an interface in the scenario you've suggested might be along the lines of...

public interface ITextBoxAccessor
{
   System.Windows.Forms.RichTextBox TextBox
   {
      get;
   }
}

now as you may know, this basically declares a signature or template (if you like) that your code will understand. So with the Form1 object you'd have the following:

public class Form1 : System.Windows.Forms.Form, ITextBoxAccessor
{
   // ... all your other code

   // implement the ITextBoxAccessor interface
   System.Windows.Forms.RichTextBox TextBox
   {
      get { return TextBox1; }
   }
}

Now from any code that wants to interact with the interface - let's say you have access to an instance of Form1 then you'd do the following (assuminging form is a variable to an instance of Form1 or implements the ITextBoxAccessor interface)

ITextBoxAccessor iface = (ITextBoxAccessor)form;
iface.TextBox.Text = "New Text";

Now in truth you may wish to check whether the form variable actually supports the interface by using the is keyword (a good idea to ensure it doesn't exception if you haven't supported the interface in more generic situations)

i.e.

if(form is ITextBoxAccessor)
{
   ITextBoxAccessor iface = (ITextBoxAccessor)form;
   iface.TextBox.Text = "New Text";
}

Also I've just re-read my earlier description of using polymorphism and it wasn't at all clear (in fact it was garbage :-) ). What we're doing with deriving Form1 from this new interface is polymorphic, just as is creating a new derived class from Form1 as a base class. So what I should have said to you is that we can use polymorphism in a couple of ways, 1. implement a known interface and 2. create a new derived class (as discussed previously) - sorry if I've confused things at all. Obviously the better of the two routes in terms of keeping code generic is to use the interface approach but really it all depends upon you requirements.

Polymorphism is basically the idea that you can interact with an object on different levels (or indeed using different interfaces). Thus we can talk to our form in terms of our new interface (i.e. make it appear to be an object of type ITextBoxAccessor) or we can talk to it as a Form1 or as any of the base classes/interfaces by simply casting to that type (as hown above).

Anyway, back to your question, basically we can declare an interface as having methods (and in C#'s case properties) now all we're doing is saying anything which implements an interface must in fact create/implement all methods of that interface. In our case we have a single property we need to implement.

"Can CBR.cs call a method in Form1.cs" - yes if pass the CBuffer an instance of the Form1 then you can use the code I've shown to get at it's interface but what you can do is - instead of passing a Form1 object which then ties your CBuffer to handling only objects of type Form1 you could pass it a System.Windows.Forms.Form or an even lower base class - so to complete the picture you might have the following within CBuffer

public void ChangeText(System.Windows.Forms.Form form)
{
   if(form is ITextBoxAccessor)
   {
      ITextBoxAccessor iface = (ITextBoxAccessor)form;
      iface.TextBox.Text = "New Text";
   }
}

Sorry it's a long answer but there's quite a few things to describe if you're not to up on the idea of interfaces etc. hope it helps
0
 

Author Comment

by:riscy
ID: 9713737
Dear  purpleblob

Thank, i will do some experiment on this and let you know how this progressed in next few days.

Best Wishes

Riscy

Dear brabchev

I have actually used the loop in Form1 that use public method on CBR.cs to performs calculation and then return value (via ref) back to Form1.cs which then display data, however in this case I trying to learn to understand interface and ploymorphism (sound like The Matrix movies, people changing at thier will (!)). I need to try something to click that and that in my mind.

Thank for kind contribution.

Best Wishes

Riscy.
 
0
 
LVL 1

Expert Comment

by:brabchev
ID: 9713753
Another approach is CBR to inherit Form1 class, but then you need to create an instance of CBR and you can’t use static method.

using System;
namespace CBuffer
{
      using Form1;
      class CBR : Form1
      {
            public CBR(){}
            
            public void CBStart()
            {
                  uint loop=0;
                  
                  while (loop<=100)
                  {
                        base.TextBox.Text += "CBLoop="+loop+" : CBLoop2="+(loop*loop)+"\n";
                        loop++;    
                  }
                  base.ShowDialog();
            }

            [STAThread]
            static void Main(string[] args)
            {
                  CBR cbr = new CBR();
                  cbr.CBStart();
            }
      }
}
0
 
LVL 6

Accepted Solution

by:
purpleblob earned 750 total points
ID: 9713934
Good luck with it
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question