1

What I want to do is, I want to pass a pointer to a function that may be any type of a variable(int, long, string, even a class I mean I should be able to pass any variable's pointer). I'm doing this like that

unsafe class whatever
{
    whatever(object* variable)
    {
          this.variable = variable;
    }
}

ERROR IS: Cannot take the address of, get the size of, or declare a pointer to a managed type ('object')

Why I want to do is, I will store the variables passed through the constructor and will use their ToString() method, I'm trying to make a class that is for console applications, Refreshing the variables with their updated variables.

If I could do it like that My code will look like that

unsafe class whatever
{
    whatever(object* variable)
    {
        this.variable = variable;
    }

    object* variable;

    public override string ToString()
    {
        return *variable.ToString();
    }
}
1
  • 4
    Why do you want to do that? There must be a better solution than trying to use pointers in a managed language. Commented Jun 23, 2011 at 14:02

4 Answers 4

4

Maybe you should pass in a delegate that your class can use to obtain the “object string”.

class MyFancyClass
{
    Func<string> getObjectString;

    public MyFancyClass(Func<string> getObjectString)
    {
        this.getObjectString = getObjectString;
    }

    private MyOtherThread()
    {
        // ...
        string desc = getObjectString();
        // ...
    }
}

// ...
long value = 34;
MyFancyClass fancy = new MyFancyClass(() => value.ToString());

// ...
value = 88;
// getObjectString() should now reflect the new value.
// The variable is captured in the lambdas closure.

Be careful though, because the delegate is called from another thread and simply calling ToString() may not be safe for all objects and require locking. However a delegate allows the caller to do this, depending on the object.

Pointers will get ugly, require unsafe code and aren't stable. The garbage collector can move objects around freely, if you don't explicitly make the pointers fixed. References can't be stored and you can only pass them around.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't know anything about delegates, either Func<> thing. But that seems the right solution for me (I'll try also), but I keep waiting for an answer for pointers of "object"
nice solution! @user810576 you really should try to avoid pointers unless really necessary (interfacing with unmanaged code etc.). This is a case where a pointer really isn't needed.
Well as I said, the garbage collector can move objects around freely. And it will in fact do so, to compact and defragment the used address space. You can instruct the garbage collector to leave an object at its address with the fixed statement, but as far as I know the scope is always restricted. So you can't put the object at a fixed address for all time and eventually your watcher thread will access something else or screw up entirely.
Plus: delegates allow you to plug-in arbitrarily complex code, that can access local variables. Implement some locking scheme? Fine. Use some other format than ToString()? No problem. Get the sum of multiple values in a list? Sure.
1

In C#, you usually don't use pointers. If you want to refer to a storage location, try this:

whatever(ref object variable)
{

}

Else, i would rather recommend using a wrapper class or another way to get to some variable. A wrapper might look like this:

class Wrapper
{
    public object Value { get; set; }
}

5 Comments

I mean I have to hold the variable adress because I will be using it on another thread watching the what variable is it holding bla. bla.
Then use a wrapper class or something like that. Using pointers for this is not appropriate.
So How Can I hold the address of referenced variable?
See my edit. You can use a wrapper class for holding the value. That way, you pass the wrapper instance and can observe the value. (because the Wrapper variable stays the same object, just the value in it changes). Then you don't even need any pointers or refs.
yes, instead of the object*, you would have a Wrapper variable. Then you create a single instance of this wrapper and pass that to your method. Then every part of your program that modifies the value, modifies it by using that wrapper object.
1

If you want to get the address of a variable it can't be a complex managed type. However, you can do something like this (assuming an unsafe context):

int a = 1;

IntPtr addr = ( IntPtr )( &a );

( * ( int* ) addr ) = 4;

a = 4;

Is this what you're looking for?

Edit:

If you're just wanting to save a "function pointer" from the object, just use a delegate to hold that.

class whatever
{
    public whatever(object variable)
    {
        getVariableValue = variable.ToString;
    }

    Func<string> getVariableValue;

    public override string ToString()
    {
        return getVariableValue();
    }
}

void Main()
{
    var type = new { F1 = "test", F2 = "value" };

    whatever w = new whatever( type );

    Console.WriteLine( w ); // This will invoke ToString
}

Output: { F1 = test, F2 = value }

2 Comments

Yes but this is only for int typed variables, I mean I have to initialize a global type like the base class of all classes in C#, object.
@user Am I missing something?
-1

You want a void* but becareful with those. if you make the wrong cast it will crash. it might be a wise idea to make a struct and cast to an intptr in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.