xsharp.eu • Casting - the general case etc.
Page 1 of 1

Casting - the general case etc.

Posted: Sun Nov 19, 2017 2:26 pm
by Phil Hepburn
Hi Robert, Chris and Team,

I have a small problem / issue and need some help please.

I wish to be able to write code so that a cast I make can be done in a general way and not by me typing a specific class name / type. The attached very small image will help explain what I am after.
Casting_01.jpg
Casting_01.jpg (40.18 KiB) Viewed 320 times
In line 114 I wish to provide a variable name and not 'Double' - as in the end I am to make some very general code where the Type comes in as a parameter, or Generic type.

I seem to have tried most things with Type and typeof() and GetType() etc., but have not coded as yet in a Generic fashion.

What is the correct and proper way to do this ?

HELP !!! please,
Phil.

Casting - the general case etc.

Posted: Sun Nov 19, 2017 8:10 pm
by robert
Phil,
What are you trying to do ?
If you want to create a method that takes "any type" you should use a generic type parameter:


Robert

Casting - the general case etc.

Posted: Sun Nov 19, 2017 8:50 pm
by Phil Hepburn
Hi Robert,

Yes, thanks - I now have my "foot in the door" to solving my problem.

This is what I have done to research my possible solution.
Casting_11.jpg
Casting_11.jpg (30.98 KiB) Viewed 320 times
Casting_12.jpg
Casting_12.jpg (44.19 KiB) Viewed 320 times
Casting_13.jpg
Casting_13.jpg (34.43 KiB) Viewed 320 times
I think this approach should solve my problems !?

Although what I am doing is an interesting X# coding challenge, it may not be of much day-to-day use to many / any folks.

However, I have built a basic class wrapper around the System.Array class so that I now have a functioning single array class which allows any value index limits. The test class worked for STRING and I was trying to convert it to accept any array item class.

Nearly there I feel.

Project task is a good one to help improve our X# syntax 'mastery' - so don't say its a waste of time. And, it makes some useful code to include in the ClickStartXSharp eNotes.

More on this stuff when I get it to work 100%

Thanks for trying to help.
Cheers,
Phil.