Assigning a Reference to a Variable

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

    Assigning a Reference to a Variable

    Hello all,
    Thank you for taking the time to read and help me with my question.

    Here is a simplified program I have:


    int[,] array2D = { {1, 2, 3}, {4, 5, 6} };


    private void Main(){
    printarray2DVal ue(0);
    printarray2DVal ue(1);
    }

    printarray2DVal ue(int dValue){
    int length;
    int i=0;
    int x;
    int y;
    string str;

    if (dValue==0){
    length = array2D .GetLength(0);
    x=i;
    y=0;
    }
    else{
    length = array2D .GetLength(1);
    x=0;
    y=1;
    }

    for (i;i<length;i++ ){
    str += 2dArray[x,y].ToString();
    }
    MessageBox.Show (str);
    }

    Now, that obviously doesn't work how I would want it to. And I can't
    say "x= ref i" either because C# won't allow it. I read
    http://www.yoda.arachsys.com/csharp/parameters.html, and

    and I believe my answer is that I can not do what I want, unless I use
    unsafe code, or boxing.

    The easiest solution is just to split this into two methods, one for
    each dimension of the array. But, the more dimensions, the more
    methods, and thats not good.

    Any suggestions?

  • Jon Skeet [C# MVP]

    #2
    Re: Assigning a Reference to a Variable

    On May 14, 2:10 pm, Drakemar <mdeve...@hotma il.comwrote:
    Thank you for taking the time to read and help me with my question.
    >
    Here is a simplified program I have:
    <snip>

    Given that this *doesn't* do what you want it to, could you explain
    what you *do* want to achieve? It's not really clear to me at the
    moment.

    Jon

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: Assigning a Reference to a Variable

      Now, that obviously doesn't work how I would want it to.  And I can't
      say "x= ref i" either because C# won't allow it.  I readhttp://www.yoda.arachs ys.com/csharp/parameters.html , andhttp://groups.google.c a/group/microsoft.publi c.dotnet.langua ges.cshar...
      and I believe my answer is that I can not do what I want, unless I use
      unsafe code, or boxing.
      What wyo uwant to do in the first place? It seems like if you want to
      print all the members of one of the given dimensions, but if this is
      the case it's pretty convoluted way that you selected.
      The easiest solution is just to split this into two methods, one for
      each dimension of the array.  But, the more dimensions, the more
      methods, and thats not good.
      Your code neither works with more than two dimensions to start with.
      Any suggestions?
      What are you tryong to do?

      Comment

      • Peter Duniho

        #4
        Re: Assigning a Reference to a Variable

        On Wed, 14 May 2008 06:10:25 -0700, Drakemar <mdeverno@hotma il.comwrote:
        [...]
        for (i;i<length;i++ ){
        str += 2dArray[x,y].ToString();
        I have the impression that in this loop, you want "x" to either remain
        constant (0, if "dValue" is non-zero) or to follow the value of "i" (if
        "dValue" is zero).

        This particular example seems odd to me (for one, if "dValue" is non-zero,
        then you just keep using the same array element over and over). But
        assuming I've understood correctly, there is no direct way to accomplish
        what you want.

        IMHO, the most straight-forward approach would be to simply break the task
        out into two different methods, one for each scenario. Assuming your code
        example is truly representative, you'd wind up with very little extra
        lines of code (since you'd get rid of some of the initialization code),
        and it would probably be more obvious what the intent of the code is.

        One alternative is to define an expression that can be evaluated as
        needed. For example:

        Func<intx;

        if (dValue == 0)
        {
        length = array2D .GetLength(0);
        x = x1 =i;
        y = 0;
        }
        else
        {
        length = array2D .GetLength(1);
        x = x1 =0;
        y = 1;
        }

        for (i; i < length; i++)
        {
        str += 2dArray[x, y].ToString();
        }

        Then instead of being a simple variable, "x" is essentially a delegate
        that can be executed as necessary. If "dValue" is 0, it will return the
        current value of "i". If not, it will always return 0.

        As the other replies suggest, your question is not easy to understand. If
        the above isn't helpful, you should try to post some code that really does
        exactly what you want, even if in a less elegant way than you'd desire, or
        at least be MUCH more specific about what you feel the correct output
        would be. Posting code that doesn't work isn't the best way to convey
        what it is you _do_ want to happen. :)

        Pete

        Comment

        • Drakemar

          #5
          Re: Assigning a Reference to a Variable

          I apologize for my utterly unclear example=).

          Basically, what I wanted was to have a single function iterate through
          the two-dimensional array in a certain dimension based on one
          parameter.
          So, with the following code, I was wanting to have
          printarray2DVal ue(0) output "123", and printarray2DVal ue(0) output
          "14".

          int[,] array2D = { {1, 2, 3}, {4, 5, 6} };

          private void Main(){
          printarray2DVal ue(0);
          printarray2DVal ue(1);

          }

          printarray2DVal ue(int dValue){
          int length;
          int i=0;
          int x;
          int y;
          string str;

          if (dValue==0){
          length = array2D .GetLength(0);
          x=i;
          y=0;
          }
          else{
          length = array2D .GetLength(1);
          x=0;
          y=i; //note, this was a typo my first post. I wrote 1 when I
          should have wrote i.
          }

          for (i;i<length;i++ ){
          str += 2dArray[x,y].ToString();
          }

          MessageBox.Show (str);

          }


          I looked more into the Func<intthat you suggested and found that the
          following solved the problem for me:
          int[,] array2D = { {1, 2, 3}, {4, 5, 6} };

          private void Main(){
          printarray2DVal ue(0);
          printarray2DVal ue(1);

          }

          printarray2DVal ue(int dValue){
          int length;
          int i=0;
          int x;
          int y;
          string str;
          Func<intxLoc;
          Func<intyLoc;

          if (dValue==0){
          length = array2D .GetLength(0);
          xLoc = (()=>i);
          yLoc=0;
          }
          else{
          length = array2D .GetLength(1);
          xLoc =0;
          yLoc= (()=>i);
          }
          x = xLoc;
          y=yLoc;

          for (i;i<length;i++ ){
          str += 2dArray[x(),y()].ToString();
          }

          MessageBox.Show (str);

          }

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Assigning a Reference to a Variable

            On May 16, 1:55 pm, Drakemar <mdeve...@hotma il.comwrote:
            I apologize for my utterly unclear example=).
            >
            Basically, what I wanted was to have a single function iterate through
            the two-dimensional array in a certain dimension based on one
            parameter.
            So, with the following code, I was wanting to have
            printarray2DVal ue(0) output "123", and printarray2DVal ue(0) output
            "14".
            Hang on - so you're talking about going in a different *direction*
            rather than just effectively slicing different rows out of a
            rectangle?

            Jon

            Comment

            • Peter Duniho

              #7
              Re: Assigning a Reference to a Variable

              On Fri, 16 May 2008 05:55:07 -0700, Drakemar <mdeverno@hotma il.comwrote:
              [...]
              I looked more into the Func<intthat you suggested and found that the
              following solved the problem for me:
              Glad it worked. Sorry for the bad syntax in my post...I was rushing, with
              predictable consequences. :)

              I'll second Jon's sentiment that the actual implementation here seems
              weird. But assuming that's really what you want to do, it seems that this
              should work fine.

              Pete

              Comment

              • Alun Harford

                #8
                Re: Assigning a Reference to a Variable

                Peter Duniho wrote:
                On Wed, 14 May 2008 06:10:25 -0700, Drakemar <mdeverno@hotma il.comwrote:
                >
                >[...]
                > for (i;i<length;i++ ){
                > str += 2dArray[x,y].ToString();
                >
                I have the impression that in this loop, you want "x" to either remain
                constant (0, if "dValue" is non-zero) or to follow the value of "i" (if
                "dValue" is zero).
                >
                This particular example seems odd to me (for one, if "dValue" is
                non-zero, then you just keep using the same array element over and
                over). But assuming I've understood correctly, there is no direct way
                to accomplish what you want.
                I accept your challenge! :-)

                static void printarray2DVal ue(int dValue)
                {
                int length;
                int i = 0, zero = 0;
                TypedReference x, y;
                string str="";

                if (dValue == 0)
                {
                length = array2D.GetLeng th(0);
                x = __makeref(i);
                y = __makeref(zero) ;
                }
                else
                {
                length = array2D.GetLeng th(1);
                x = __makeref(zero) ;
                y = __makeref(i);
                }

                for (; i < length; i++)
                {
                str += array2D[__refvalue(x, int), __refvalue(y, int)].ToString();
                }

                MessageBox.Show (str);
                }

                Alun Harford

                Comment

                Working...