Hester Hester - 3 months ago 10
C# Question

Need help figuring out 2 lines of code

I am new to programming. I am trying to figure out all the parts of these 2 lines of code below. If someone could break down the parts of the code. For example: this part is a variable; this other part is a method; and the other part is a property. If there are multiple options I would like to know them as well.

class Program
{
static void Main(string[] args)
{
var InteriorLight = GridTerminalSystem.GetBlockWithName("Interior Light") as IMyInteriorLight;
InteriorLight.ApplyAction("OnOff_On");
}
}

Answer
var InteriorLight = GridTerminalSystem.GetBlockWithName("Interior Light") as IMyInteriorLight;

Here you are calling the static method GetBlockWithName of class GridTerminalSystem and passing in a string argument "Interior Light". Then, the result is being casted to IMyInteriorLight, and assigned to InteriorLight variable.

InteriorLight.ApplyAction("OnOff_On");

Here you are calling the method ApplyAction on the IMyInteriorLight instance that was previously assigned to InteriorLight. You are passing in a string "OnOff_On" to the method. Apparently, the method does not return anything (void), as there is no assignment.