Passing the address of the variable public

When I try to pass the address of a public variable as follows:

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)

Lighthouses are a list. He has a class with Public Radius.

+3
source share
4 answers

Doesn't it make sense to just 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 also a bad idea ... it will work like this:

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

But that will not work if you make a Radiusproperty - so don't do it. Consider transmitting a beacon (or similar) mechanism itself or some other object-oriented (or possibly event-based) mechanism.


sort of:

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

from:

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

Beacon, unsafe . Beacon, .

+7

, , , :

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

, , .

+4

? "ref" .

:

object blah = new object();

CallMethod(ref blah);


public void CallMethod(ref object param)
{

}
+2

.

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

, . ML rad , , , . , API.

0

Source: https://habr.com/ru/post/1708893/


All Articles