Solved

Casting a ref variable..

Posted on 2003-11-05
2
268 Views
Last Modified: 2010-04-16
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
Comment
Question by:heboh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 50 total points
ID: 9692039
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9702866
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question