Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Casting a ref variable..

Hi,

I wanted to cast a reference to another type..
I have a class Port which implements IPortInput ..

My current variable is : IPortInput anInputPort;

And I have a method as follows: public void Test(ref Port){}

So if I want to call this method by passing a reference of anInputPort (IPortInput) as a type Port instead of type IPortInput which requires casting how can I do that?

Since casting directly by using (Port) ref anInputPort or ref (Port) anInputPort doesnt work..

Can someone give me an example please. Thanks..
0
heboh
Asked:
heboh
1 Solution
 
SRigneyCommented:
Why is your current variable
IPortInput anInputPort?

Don't you want to create a main variable of the specific type and the interface is only used by functions that require interface only functionality?

In any case doing something like this should work.
IPortInput anInputPort = new Port();

Port PortToSend = (Port)anInputPort;
Test(ref PortToSend);


If you want to do it within the function call you probably need another layer of parenthesis.  I didn't test this, so I'm not sure if it will work.

Test(ref ((Port) anInputPort));

Good Luck, let me know how it goes.
0
 
ptmcompCommented:
Why are you passing it as a reference?? There is usually no sense to do this. If you need to change it then return the changed value.

public Port Test(IPortInput anInputPort)
{

}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now