C++ Reference equivalent in C#

A

andrew.ropson

in C++ you could do something like

int a = 0;
int& variableToSet = a; // variableToSet refers to a
variableToSet = 1; // a == 1

is there an equivalent in C#?
 
J

Jeff Johnson

in C++ you could do something like

int a = 0;
int& variableToSet = a; // variableToSet refers to a
variableToSet = 1; // a == 1

is there an equivalent in C#?

Not for a value type like int, no. You could make a class which exposes "a"
as a property, and then assign the "main" variable that points to that class
to other variables and they will all end up pointing to the same thing
(which is why they're known as "reference types").
 
A

andrew.ropson

As Jeff says, no.  Depending on what the actual context is, you could use  
passing-by-reference ("ref" or "out"), anonymous methods, or some  
combination to achieve similar results.  Or it's possible that there's an  
even simpler, more maintainable approach you could use.

Not knowing the context, I'm loathe to suggest a specific approach.  The  
code you posted obviously doesn't really illustrate how you're wanting to 
do this, since if you had code that really looked like that, you'd just  
set "a" directly.  But, an awkward way to accomplish this could look  
something like this:

     int a, b, c;
     Action<int> setter;

     switch (someCondition)
     {
     case ConditionA:
         setter = x => a = x;
         break;
     case ConditionB:
         setter = x => b = x;
         break;
     case ConditionC:
         setter = x => c = x;
         break;
     }

     setter(newValue);

The fact is, if all of this appears in a single method, you're probably  
better off just putting the actual assignments in a conditional where you 
want to actually set the variable.  And if it doesn't all appear in a  
single method, there's a good chance it'd be better to put the assignment 
code in a method that takes an "out" argument, and then use the  
conditional to select from different calls to the method, using the  
appropriate argument according to the conditional.

But, in some very specific scenarios, I suppose the awkward approach might  
actually be useful to you.  :)

Pete

Thanks for the response, I was looking to select members from a class
and set the based on an enum, some thing like

class MyClass
{
enum eType {Tcp, Http};
private IChannel tcpChannel;
private IChanbel httpChannel;

private void GetMemberToSet(eType Proctocol, out varToSet)
{
varToSet = null;
switch(Proctocol)
{
case eType.Tcp:
varToSet = tcpChannel; // can't new at this point as I
don't have enough information
break;
case eType.Http:
varToSet = httpChannel; // can't new at this point as I
don't have enough information

break;
}
}

public void SetupChannel(eType Proctocol)
{
IChannel ChannelToSet = null;
GetMemberToSet(Protocol, out ChannelToSet);

switch(Proctocol)
{
case eType.Tcp:
// ... set tcp information
// ...
ChannelToSet = new TcpChannel(); // with C++ ref or point
this would set tcpChannel, here local is set
break;
case eType.Http:
// ... set http information
// ...
ChannelToSet = new HttpChannel(); // with C++ ref or point
this would set httpChanne, here local is setl
break;
}

}
}
 
J

Jeff Johnson

Thanks for the response, I was looking to select members from a class
and set the based on an enum, some thing like
class MyClass
{
enum eType {Tcp, Http};
private IChannel tcpChannel;
private IChanbel httpChannel;

I would completely rewrite that, getting rid of the two separate IChannel
variables and instead putting them in an IChannel[] array. Then I'd use
constants instead of the enum, sort of like this:

const int TCP_CHANNEL = 0;
const inst HTTP_CHANNEL = 1;
private IChannel[];

You could still use an enum, especially if you want to expose this class to
other code, but then you'll be doing a bunch of (int) casts whenever you
access the array.
 
A

andrew.ropson

Thanks for the response, I was looking to select members from a class
and set the based on an enum, some thing like [...]

In the code you posted, it's not clear to me why the method SetupChannel()  
is intended to not know which member field it's setting.  You're  
duplicating the conditional logic, but for no apparent gain that I can see.

So, one suggestion would be to simply use a different member field  
according to which case of your switch in SetupChannel() you're in.

That said, the other question your code example raises is why do you have 
two member fields at all?  They are both of type IChannel, and in an ideal  
OOP design, any code that uses them wouldn't care _what_ IChannel  
implementation they are.  That's the whole point of having an interface..

By having two different variables, you not only introduce this question of  
which one to assign during initialization, you also will necessarily have 
to have conditionals every time you want to use one or the other to see  
which one to use.  A better OOP design would simply ignore the  
implementation detail and use a single IChannel member field, in the usual  
polymorphic way.

If you really want this kind of pre-selection during initialization, the  
use of a delegate as I showed earlier certainly would work.  But as I  
mentioned, it's an awkward way to go about things, and I think in this  
case there are at least a couple of clearly better approaches.

As an aside, I'm also a bit confused by the relationship between the two  
"protocols", since HTTP and TCP aren't peers in the hierarchy of  
protocols.  But, I'll take as granted that you have a good reason for  
that, since it's not what you asked about.  :)

Pete

Thanks for the advice Gents, I orginally had 2 members because I could
have both http and tcp, or either depending on the callers actions. In
the end I just set them explicitly.
 

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