0

I have a class function in C# in which I want to pass a pointer to the current class as an argument in a call to another function:

public class SomeClass
{
   public void SomeClassFunction()
   {
      SomeFunction(&this);
   }
}

public void SomeFunction(SomeClass* ptr)
{
}

I have very little C# experience, and I'm using Visual Studio 2022.

Doing &this results in CS0211: Cannot take the address of the given expression.

Doing *this results in CS0193: The * or -> operator must be applied to a pointer.

What is the proper way to pass a pointer to the current class from a class function?

Thanks for reading my post, any guidance is appreciated.

Runsva
  • 365
  • 1
  • 7
  • 4
    Do you really want to pass a pointer, or do you just need to pass a reference? C# doesn't support pointers by default. If you want to use pointers then you have to write `unsafe` code. That is very much the exception rather than the rule. I've been using C# for 20 years and never used a pointer. What are you actually trying to achieve? – jmcilhinney Feb 28 '23 at 00:00
  • I'm not really sure as I'm brand-new to C#. If a reference would let me access a class in the same way that a pointer in C++ would, then that would be the solution for me. I would just like to be able to access the class and it's functions via some sort of reference. – Runsva Feb 28 '23 at 00:06
  • just pass `this`, `this` is a pointer to the object, and the function should just take SomeClass as an arg, not SomeClass* – pm100 Feb 28 '23 at 00:06
  • Ok, but what would the argument type of the function I call be in order for that pointer argument to be accepted? Should it just be `SomeClass ptr`? – Runsva Feb 28 '23 at 00:07
  • 1
    Classes in C# are reference types, structs are value types. You have very little choice - the fact that `SomeClass` is a class means that `new SomeClass()` gets allocated on the heap, and `SomeClass x` is a reference to the value on the heap. This is quite an odd concept if you're used to explicitly declaring pointers/references. I recommend you read up on C# reference types – Andrew Williamson Feb 28 '23 at 00:09
  • 1
    It would probably be worth your time to take some free online intro to C# courses. There are very specific ways to think about objects in C# that are quite different from C++ and getting a solid foundation would be beneficial. – David L Feb 28 '23 at 00:20
  • This doesn't answer your question but it should help you understand https://stackoverflow.com/questions/69421426/what-is-the-c-sharp-equivalent-to-c-s-param-in-a-method-signature/69421775#69421775 – Flydog57 Feb 28 '23 at 04:48
  • Thanks for all the advice guys, I really appreciate it. I helped me clear up some misconceptions I had about classes in C#. And yes, I'm coming over from mostly C++, and I've never worked with C# before, so I'm adjusting. – Runsva Feb 28 '23 at 09:31

2 Answers2

2

you need

public class SomeClass
{
   public void SomeClassFunction()
   {
      SomeFunction(this);
   }
}

public void SomeFunction(SomeClass ptr)
{
}

all class objects in c# are references, so you dont need to pass pointers (like you would in C say)

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thank you; One further question: If `SomeFunction` in the example above was a setter function for a field of another, separate class, would that field remain as a reference to the original instance of `SomeClass`? In other words, can class fields in C# act as references to other classes? So will changes in the classes they point to always be reflected in the reference field? – Runsva Feb 28 '23 at 00:18
  • @OP - yes...... – pm100 Feb 28 '23 at 00:20
  • 1
    @Runsva, *"can class fields in C# act as references"*. They can't act any other way. Classes are reference types and structures are value types. That's probably a subject you should do a bit of reading on. – jmcilhinney Feb 28 '23 at 00:39
  • This does not answer the question as asked. I know that what OP wrote is unrelated to what they need... but then you should edit the question to match the answer. Really should be downvoted... but that will only make more positive votes and general discontent... Please consider fixing the question. – Alexei Levenkov Feb 28 '23 at 00:48
0

Just declare function as instance method in class. Then refer current instance by “this” keyword. As commented above the object (that is the all defined class’s base type) type is reference type that store references to their data. if you want to change reference (that what points data stored) you have to use “ref” keyword regardless it is reference type or valie type. For example; in method you want to create new instance and assign to sended parameter, you have to send variable with ref keyword.

jepozdemir
  • 260
  • 1
  • 4