Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

Pointers or similar functionality in C#.

This topic is 5244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to do something like pointers in C#? That is, have something like int a; int* b = &a; and then changes in a will reflect in *b; Sorry, I''m still kinda new to C#. --Vic--

Share this post


Link to post
Share on other sites
Advertisement

public unsafe void function()
{
int a;

fixed (int *b = &a)
{
*b = 10;
}
}


project properties: allow unsafe code blocks = true;


the reason fixed is necessary is because a is a managed object. if you want a pointer to a stackalloc object or an unmanaged object (say from a C++ cll or something) the fixed isn't required.

[edited by - Nypyren on January 11, 2004 5:09:28 PM]

Share this post


Link to post
Share on other sites
You can use references in method signatures:

void Foo( ref int bar )
{
bar = 42;
}

int a = 0;
Console.WriteLine( a ); // prints 0

Foo( ref a );
Console.WriteLine( a ); // prints 42



You *can* also use pointers:

unsafe void Bar()
{
int a;
int* b = &a;
*b = 42;
}

Ordinarily, you shouldn''t use pointers.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
quote:
Original post by Nypyren
the reason fixed is necessary is because a is a managed object. if you want a pointer to a stackalloc object or an unmanaged object (say from a C++ cll or something) the fixed isn''t required.

No. a is on the stack. No fixed necessary.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Also, "ref" parameters need to be initialized (acting both
as input and output parameter), while "out" parameters do not
need to be.




Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
Well, if pointers are not recommended, then I want to do it the *right* way. What I''m doing is creating a GUI. In my GUI, I have an object called a TextBox. This text box has a member

object referenceObject;
int type;

I want to be able to do something like:

TextBox myTextBox = new TextBox();
int a;
myTextBox.SetReferenceObject(a);

And inside the class I want to be able to alter this integer through changes in the textBox (whatever the user types and formatting). I have everything set up except the pointer, and am trying to figure out how to do that.

--Vic--

Share this post


Link to post
Share on other sites
all "class" instances in C# are reference types - each time you assign a class-type variable to a class-tpye instance, it adds a new "reference" to the instance (so that the instance does not delete itself until noone has a reference to him anymore). whenever you assign a class-type variable to null (which happens automatically when the class-type variable goes out of scope), it decrements the reference count of the instance it was pointing at automatically, and it will eventually get cleaned up for you when noone is referenceing it anymore.

This is pretty different from the way you do this kind of thing in C++. You don't use pointer syntax anymore, and you don't have to worry about deleting objects. (there are certain objects built into .Net that you do need to "Dispose" though)


class A
{
public ClassC foo;
//...
}


class B
{
public ClassC foo;
//...
}

class C
{
//...
}


static void Main()
{
A a = new A();
B b = new B();
C c = new C();

a.foo = c;
b.foo = c;

// they are both poiting to the same object. if you then say
a.foo.variable1 = 10;
// then b.foo.variable1 is also 10, because they are referencing the same object.
}



[edited by - Nypyren on January 11, 2004 6:02:03 PM]

[edited by - Nypyren on January 11, 2004 6:05:21 PM]

Share this post


Link to post
Share on other sites
Nypyren, I see that, but the issue is that I''m working with basic types, not objects. That is, classes are all done by reference, but basic types like int, float, string are by value. So my question is how can I work with basic types by reference (notice how in my example I used ints).

--Vic--

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!