Using Generics in our own coded classes.

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Using Generics in our own coded classes.

Post 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 :-
Generics_01.jpg (18.3 KiB) Viewed 160 times
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 :-
Generics_02.jpg (49.73 KiB) Viewed 160 times
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 :-
Generics_03.jpg (10.22 KiB) Viewed 160 times
And the important method which accepts an input value (obviously a grid point object) check this out :-
Generics_04.jpg (25.94 KiB) Viewed 160 times
And in the next small image we use 'T' to define our result Type - check this out :-
Generics_05.jpg (48.86 KiB) Viewed 160 times
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 :-
Generics_06.jpg (43.63 KiB) Viewed 160 times
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 :-
Generics_07.jpg (40.43 KiB) Viewed 160 times
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.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Using Generics in our own coded classes.

Post by Phil Hepburn »

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.

Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Using Generics in our own coded classes.

Post by NickFriend »

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.

Post Reply