Get Reference

  • Thread starter Thread starter Tatyana
  • Start date Start date
T

Tatyana

I have a function
private static TypeA GetA()
{...}

Now I need to pass object that I get from GetA() as a
reference to a base class constructor. How to do it?

I tried doing
public MyClass() :
base(GetA())
{
}
but the compiler complains
"Argument '1': cannot convert from 'TypeA' to 'ref TypeA'
"

Please help.
Thanks,
Tatyana
 
your example is far from complete, but i can give you a suggestion.
try
public MyClass() : base(ref GetA())
{

}

If that won't work, then you are pretty much out of luck, i don't know how
well ref works when it comes to method calls.
 
It doesn't work. I get "A ref or out argument must be an
lvalue" when I try to do it.

I'm pretty sure that there should be some way to do it.
Otherwise I'm not able to construct the object because
base class doesn't have constructor without arguments.

Any other idea?

Thanks!
 
Well, that is a nasty spot(and a not very well designed base class, but
thats another matter)

the only thing i could think of is

public class MyClass : Base
{

public MyClass() : Base(ref internalStaticRef)
{

}

internal static TypeA internalStaticRef = new TypeA();
}

if that works, i'd contact the assembly maker and suggest they reconsider
their design, if you can.
 
I DID IT!
I created one more constructor
private MyClass(TypeA a) :
base(ref a)
{
}

and changed existing constructor to
public MyClass():
this(GetA())
{
}

Daniel, thanks for you help!
Let me know if you find better solution :)

Tatyana
 
actually, i was just going to come back and suggest this same solution, but
you beat me to it, ;)
Still, i'd complain about that lacking design in the assembly if possible,
but your welcome
 
Back
Top