× Share your code snippets, screen shots etc. here

Using Generics in our own coded classes.

1 year 1 month ago #1 by Phil Hepburn
Using Generics in our own coded classes. was created by Phil Hepburn
Hi all X# Forum guys,

Recently, while researching .NET arrays (and Collections) Robert encouraged me to solve one of my problems using Generics. This has worked well and so I wished to share some thoughts with you on the topic of Generics.

Probably a lot of us guys use stuff like 'List<T>' and 'ObservableCollection<T>' and 'Stack<T>'. This is using the Generics which is already built into the .NET Framework. And helpful it is indeed.

This is good - BUT - we can go a step or two further, if we start to code generics into our own new Types - classes that is. Here is an example from my current working array class :-

Notice the '<T>' at the end of line 228.

In the rest of my code for this class, in Constructors, Properties and Methods, we can use the 'T' to represent the type of the object we wish to be passed into the instantiated object of type 'MyXIndexArray3D'. Here is my example :-

In the above code I am defining a property in my main WPF app to be an array of my type, based on a class of type 'XYZ_GridPoint'. Equally, I could have coded things to use 'INT' or 'STRING' or DateTime, instead of my own defined 'grid point' class.

So then, the rest of the code in my Generic class can use 'T' in many and varied, and appropriate places. Lets look briefly at a few such different places, starting with the creating of an instance of a System.Array object :-

And the important method which accepts an input value (obviously a grid point object) check this out :-

And in the next small image we use 'T' to define our result Type - check this out :-

You can also see that on line 383 we make a cast - this seems to be necessary to get the code to work correctly.

Finally, for this short post I will include a method to find and return all the elements in the array, 'T' is used quite a few times check this out :-

Its nice to be able to get at elements in a 3D array without having to loop through I,J, and K, as in the olds days.

Here is a suitable final image, showing the results of the "Customer Survey" being displayed in a WPF data bound fashion :-

Okay then guys, this is enough for one post, let me tell you that this example will be published in full (soon) in my 'ClickStartXSharp' eNotes which I will make available to you via my OneDrive folder - and a link here in a further post.

Lets just say that thanks to Robert's advice I have now got a suitable simple Generic class for storing defined class objects in a 3D array - where the index 'value ranges' for all 3 indexes can be anything at all reasonable, like -10 to 10, and 30 to 50, and -25 to -15 (these are X, Y and Z index ranges).

Now then folks, before you rush off and put Generics everywhere in your code, it can only be sensibly be used in your new business classes when it makes good sense to use it - probably the difficult part of doing our own Generics is spotting where to use it in our projects and solutions.

Remember, "When all we have in our toolbox is a hammer, then everything looks like a nail! "

Maybe Nick Friend can come in here as he has used it quite a bit in his code I believe.

Hope this proves useful to some .....
Wales, UK.

Please Log in or Create an account to join the conversation.

1 year 1 month ago #2 by Phil Hepburn
Replied by Phil Hepburn on topic Using Generics in our own coded classes.
Sorry folks,

Should have said that my use of R, C and S refers to Rows, Columns and Sheets.

So now you know that RI is row index, and CI column index etc.


Please Log in or Create an account to join the conversation.

1 year 1 month ago #3 by Nick Friend
Replied by Nick Friend on topic Using Generics in our own coded classes.
Hi Phil,

My rule of thumb is if I find myself repeating a class very closely, only varying the type of object being acted upon, it's probably a sign that I need to replace those classes with a single generic one.

Classic case is data access. You may want to have a simple data access class which can retrieve a single object by it's primary key, a list of objects by some search criteria, or else update an object. Most of the basic code will be the same, whatever the type of object being acted upon. So you create a generic base class, and then use subclasses to vary the details depending on the type of object.


Please Log in or Create an account to join the conversation.