Get Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tatyana

    #1

    Get Reference

    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
  • Daniel O'Connell

    #2
    Re: Get Reference

    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.
    "Tatyana" <m_tanya@mail.r u> wrote in message
    news:050301c36d cb$be352b10$a60 1280a@phx.gbl.. .[color=blue]
    > 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[/color]


    Comment

    • Tatyana

      #3
      Re: Get Reference

      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!
      [color=blue]
      >-----Original Message-----
      >your example is far from complete, but i can give you a[/color]
      suggestion.[color=blue]
      >try
      >public MyClass() : base(ref GetA())
      >{
      >
      >}
      >
      >If that won't work, then you are pretty much out of[/color]
      luck, i don't know how[color=blue]
      >well ref works when it comes to method calls.
      >"Tatyana" <m_tanya@mail.r u> wrote in message
      >news:050301c36 dcb$be352b10$a6 01280a@phx.gbl. ..[color=green]
      >> 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[/color][/color]
      TypeA'[color=blue][color=green]
      >> "
      >>
      >> Please help.
      >> Thanks,
      >> Tatyana[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Daniel O'Connell

        #4
        Re: Get Reference

        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 internalStaticR ef)
        {

        }

        internal static TypeA internalStaticR ef = new TypeA();
        }

        if that works, i'd contact the assembly maker and suggest they reconsider
        their design, if you can.
        "Tatyana" <m_tanya@mail.r u> wrote in message
        news:06a401c36d d3$1f2632f0$a40 1280a@phx.gbl.. .[color=blue]
        > I can not change the base class.It is defined in external
        > assembly and I don't have control over it.
        >[color=green]
        > >-----Original Message-----
        > >Does the base class HAVE to have a ref argument?
        > >Your only other possibility is a internal static field,[/color]
        > I just did some[color=green]
        > >reading and properties cannot be passed as ref either.
        > >Did you design the base, or are are you stuck wtih it?
        > >"Tatyana" <m_tanya@mail.r u> wrote in message
        > >news:05a701c36 dce$a9a1ce30$a5 01280a@phx.gbl. ..[color=darkred]
        > >> It doesn't work. I get "A ref or out argument must be[/color][/color]
        > an[color=green][color=darkred]
        > >> 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!
        > >>
        > >> >-----Original Message-----
        > >> >your example is far from complete, but i can give you[/color][/color]
        > a[color=green][color=darkred]
        > >> 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.
        > >> >"Tatyana" <m_tanya@mail.r u> wrote in message
        > >> >news:050301c36 dcb$be352b10$a6 01280a@phx.gbl. ..
        > >> >> I have a function
        > >> >> private static TypeA GetA()
        > >> >> {...}
        > >> >>
        > >> >> Now I need to pass object that I get from GetA() as[/color][/color]
        > a[color=green][color=darkred]
        > >> >> 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
        > >> >
        > >> >
        > >> >.
        > >> >[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        • Tatyana

          #5
          Re: Get Reference

          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
          [color=blue]
          >-----Original Message-----
          >I can not change the base class.It is defined in[/color]
          external[color=blue]
          >assembly and I don't have control over it.
          >[color=green]
          >>-----Original Message-----
          >>Does the base class HAVE to have a ref argument?
          >>Your only other possibility is a internal static field,[/color]
          >I just did some[color=green]
          >>reading and properties cannot be passed as ref either.
          >>Did you design the base, or are are you stuck wtih it?
          >>"Tatyana" <m_tanya@mail.r u> wrote in message
          >>news:05a701c3 6dce$a9a1ce30$a 501280a@phx.gbl ...[color=darkred]
          >>> It doesn't work. I get "A ref or out argument must be[/color][/color]
          >an[color=green][color=darkred]
          >>> lvalue" when I try to do it.
          >>>
          >>> I'm pretty sure that there should be some way to do[/color][/color][/color]
          it.[color=blue][color=green][color=darkred]
          >>> Otherwise I'm not able to construct the object because
          >>> base class doesn't have constructor without arguments.
          >>>
          >>> Any other idea?
          >>>
          >>> Thanks!
          >>>
          >>> >-----Original Message-----
          >>> >your example is far from complete, but i can give[/color][/color][/color]
          you[color=blue]
          >a[color=green][color=darkred]
          >>> 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.
          >>> >"Tatyana" <m_tanya@mail.r u> wrote in message
          >>> >news:050301c36 dcb$be352b10$a6 01280a@phx.gbl. ..
          >>> >> I have a function
          >>> >> private static TypeA GetA()
          >>> >> {...}
          >>> >>
          >>> >> Now I need to pass object that I get from GetA()[/color][/color][/color]
          as[color=blue]
          >a[color=green][color=darkred]
          >>> >> reference to a base class constructor. How to do[/color][/color][/color]
          it?[color=blue][color=green][color=darkred]
          >>> >>
          >>> >> I tried doing
          >>> >> public MyClass() :
          >>> >> base(GetA())
          >>> >> {
          >>> >> }
          >>> >> but the compiler complains
          >>> >> "Argument '1': cannot convert from 'TypeA' to 'ref
          >>> TypeA'
          >>> >> "
          >>> >>
          >>> >> Please help.
          >>> >> Thanks,
          >>> >> Tatyana
          >>> >
          >>> >
          >>> >.
          >>> >[/color]
          >>
          >>
          >>.
          >>[/color]
          >.
          >[/color]

          Comment

          • Daniel O'Connell

            #6
            Re: Get Reference

            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
            "Tatyana" <m_tanya@mail.r u> wrote in message
            news:05bb01c36d d6$4c261920$a60 1280a@phx.gbl.. .[color=blue]
            > 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
            >[color=green]
            > >-----Original Message-----
            > >I can not change the base class.It is defined in[/color]
            > external[color=green]
            > >assembly and I don't have control over it.
            > >[color=darkred]
            > >>-----Original Message-----
            > >>Does the base class HAVE to have a ref argument?
            > >>Your only other possibility is a internal static field,[/color]
            > >I just did some[color=darkred]
            > >>reading and properties cannot be passed as ref either.
            > >>Did you design the base, or are are you stuck wtih it?
            > >>"Tatyana" <m_tanya@mail.r u> wrote in message
            > >>news:05a701c3 6dce$a9a1ce30$a 501280a@phx.gbl ...
            > >>> It doesn't work. I get "A ref or out argument must be[/color]
            > >an[color=darkred]
            > >>> lvalue" when I try to do it.
            > >>>
            > >>> I'm pretty sure that there should be some way to do[/color][/color]
            > it.[color=green][color=darkred]
            > >>> Otherwise I'm not able to construct the object because
            > >>> base class doesn't have constructor without arguments.
            > >>>
            > >>> Any other idea?
            > >>>
            > >>> Thanks!
            > >>>
            > >>> >-----Original Message-----
            > >>> >your example is far from complete, but i can give[/color][/color]
            > you[color=green]
            > >a[color=darkred]
            > >>> 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.
            > >>> >"Tatyana" <m_tanya@mail.r u> wrote in message
            > >>> >news:050301c36 dcb$be352b10$a6 01280a@phx.gbl. ..
            > >>> >> I have a function
            > >>> >> private static TypeA GetA()
            > >>> >> {...}
            > >>> >>
            > >>> >> Now I need to pass object that I get from GetA()[/color][/color]
            > as[color=green]
            > >a[color=darkred]
            > >>> >> reference to a base class constructor. How to do[/color][/color]
            > it?[color=green][color=darkred]
            > >>> >>
            > >>> >> I tried doing
            > >>> >> public MyClass() :
            > >>> >> base(GetA())
            > >>> >> {
            > >>> >> }
            > >>> >> but the compiler complains
            > >>> >> "Argument '1': cannot convert from 'TypeA' to 'ref
            > >>> TypeA'
            > >>> >> "
            > >>> >>
            > >>> >> Please help.
            > >>> >> Thanks,
            > >>> >> Tatyana
            > >>> >
            > >>> >
            > >>> >.
            > >>> >
            > >>
            > >>
            > >>.
            > >>[/color]
            > >.
            > >[/color][/color]


            Comment

            Working...