Ref problem

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

    #1

    Ref problem

    I need to write a value over a field of a structure.
    .....
    public struct ARSubMesh {
    private Matrix matrix;
    public void setMatrix ( Matrix mat ) {
    this.matrix = mat;
    }
    }
    .....
    ARSubMesh mesh = getMesh("torus" );
    mesh.setMatrix ( mat );
    .....

    public ARSubMesh getMesh(string name) {
    // find the mesh *name*
    return mesh_found;
    }

    This don't work because getMesh create a *new* object.

    I need some like "return ref mesh_found", but the compiler say "Hey, learn a
    bit of c# or ask to the experts..."
    Well, here i am, askin to the experts.

    How can i write directly over the struct field matrix from a returned value?


  • Bruno Jouhier [MVP]

    #2
    Re: Ref problem

    If you want "reference" semantics, use a class not a struct. Just replace
    "struct" by "class" and your example will work.

    Bruno.

    "~toki" <pedorro77.hotm ail.com> a écrit dans le message de
    news:%23pQBRU$r DHA.2304@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I need to write a value over a field of a structure.
    > ....
    > public struct ARSubMesh {
    > private Matrix matrix;
    > public void setMatrix ( Matrix mat ) {
    > this.matrix = mat;
    > }
    > }
    > ....
    > ARSubMesh mesh = getMesh("torus" );
    > mesh.setMatrix ( mat );
    > ....
    >
    > public ARSubMesh getMesh(string name) {
    > // find the mesh *name*
    > return mesh_found;
    > }
    >
    > This don't work because getMesh create a *new* object.
    >
    > I need some like "return ref mesh_found", but the compiler say "Hey, learn[/color]
    a[color=blue]
    > bit of c# or ask to the experts..."
    > Well, here i am, askin to the experts.
    >
    > How can i write directly over the struct field matrix from a returned[/color]
    value?[color=blue]
    >
    >[/color]


    Comment

    • ~toki

      #3
      Re: Ref problem

      :) It works. Thanks!!

      "Bruno Jouhier [MVP]" <bjouhier@clu b-internet.fr> escribió en el mensaje
      news:eW0tf$$rDH A.3744@TK2MSFTN GP11.phx.gbl...[color=blue]
      > If you want "reference" semantics, use a class not a struct. Just replace
      > "struct" by "class" and your example will work.
      >
      > Bruno.
      >
      > "~toki" <pedorro77.hotm ail.com> a écrit dans le message de
      > news:%23pQBRU$r DHA.2304@TK2MSF TNGP12.phx.gbl. ..[color=green]
      > > I need to write a value over a field of a structure.
      > > ....
      > > public struct ARSubMesh {
      > > private Matrix matrix;
      > > public void setMatrix ( Matrix mat ) {
      > > this.matrix = mat;
      > > }
      > > }
      > > ....
      > > ARSubMesh mesh = getMesh("torus" );
      > > mesh.setMatrix ( mat );
      > > ....
      > >
      > > public ARSubMesh getMesh(string name) {
      > > // find the mesh *name*
      > > return mesh_found;
      > > }
      > >
      > > This don't work because getMesh create a *new* object.
      > >
      > > I need some like "return ref mesh_found", but the compiler say "Hey,[/color][/color]
      learn[color=blue]
      > a[color=green]
      > > bit of c# or ask to the experts..."
      > > Well, here i am, askin to the experts.
      > >
      > > How can i write directly over the struct field matrix from a returned[/color]
      > value?[color=green]
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...