Page 2 of 3
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 3:09 pm
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
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 3:52 pm
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 (31.53 KiB) Viewed 456 times
And here is your coded slightly amended :-
- RvdH_Generics_02.jpg (133.51 KiB) Viewed 456 times
And here is my set-up in the Initialise method :-
- RvdH_Generics_03.jpg (36.67 KiB) Viewed 456 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 (121.46 KiB) Viewed 456 times
And now the issue that I found when setting up a string example test :-
- RvdH_Generics_05.jpg (131.12 KiB) Viewed 456 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 (72.21 KiB) Viewed 456 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.
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 4:02 pm
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.
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 4:13 pm
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!
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 4:19 pm
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
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 4:55 pm
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.
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 5:02 pm
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
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 5:13 pm
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
HELP please - syntax for new class ...... >>
Posted: Mon Mar 20, 2017 9:05 pm
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 (35.77 KiB) Viewed 457 times
- RvdH_Generics_12.jpg (57.92 KiB) Viewed 457 times
Regards to all,
Phil.
Wales, UK.
HELP please - syntax for new class ...... >>
Posted: Tue Mar 21, 2017 5:28 am
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:
Fortunately X# gives us a lot of possibilities to write code....
Wolfgang