Jablonovo Jablonovo - 15 days ago 6
C# Question

c# using other class method

Thanks to NHMountainGoat for an answer!
Implementing Interface looks a good choice so we have only the 'needed' method instanciated.
It looks like this now:
EDIT

class Machine
{
//REM: MachineConnexion is a link to the main server where asking the data
internal linkToPLC LinkToPLC;
public IlinkToPLC ILinkPLC;
public interface IlinkToPLC//Interface to linkPLC
{
Int16 MachineNumIS { get; set; }
}
internal class linkToPLC : IlinkToPLC
{
private Int16 Act_MachineNum;
private List<string> genlnkPLCCanvas;
private List<string> genlnkPLCworkingwith;
static private List<string> ListSymbolNoExist;
private string[] ListToPLClnk = {
"GlobalFolder.PMachine[{0}].",
"GlobalFolder.PMachine[{0}].STATE.",
"GlobalFolder.Machine[{0}].",
"GlobalFolder.Machine[{0}].STATE.",
};
public linkToPLC()//ctor
{
genlnkPLCCanvas = new List<string>(ListToPLClnk);
genlnkPLCworkingwith = new List<string>(ListToPLClnk);
ListSymbolNoExist = new List<string>();
Act_MachineNum = MachineNumIS;
}
public Int16 MachineNumIS { get { return (Int16)ReadWriteMachine("data"); } set { ReadWriteMachine("data", value); } }
public string ValueExist(string ValueToreach, bool WorkingDATA = false)
{
if (!WorkingDATA)
{
for (int inc = 0; inc < genlnkPLCworkingwith.Count; inc++)
{
string StrValueToReach = genlnkPLCworkingwith[inc] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[inc] + ValueToreach);
}
}
else if (WorkingDATA)
{
string StrValueToReach = genlnkPLCworkingwith[10] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[10] + ValueToreach);
}
if (ListSymbolNoExist.Count != 0)
{
string ErrorList = "";
for (int inc = 0; inc < ListSymbolNoExist.Count; inc++)
{
ErrorList = string.Concat(ErrorList + "Num: " + inc.ToString() + " " + ListSymbolNoExist[inc].ToString() + "\n");
}
Console.WriteLine("Error" + ErrorList);
}
return null;
}
public object ReadWriteMachine(string VariableName, object DataToWrite = null, bool WorkingDATA = false)
{
string valueToFind = "";
if (ValueExist(VariableName) != "FALSE")
{
if (DataToWrite != null) { MachineConnexion.WriteSymbol(valueToFind, DataToWrite); }
return MachineConnexion.ReadSymbol(valueToFind);
}
return VariableName;
}
}
public Machine() //constructor
{
LinkToPLC = new linkToPLC();
}
}


And It doesn't work telling me that the reference object is not defined to an instance of the object..... in the line : Machine() LinkToPLC = new linkToPLC();//REM I found the bug, it was me ;o)) 24112016

//REM 24112016

What are the main differences between those two concept: static Instance and Interface?

Example:

class Program
{
static void Main(string[] args)
{
ITestInterface InterInstance = new TestInterface();
//test Interface
bool value1 = true;
value1 = InterInstance.invert(value1);
InterInstance.print(value1);
//test Instance static
TestStaticInstance staticInstance = new TestStaticInstance();
staticInstance.Instance.invert(value1);
staticInstance.Instance.print(value1);
Console.ReadKey();
}
}
class TestInterface : ITestInterface
{
public bool invert(bool value)
{
return !value;
}
public void print(bool value)
{
Console.WriteLine(value.ToString()+"\n");
}
private void methodX()
{ }
}
interface ITestInterface
{
bool invert(bool value);
void print(bool value);
}
public class TestStaticInstance
{
public TestStaticInstance Instance;
public TestStaticInstance()
{
Instance = this;
}
internal bool invert(bool value)
{
return !value;
}
internal void print(bool value)
{
Console.WriteLine(value.ToString());
}
}


Thanks

Answer

Can you structure your other classes to take an instance of the link class? See:

/// <summary>
/// just a stub to demonstrate the model
/// </summary>
internal class Machine
{
    public string ReadData() { return "this is data"; }
    public void WriteData(string data) { Console.WriteLine(data); }
}

internal interface IMachineDataAccessor
{
    string Read();
    void Write(string data);
}

class LinkClass : IMachineDataAccessor
{
    protected Machine _machine;

    public LinkClass(Machine machine)
    {
        _machine = machine;
    }

    public void DoMyWork()
    {
        // insert work somewhere in here.

        string dataFromMachine = Read();

        Write("outbound data");
    }

    public string Read()
    {
        return _machine.ReadData();
    }

    public void Write(string data)
    {
        _machine.WriteData(data);
    }
}

class PersistentClass
{
    IMachineDataAccessor _machineImpl;

    public PersistentClass(IMachineDataAccessor machineAccessImplementation) 
    {
        _machineImpl = machineAccessImplementation;
    }

    public void DoMyWork()
    {
        string dataFromMachine = _machineImpl.Read();

        // insert work here.  Or anywhere, actually..

        _machineImpl.Write("outbound data");
    }
}

class StateClass
{
    IMachineDataAccessor _machineImpl;

    public StateClass(IMachineDataAccessor machineAccessImplementation) 
    {
        _machineImpl = machineAccessImplementation;
    }

    public void DoMyWork()
    {
        string dataFromMachine = _machineImpl.Read();

        // insert work here.  Or anywhere, actually..

        _machineImpl.Write("outbound data");
    }
}

static void Main(string[] args)
{
    LinkClass link = new LinkClass(new Machine());
    PersistentClass persistent = new PersistentClass(link as IMachineDataAccessor);
    StateClass state = new StateClass(link as IMachineDataAccessor);

    persistent.DoMyWork();
    state.DoMyWork();
    link.DoMyWork();
}