-
August 15th, 2009, 03:40 AM
#1
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.
-
August 15th, 2009, 05:28 AM
#2
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.
-
August 15th, 2009, 03:08 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|