HELP please - syntax for new class ...... >>

This forum is meant for anything you would like to share with other visitors
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

HELP please - syntax for new class ...... >>

Post by wriedmann »

Hi Robert,

even if I'm not Phil:
Maybe we should create a small tool that takes a block of C# code, parses it with Roslyn into a parse tree and then spits out X# code?
I would like such a tool very much, and I'm pretty sure a lot of other people would do the same.

This would be interesting not only to move C# code to X#, but sometimes to understand C# code better. Ideally such a tool should be callable from both Visual Studio and XIDE.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP please - syntax for new class ...... >>

Post by Phil Hepburn »

Thanks Robert,

Interesting, and I do understand ;-0) And yes, the curly braces may have made more sense I suppose.

Talking about Generics, I am making good progress with translating Nick's good 'DevShare 2015' sample involving Generics, BUT, this morning I thought I would visit your X# CHM notes and try some simple stuff for myself.

Since what follows is a general discussion, I have used images to help explain what I have done, and what the issues may be.

I went to the X# CHM file which came with Beta 10, and found the Generic example you wrote and included in the notes. I got the sample working for type INT, almost as you wrote it - just did a tweak so I could bind the Stack to a WPF control - see below :-
RvdH_Generics_01.jpg
RvdH_Generics_01.jpg (31.53 KiB) Viewed 219 times
And here is your coded slightly amended :-
RvdH_Generics_02.jpg
RvdH_Generics_02.jpg (133.51 KiB) Viewed 219 times
And here is my set-up in the Initialise method :-
RvdH_Generics_03.jpg
RvdH_Generics_03.jpg (36.67 KiB) Viewed 219 times
So then, what's the problem ? Well, it was when I took the next step to test things out. I thought the point of a Generic class was that I could use another type with it, say 'string' instead of 'int'. It was when I tried the 'string' type that I hit problems.

Here is the full Generic class I was suing :-
RvdH_Generics_04.jpg
RvdH_Generics_04.jpg (121.46 KiB) Viewed 219 times
And now the issue that I found when setting up a string example test :-
RvdH_Generics_05.jpg
RvdH_Generics_05.jpg (131.12 KiB) Viewed 219 times
Please can you tell me what needs changing / adding to the sample to get it to work successfully ?

Here is my test second Initialise method :-
RvdH_Generics_06.jpg
RvdH_Generics_06.jpg (72.21 KiB) Viewed 219 times
I know how to make Nullable types but don't know what to do to take 'string' and make it non nullable !?

Any pointers, suggestions, help etc., etc.. ?

TIA,
Phil.
Wales, UK.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP please - syntax for new class ...... >>

Post by Phil Hepburn »

Wolfgang / Robert,

That is what many of us have been waiting for ;-0)

I used to do stuff in the past using Fabrice's plug-in to Reflector, for Vulcan to and from C#.

Many of us could save LOTS of time by being able to deploy such a useful tool.

Also, this would cut down standard guys (like me) asking endless queries on the Forum about syntax ;-0) ;-0)

Power to your elbows,
Phil.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

HELP please - syntax for new class ...... >>

Post by NickFriend »

Hi Phil,

You've used WHERE T IS STRUCT, NEW() in your class declaration, and a string is not a struct. At a guess that could be your problem.

HTH

Nick

PS. The NEW() might be a problem as well in this case... in C# a struct can't have a parameterless constructor, so you might be giving an impossible set of criteria for T!
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

HELP please - syntax for new class ...... >>

Post by NickFriend »

Talking to myself here ;-) The new() should be fine as you're not actually declaring a parameterless constructor, which would be a problem.... simply calling new() on a struct is fine, which is all the qualifier is specifying.

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP please - syntax for new class ...... >>

Post by Phil Hepburn »

Hi Nick,

Then what do I write in place of STRUCT ?

I have just there now tried Object and Type and without 'NEW()' as well, and get no joy.

All I want is a coded class which will allow me to supply any Type in the ' < > ' either a value type or a business object reference type.

May be we need Robert or Chris on this one ?

Cheers,
Phil.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

HELP please - syntax for new class ...... >>

Post by NickFriend »

I would have thought just new() on it's own should be enough.... I'm not too sure what some of the syntax in the class means to be honest.

The idea of these qualifiers on T is to make sure that T has the correct characteristics to work with the generic class... you may not need any qualifiers at all.

Nick
User avatar
robert
Posts: 4258
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

HELP please - syntax for new class ...... >>

Post by robert »

Phil, Nick

The where clauses in the generic class declaration are all optional.
However you may want to use them when you want to access propertie or call methods on one of the declared types. By adding a where you can tell the compiler that the specific type will support a method or property.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

HELP please - syntax for new class ...... >>

Post by Phil Hepburn »

Thanks Robert,

Yes, that now gets me off on the right foot.

I can now start to add a few notes to my CHM on Generics - start simple and work up ;-0)

Attached is an image or two of success with the amended syntax :-
RvdH_Generics_11.jpg
RvdH_Generics_11.jpg (35.77 KiB) Viewed 219 times
RvdH_Generics_12.jpg
RvdH_Generics_12.jpg (57.92 KiB) Viewed 219 times
Regards to all,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

HELP please - syntax for new class ...... >>

Post by wriedmann »

Hi Phil,

these days I like the short property syntax much more:

Code: Select all

property Items a T[] get self:_Items set self:_Items := value
Or, since your Items property is get/set, you could use an auto property:

Code: Select all

property Items as T[] auto
Fortunately X# gives us a lot of possibilities to write code....

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply