views:

684

answers:

4

When i try to pass the address of a public variable like this:

ML.Register("Radius", &lBeacons[i].Radius, 0.0f, 200.0f, 10.0f);

I get this error:

error CS0212: You can only take the address of an unfixed expression inside of a fixed statement initializer

The Register function looks like this:

public unsafe void Register(string desc, float* val, float minimum, float maximum, float stepsize)

Beacons is a List. It holds a class with the Public Radius.

+2  A: 

Why use pointers? You can just use the "ref" keyword to pass a reference of the variable.

example:

object blah = new object();

CallMethod(ref blah);


public void CallMethod(ref object param)
{

}
CSharpAtl
Yes, i forgot the mention i want to store the address of Radius (and others) in ML for future things... Thats why i use pointers (and my C++ thinking)
Intosia
This could be slightly to the OP, because now you are passing, by reference, a reference to a boxed copy of the variable - which isn't the same as passing a reference to the variable itself...
Marc Gravell
Are you saying that because I am using a reference type instead of a value type..sorry got confused?
CSharpAtl
I think I know what you are saying, and I was just trying to show the passing of references..guess I could have been more concise to this example....need to work on the wording of my answers :)
CSharpAtl
It is tricky, because there is "passing a reference" and "passing by reference" - and all 4 permutations are valid.
Marc Gravell
completely understand what you are saying....and I was just using a generic example to show the "ref". Completely understand what you mean though.
CSharpAtl
+7  A: 

Would it not make sense just to pass the value by reference?

public void Register(string desc, ref float val, float minimum,
         float maximum, float stepsize) {...}

Of course, using public variables (fields) is a bad idea too... it would work like so:

ML.Register("Radius", ref lBeacons[i].Radius, 0.0f, 200.0f, 10.0f);

But it won't work if you make Radius a property - so don't do this. Consider passing the beacon (or similar) itself, or some other object-based (or maybe event-based) mechanism.


something like:

ML.Register("Radius", lBeacons[i], 0.0f, 200.0f, 10.0f);

with:

private Beacon beacon;
public void Register(string desc, Beacon beacon, float minimum,
         float maximum, float stepsize) {
    this.beacon = beacon;
}
void Foo() {
    beacon.Radius++; // etc
}

Here we have a reference to the Beacon object, which doesn't have the unsafe issues of pointers. If you don't want to expose the Beacon directly, consider using an interface.

Marc Gravell
just cannot compete against a guy with your rep score...:)
CSharpAtl
I dont take offense....you give very good, concise answers......very complete.
CSharpAtl
Of course you can ;-p
Marc Gravell
A: 

You can try using a fixed block.

fixed(float *rad = &(lBeacons[i].Radius))) {
    ML.Register("Radius", rad, 0f, 200f, 10f);
}

I don't think you want to do this in any case. If ML uses rad outside of the fixed block, it is likely to fail. I think you're better off rethinking your API.

plinth
I doubt that would work. You are getting the address of the copied valuetype, I think.
leppie
+1  A: 

To get a pointer to a variable, use the fixed keyword to pin it in place, like:

fixed(float *radius=&lBeacons[i].Radius)
{
  ML.Register("Radius", radius, 0.0f, 200.0f, 10.0f);
}

The method this gets placed in needs to be declared as unsafe.

Blindy