0

So, I had a code like this (detail):

namespace itssafe
{
    public unsafe partial class testing : Form
    {
        unsafe private static int original = 10;
        unsafe private int tmp;

        unsafe setter() // I'll use this several time
        {
            tmp = original;
        }

        unsafe private void Form1_Load(object sender, EventArgs e)
        {
            setter();
        }

        unsafe private void timers_Tick(object sender, EventArgs e)
        {
            int* counter;   // working
            counter = &tmp; // not working, but I need this
        }
    }
}

And I had a global variable, called "tmp" and I need to create a pointer what point to it. But I can't, because I get "CS0212 You can only take the address of an unfixed expression inside of a fixed statement initializer" error. But the variable is global so I can't create in this method, because I need to use elsewhere too. What can I do?

I know I'm using to many unnecessary "unsafe", but i'm so desperate.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Gregori
  • 85
  • 7
  • There are no global variables in C#. This is just a static field in a class. – Enigmativity Sep 26 '18 at 00:26
  • 3
    You almost certainly do not need that; I've been working in C# for decades and never needed pointers. Why do you believe that you need a pointer? – Eric Lippert Sep 26 '18 at 00:31
  • Have you tried putting this in a [`fixed { }`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/fixed-statement) block like the error suggests? – Ron Beyer Sep 26 '18 at 00:31
  • 3
    Stop programming from a mindset of desperation. You will not write good solutions. **Programming in the unsafe subset is one of the hardest things to do correctly in C# and you need to have a thorough, accurate and deep knowledge of the memory model and its implementation details to do so**. Proceed like an engineer: study the system, understand its characteristics, understand the parts at your disposal, and build a robust, well-engineered solution that meets your user's requirements. – Eric Lippert Sep 26 '18 at 00:34
  • 2
    You ask "what can I do?" but we have no idea whatsoever of what you're *trying* to do, so it is impossible to tell you how to do it. This is what we call an "XY" problem. You have a real problem. You have a completely wrong idea about how to solve it. Now you are asking a question about the wrong idea. Don't do that. Ask a question that explains the real problem that you have, rather than the problem you are creating for yourself by using pointers incorrectly. – Eric Lippert Sep 26 '18 at 00:36
  • 1
    @Enigmativity: It's not even a static field. `tmp` is an instance field. – Eric Lippert Sep 26 '18 at 00:36
  • I see no reason for using pointers in the code you have provided. Just use `tmp` and don't try to use pointers to handle `int` as a reference type when it isn't. – ProgrammingLlama Sep 26 '18 at 00:43
  • @EricLippert - Sorry, I thought we were looking at `original`, not `tmp`. – Enigmativity Sep 26 '18 at 01:27

0 Answers0