0

I have the following classes:-

Common: contains common methods

Tab: represents a single tab in a browser

Window: represents the browser window

Class Common
{
   public goURL()
   {
       //method definition
   }
}

Class Tab 
{
   int tab_id;
   string tab_name;
   Common common;

   public Tab(tab_id, tab_name)
   {
      this.tab_id = tab_id;
      this.tab_name = tab_name; 
      common = new Common();
   }

   public void createNewTab()
   {
      //method definition
      this.common.goURL(); 
   }
}

Class Window
{
   public void startWindow()
   {
     //how do I access goURL() here using current instance of Tab?
   }
   public void newTabButton_Click(object sender, EventArgs e)
   {
      Tab T0 = new Tab(0, "Tab Zero");
      T0.createNewTab();
   }
}

How do I access the goURL() method using the common field inside an instance of Tab class?

One option is to make the method static and access it without creating an object.

EDIT:

This was bad Object-Oriented design from my end. I ended up following the programming principles of High Cohesion and Loose Coupling to re-design my classes and come up with a better design. Thanks to everyone for the advice!

Arjun
  • 817
  • 3
  • 16
  • 28
  • My guess is that `goURL` could be static if that class doesn't have any fields. In fact based on what you're showing `Common` could be static as well. – juharr Oct 18 '17 at 12:24
  • Are you trying to access common.goUrl() from the Window class? – Sam Goldberg Oct 18 '17 at 12:26
  • @juharr The `Common` class does have a field. – Arjun Oct 18 '17 at 12:26
  • @SamGoldberg I am trying to access it like this: `current_instance_of_tab.common.goURL()` – Arjun Oct 18 '17 at 12:27
  • @Arjun: Set `Common` to `public` inside `Tab` then. – waka Oct 18 '17 at 12:28
  • Is the field used in `goURL`? If so it cannot be static and you just need to make it accessible using the appropriate access modifier (public or internal). – juharr Oct 18 '17 at 12:28
  • I have no idea what your startWindow method should be doing? Does it create a new window for browser? Is it initalizing something on existing window? Should it create a new window with a new tab and navigate to some specific url by default? – Janne Matikainen Oct 18 '17 at 12:29
  • @Arjun: OO design principles say that you shouldn't, in general, be calling methods on objects contained within other objects. See [Law of Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter). This principle makes your code more flexible and creates less dependencies between objects. So a better approach would be to add a new `public` method to Tab, which allows client objects to call the `goURL()` method. If you don't follow good design principles, you end up with a system where everything is `static` or `public`, making it a "brittle" system. – Sam Goldberg Oct 18 '17 at 12:34
  • @JanneMatikainen I just abstracted everything for the sake of the question. But, `goURL()` actually fetches the web response. `Common` actually has a field which I instantiate in its constructor. This field is used in the definition of `goURL()` to achieve another task. – Arjun Oct 18 '17 at 12:35
  • Thank you @SamGoldberg. I completely understand what you are saying. The current OO design was bad so I took your advice and re-designed my classes to make it more modular. This allowed me to dissect the problem and come up with a working solution! – Arjun Oct 18 '17 at 15:27

0 Answers0