Passing ANY form

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello
I have the following reference in a class:
public SerialPortCommunication(Form1 parentForm,string PortName)

I want to be able to pass ANY form to this reference, not just a
Form1.
I changed it to "Form" and I was told there was an explicet
conversion, but I dont know what it is.
Any help?
Thanks
Mike
 
Hello
I have the following reference in  a class:
public SerialPortCommunication(Form1 parentForm,string PortName)

I want to be able to pass ANY form to this reference, not just a
Form1.
I changed it to "Form"  and I was told there was an explicet
conversion, but I dont know what it is.
Any help?
Thanks
Mike

Actually, I'm trying to pass it to the constructor.
 
I have the following reference in a class:
public SerialPortCommunication(Form1 parentForm,string PortName)

I want to be able to pass ANY form to this reference, not just a
Form1.
I changed it to "Form" and I was told there was an explicet
conversion, but I dont know what it is.

Changing it to Form should work. Post the actual code of both the
constructor and the code that calls the constructor.
 
Hello
I have the following reference in a class:
public SerialPortCommunication(Form1 parentForm,string PortName)

I want to be able to pass ANY form to this reference, not just a
Form1.
I changed it to "Form" and I was told there was an explicet
conversion, but I dont know what it is.
Any help?
Thanks
Mike

What is the line of code that gets the error? Where is it?
 
AMP said:
I have the following reference in a class:
public SerialPortCommunication(Form1 parentForm,string PortName)

I want to be able to pass ANY form to this reference, not just a
Form1.
I changed it to "Form" and I was told there was an explicet
conversion, but I dont know what it is.

There is an implicit upcast going on. As 'Form1' is a subtype of 'Form',
each instance of 'Form1' is a 'Form' too.

\\\
Form f = new Form1(); // Implicit upcast (each 'Form1' is a 'Form').
Form1 f1 = (Form1)f; // Explicit downcast (each 'Form' is not necessarily
a 'Form1').
///

Implicit upcasts always succeed, but explicit downcasts can fail at runtime.

In addition, take a look at the 'as' operator in C#.
 
Back
Top