Steve Jackson

Consider the following fields; The ModbusReadGroup classis part of my project, so I can change it as needed;

Code Snippet

ModbusReadGroup<bool> coilsReadGroup = new ModbusReadGroup<bool>();

ModbusReadGroup<bool> discreteInputsReadGroup = new ModbusReadGroup<bool>();

ModbusReadGroup<short> inputRegisterReadGroup = new ModbusReadGroup<short>();

ModbusReadGroup<short> holdingRegisterReadGroup = new ModbusReadGroup<short>();

No consider my ugly way of trying to use them;

Code Snippet

private void readTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

// Read any coils

if (coilsReadGroup != null)

{

bool[] _coils = new bool[coilsReadGroup.Length];

fieldTalk.readCoils(1, coilsReadGroup.StartAddress, _coils);

coilsReadGroup.setValues(_coils);

}

// Read any discrete inputs

if (discreteInputsReadGroup != null)

{

bool[] _inputs = new bool[discreteInputsReadGroup.Length];

fieldTalk.readInputDiscretes(1, discreteInputsReadGroup.StartAddress, _inputs);

discreteInputsReadGroup.setValues(_inputs);

}

// Read any input registers

if (inputRegisterReadGroup != null)

{

short[] _registers = new short[inputRegisterReadGroup.Length];

fieldTalk.readInputRegisters(1, inputRegisterReadGroup.StartAddress, _registers);

inputRegisterReadGroup.setValues(_registers);

}

// Read any holding registers

if (holdingRegisterReadGroup != null)

{

short[] _registers = new short[holdingRegisterReadGroup.Length];

fieldTalk.readMultipleRegisters(1, holdingRegisterReadGroup.StartAddress, _registers);

holdingRegisterReadGroup.setValues(_registers);

}

I feel like I should be able to put it all in an arraylist and do a for each, but I'm not sure what to do with the different array datatypes (short of making them objects and doing a bunch of memory sucking conversions. It seems like I should be able to define the array datatype based on the datatype used for the generic (ModbusReadGroup<>), but I'm not having luck with that.

Any ideas

Thanks




Re: Visual C# General Generics - Save me from myself;

Peter Ritchie

You can't instantiate a generic without the template type, so you can't declare a reference to an instantiated generic without the template type. This is due partially to generics' lack of covariance. I.e. GenericType != GenericType.

You'd have to have a common, non-generic, base for your ModbusReadGroup<> type in order to declare a non-Object array of those different types. e.g.

Code Snippet

class MyBase

{

// ...

}

class ModbusGroup<T> : MyBase

{

// ...

}

List<MyBase> list = new List<MyBase>();

list.Add (new ModbusGroup<int>());

list.Add(new ModbusGroup<bool());

... but, I doubt that would be useful because it would be hard to get to a specific ModbusGroup<> type






Re: Visual C# General Generics - Save me from myself;

Steve Jackson

OK, thanks Peter;






Re: Visual C# General Generics - Save me from myself;

Robert C. Barth

I believe if you make MyBase an Interface instead of it being a base class, your list<> will return the actual type of the objects within the list, needing no casting, and providing the functionality desired.