Passing ANY form

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
 
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

Actually, I'm trying to pass it to the constructor.
 
J

Jeff Johnson

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.
 
J

Jack Jackson

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?
 
H

Herfried K. Wagner [MVP]

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#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top