Dynamic Structures The values are not assigned

  • Thread starter Thread starter zequion1
  • Start date Start date
Z

zequion1

Guest
I have checked this problem for years. I have two different structures that I need to pass as parameters to a common function. The two structures have within another structure that is common to both. If the root is dynamic, the values are not assigned in the internal structure.

In the background there is also the problem that instead of having a single structure ref dynamic I have to have two structures because you cannot pass ref struct to ref dynamic or vice versa. Why?

It does not help me to be told to think things differently because in the common function I have many things that are general to both structures and some things that are different.

public struct St_A
{ public dynamic MyValue1;
public string MyValue2;

public struct St_A1 StProp
public struct St_COMMON StCommon;
}

public struct St_B
{ public struct St_COMMON StCommon;
}

public struct St_A1
{ public dynamic MyValue3;
public string MyValue4;
}

public struct St_COMMON
{ public int Value;
}

MyFunction()
{ struct St_A StA = new St_A();
struct St_B StB = new St_B();
dynamic MyRet = Function_Common(true, ref StA, ref StB)
}

public static Function_Common(bool Struct_is_A, ref St_A StA, ref St_B StB)
{ dynamic MyStruct = null;

if (Struct_is_A) MyStruct = (St_A)StA;
else MyStruct = (St_B)StB;

// This Works.
MyStruct.MyValue1 = "10";
MyStruct.MyValue2 = "10";

// PROBLEM. It does not produce an error and does not assign the value. If from the debugger I perform the same action, then it is assigned.
MyStruct.StProp.MyValue3 = "10";
MyStruct.StProp.MyValue4 = "10";

// PROBLEM. It does not produce an error and does not assign the value. If from the debugger I perform the same action, then it is assigned.
MyStruct.StCommon.Value = 10;
}

Continue reading...
 
Back
Top