Tricky boxing question

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

    #1

    Tricky boxing question

    I want to do something along the lines of this:

    class someclass {
    static void Main() {
    int i = 5;
    int j = 3;
    changevalue(ref i, j);
    Console.WriteLi ne(i.ToString() ); //I want this to output 3
    }
    //it's important that this parameter is an object, and not an int or
    something
    static void changevalue(ref object i, object j) {
    i = j
    }
    }

    I'd like this to work for any type, not just ints. Is there any way to
    accomplish something like this?

    Chris


  • 100

    #2
    Re: Tricky boxing question

    Hi Chris
    The best you can do, I believe, is to revise your program logic

    B\rgds
    100
    "Chris Capel" <chris@ibanktec h.net.zerospam> wrote in message
    news:eSycwOEYDH A.1384@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I want to do something along the lines of this:
    >
    > class someclass {
    > static void Main() {
    > int i = 5;
    > int j = 3;
    > changevalue(ref i, j);
    > Console.WriteLi ne(i.ToString() ); //I want this to output 3
    > }
    > //it's important that this parameter is an object, and not an int or
    > something
    > static void changevalue(ref object i, object j) {
    > i = j
    > }
    > }
    >
    > I'd like this to work for any type, not just ints. Is there any way to
    > accomplish something like this?
    >
    > Chris
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Tricky boxing question

      Hi Subash,
      I don't thing this is exactly what Chris wants to do. a and b are declared
      as a references to object. I think Chris wants to have changevalue method
      regardles of the type of variables. It is kind a silly to keep value types
      in boxing state. At least it is error prone.

      B\rgds
      100
      "Subash" <subashb-nospam@nospam.c om> wrote in message
      news:o$tveDFYDH A.2528@cpmsftng xa06.phx.gbl...[color=blue]
      > class Class1
      > {
      > static void Main()
      > {
      > // Declaration
      > object a, b;
      > // Integer stuff
      > a = 1;
      > b = 2;
      > changevalue(ref a, b);
      > Console.WriteLi ne(a.ToString() ); //This will output 2
      >
      > // string stuff
      > a = "string 1";
      > b = "string 2";
      > changevalue(ref a, b);
      > Console.WriteLi ne(a.ToString() ); //This will output "string 2"
      > }
      >
      > static void changevalue(ref object i, object j)
      > {
      > i = j;
      > }
      > }
      >[/color]


      Comment

      • Chris Capel

        #4
        Re: Tricky boxing question

        Yeah, that sounds ok. It's not an earth-shaking matter.

        Chris

        "100" <100@100.com> wrote in message
        news:#9bL3OOYDH A.2204@TK2MSFTN GP12.phx.gbl...[color=blue]
        >
        > Hi Chris,
        >[color=green]
        > > will change oA, and not a. In other words, I don't know how to create an
        > > object that references the original value type.[/color]
        > That is the point. You can't. Unless you make *changevalue* method to be
        > changevalue (ref int a, int b);
        >
        > or you have to return back the value to its original varable after[/color]
        changing[color=blue]
        > it.
        > int a = 1, b = 2
        > object oA = (object) a;
        > changevalue(oA, b);
        > a = (int)oA; //What about if it is not *int*
        >
        > B\rgds
        > 100
        >
        >[/color]


        Comment

        Working...