Home > Back-end >  Make struct pointing to different struct in C#
Make struct pointing to different struct in C#

Time:08-28

class IntComponent
{
   public int size;
}

class IntReferenceComponent : IntComponent
{
   public IntComponent target; // keep my size same as target size

   private void OnValidate()
   {
     //triggered on target assignment
   }
}

Is it possible in C# make struct variable pointing to different struct variable like with objects ? Even with unsafe pointers ? My current solution:

class IntComponent
{
   public Action onSizeChange;
   private int _size;
   public int size
   {
    get{return _size;}
    set
    {
      _size = value;
      onSizeChange?.Invoke();
    }
   }
}

class IntReferenceComponent : IntComponent
{
   public IntComponent target; // keep my size same as target size

   private void OnValidate()
   {
     //triggered on target assignment
     target.onSizeChange -= OnSizeChange;
     target.onSizeChange  = OnSizeChange;
   }
   
   private void OnSizeChange()
   {
      size = target.size;
   }
}

Is there some dark magic on this ?

CodePudding user response:

You don't need to do this in your case, and generally it would be ill-advised to try and mess around with pointers in normal Object Oriented cases, as you don't normally expose the internals of a class.

Instead, just use composition, with an outer property exposing the value of the inner object

class IntReferenceComponent : IntComponent
{
    public IntComponent target; // keep my size same as target size

    public int Size
    {
        get => target.size;
        set => target.size = value;
    }
}

CodePudding user response:

You Can not do this with struct since its value type means you only can take a copy from it and you can't use it as a pointer as you do with classes

Below Code shows that even with pointers unsafe code will not work because you, in the end, will copy the values to a struct to use them,

since you can't access the object attribute with the pointer that just `point to the object itself (this is what I know )

namespace Feto
{
    internal struct Complex
    {
        public float real;
        public float imag;

        public Complex(float real, float image)
        {
            this.real = real;
            this.imag = image;

        }
        public override string ToString()
        {
            return $" Class Complex {this.real} , {this.imag}";
        }
    }
    class FixingComplex 
    {

        public float real;
        public float imag;
        public FixingComplex(float real, float image)
        {
            this.real = real;
            this.imag = image;

        }
        public override string ToString()
        {
            return $" Class Complex {this.real} , {this.imag}";
        }
    }

    unsafe class program
    {
        public static void Main()
        {

            Complex x = new Complex(10, 20);
            Console.WriteLine(x);
            Complex* y = &x;
            //address of stuct
            Console.WriteLine((int)&x);
            //the y point to it
            Console.WriteLine((int)y);
            //what z point to
            Console.WriteLine(*y);
            //send the addresss of the stuct
            addNumbers(y);
            Console.WriteLine(x);
            Console.ReadKey();

            void addNumbers(Complex* result)
            {
                //make sure it is the same address of stuct
                Console.WriteLine((int)result);
                //now here we got the address of struct we need to modify it
                Console.WriteLine(*result);
                //Here is the problem it will take copy 
                var value = *result;
                value.real = 8888;
                value.imag = 8888;
                //you can use fixedcomplex to go on with pointers and workaround
                .....
            }




        }


    }

}

But there is a Solution

The Cool point with small modifications like pathing the address as a reference, not a copy of the address this will workaround and change the values modify those codes

addNumbers(ref y);
void addNumbers(ref Complex* result)
*result = new Complex(8888,8888);

namespace Feto
{
    internal struct Complex
    {
        public float real;
        public float imag;

        public Complex(float real, float image)
        {
            this.real = real;
            this.imag = image;

        }
        public override string ToString()
        {
            return $" Class Complex {this.real} , {this.imag}";
        }
    }
    class FixingComplex 
    {

        public float real;
        public float imag;
        public FixingComplex(float real, float image)
        {
            this.real = real;
            this.imag = image;

        }
        public override string ToString()
        {
            return $" Class Complex {this.real} , {this.imag}";
        }
    }

    unsafe class program
    {
        public static void Main()
        {

            Complex x = new Complex(10, 20);
            Console.WriteLine(x);
            Complex* y = &x;
            //address of stuct
            Console.WriteLine((int)&x);
            //the y point to it
            Console.WriteLine((int)y);
            //what z point to
            Console.WriteLine(*y);
            //send the addresss of the stuct
            addNumbers(ref y);
            Console.WriteLine(x);
            Console.ReadKey();

            void addNumbers(ref Complex* result)
            {
                //make sure it is the same address of stuct
                Console.WriteLine((int)result);
                //now here we got the address of struct we need to modify it
                Console.WriteLine(*result);
                //Here is the problem it will take copy 
                //var value = *result;
                *result = new Complex(8888,8888);
                //you can use fixedcomplex to go on with pointers and workaround
                
            }




        }


    }

}
  •  Tags:  
  • c#
  • Related