-1

I have a method as shown below. It passes a form as a parameter, Form1 form. This is because I am calling the method from a form called Form1 using the call edit.DropHire(this);

The contents of the method are exactly the same as what I want to use for several other forms, but I want to avoid code repetition. Is there a way I can change the parameter of the method based on what form I am on when I call the method. I.e change it to DropHire(Form2 form) for Form 2, DropHire(Form3 form) for Form 3, DropHire(Form4 form) for Form 4.

Current Method:

public void DropHire(Form1 form)
{
    form.textbox1.Text = "123";
}

Example of what I want to change it to if I call it from another form:

public void DropHire(Form2 form)
{
    form.textbox1.Text = "123";
}
johnny
  • 13
  • 4
  • 1
    You can change yours `Form1`, `Form2` classes? if yes, you can make them implements a Interface and change you `DropHire` to receive a Interface parameter `DropHire(IForm form)`. Or, maybe, you can use a Generic Type in your `DropHire` method, like as `DropHire(T form)`. – Pedro Fernandes Filho Apr 29 '21 at 23:02
  • 2
    Something tells me `//Irrelevant information to the question I'm asking` is not going to be true. – LarsTech Apr 29 '21 at 23:09
  • You are correct. I realised that even though both forms have the same names for textboxes, this throws an error when I try to call something like `form.textbox1.Text = "123"` – johnny Apr 29 '21 at 23:18
  • You have a number of options, depending on what you plan to do with the argument in the constructor. If you need access to common features besides the `Form` class, you need to declare an intermediate base class inheriting `Form` and which your form objects then inherit, and you can use that. Or you might be able to just use `Form`. Or you might want to declare an _interface_ that each of your form's implements. Or you might want to make your method or the class in which it's declared generic. You didn't provide enough information to know which of these options is best, and even if ... – Peter Duniho Apr 29 '21 at 23:29
  • ... you did, it's fundamental object-oriented programming concepts, which means we already have hundreds, if not thousands, of questions with answers on the site that already address your particular concern. See duplicates for a couple of examples. – Peter Duniho Apr 29 '21 at 23:29
  • As an additional note, the code you've shown is really bad. A `Form` subclass should _never_ expose its implementation details, such as a particular `textbox1` object it might own. Based on the example, I'd say an intermediate base class or interface would be the right approach here, but when you do that, you need to fix your public API "surface" to hide particulars and show only the higher-level abstractions relevant to the consumer of each form. See https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp for additional details about fixing that. – Peter Duniho Apr 29 '21 at 23:32

3 Answers3

2

Sure You can, just change your Form1, Form2 to Form, because they are all inherit Form:

public void DropHire(Form form)
{
    //Irrelevant information to the question I'm asking
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

It was already mentioned in the comments, but have you considered implementing and Interface?

If Form, Form1, Form3, etc have similar or some of the same members then you can consider putting the similar members into an Interface and inheriting that Interface on every class or Form.

Then you could just pass each method the Interface. Something like this:

public interface IForm
{
    public Int64 Method1();  //arbitrary methods and properties
    public string Name { get; set; }
    public string Job { get; set; }
}

Then the method that accepts the different Form types could just accept IForm:

public void DropHire(IForm form)
{
    //do stuff relevant
}
    
-1

Use can use the Form class and pattern matching

public void DropHire(Form form)
{
    if(form is Form1 f1)
    {
        f1.textBox1 = "123";
    } else if(form is Form2 f2)
    {
        f2.textBox2 = "123";
    } else
    throw new NotSupportedExpeption();
}

But I recommend setting properties instead of accessing UI elements like text boxes directly.

public class Form1 : Form
{
    ...
    public string Info
    {
       get => textBox1.Text;
       set => textBox1.Text = value;
    }
}

and similarly for Form2

public class Form2 : Form
{
    ...
    public string Extra
    {
       get => textBox1.Text;
       set => textBox1.Text = value;
    }
}

and then use the properties to read/write information:

public void DropHire(Form form)
{
    if(form is Form1 f1)
    {
        f1.Info = "123";
    } else if(form is Form2 f2)
    {
        f2.Extra = "123";
    } else
    throw new NotSupportedExpeption();
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133