Extension Methods & Pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Extension Methods & Pointers

  1. #1
    Join Date
    May 2006
    Posts
    306

    Extension Methods & Pointers

    Okay, so I'm having this problem.

    I defined this animation function (similar to jQuery if you have heard of it) for controls. Notice: I condensed the code into one box, the two methods are in two different classes.

    Code:
    private unsafe void FormMain_Click(object sender, EventArgs e)
    {
        this.Animate(&Width, this.Width + 50);
    }
    
    public static Control(this Control C, int* Property, int Value, float Time)
    {
        // Calculate Steps
        &Property += 50;
    }
    Now, the way I have it set up is that you can pass a property by reference, so that the function can edit it directly, instead of use a copy, right?

    It keeps giving me an error in the FormMain_Click:

    "Cannot take the address of the given expression."

    Please help. Also, if I was to get this working correctly, I don't know how to get the interval set correctly. It has to do with dividing the Time by the property and the value passed, but I don't know the algorithm.
    Last edited by code?; August 15th, 2009 at 04:02 AM.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Extension Methods & Pointers

    Generally speaking if you're using pointers and you're not interoping with native code, you're doing it wrong. Firstly, you can't take the address of a property, which is what you've done there. You also can't pass the address of it by ref, which is what you really wanted to do.

    Code:
    private unsafe void FormMain_Click(object sender, EventArgs e)
    {
        int width = Width;
        this.Animate(ref width, width + 50);
        Width = width;
    }
    
    public static Control(this Control C, ref int Property, int Value, float Time)
    {
        // Calculate Steps
        Property += 50;
    }
    thats the best you can do.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Extension Methods & Pointers

    Like Mutant said, I think that you should go off and find a good tutorial about reference v.s. value types. You do not need to do anything special to change a reference type when passing it to another method. You are passing a copy of the reference, so modifying the object it points to will work. Now, if you need to assign the reference to a new different object in memory you will need to use the 'ref' keyword in the function signature so that the original reference is passed instead of a copy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center