Solved

Casting a ref variable..

Posted on 2003-11-05
2
255 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
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now