Casting - the general case etc.

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Casting - the general case etc.

Post 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 148 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.
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Casting - the general case etc.

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Casting - the general case etc.

Post 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 148 times
Casting_12.jpg
Casting_12.jpg (44.19 KiB) Viewed 148 times
Casting_13.jpg
Casting_13.jpg (34.43 KiB) Viewed 148 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.
Post Reply