fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

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

HELP please - syntax for new class ...... >> 2 years 3 months ago #1411

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Hi guys,

Can anyone please suggest some X# syntax to express the following C# class ? :-



And for those how give me a hard time over images V text, here is some text to use :-

public abstract class GenericDataSourceBase<DSO, VMO, MAPPER, DAL> : IGenericDataSource<VMO>
where MAPPER : IGenericVMMapper<DSO, VMO>, new()
where VMO : IDSObjectWithState
where DSO : IDSObjectWithState
where DAL : IGenericDAL<DSO>, new()
{
// method to get a single object by its primary key
public virtual DSResult<VMO> GetData(int dataid)

Just to keep you in the picture, the translation of Nick friend's C# Generic Data, EF6 and MVVM sample app is progressing nicely. A lot of work before we seen any tangible results however ;-0)

TIA,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1412

  robert's Avatar robert Offline Posts: 1001
Phil,

Look at: https://www.xsharp.info/help/class.html
and
https://www.xsharp.info/help/generic-types.html

for the syntax.

It will become something like
CLASS GenericDataSourceBase<DSO, VMO, MAPPER, DAL> IMPLEMENTS IGenericDataSource ;
   WHERE MAPPER IS IGenericVMMapper<DSO, VMO>, NEW();
   WHERE VMO IS IObjectWithState ;
   WHERE DSO IS IObjectWithState ;
   WHERE DAL IS IGenericDAL<DSO>, New()

.
.
END CLASS

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Last edit: by robert.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1413

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Thanks Robert,

Got me on my way, and past the parser and compiler.

Out of interest the image shows the last line has 'IS' and not 'is'.



This is simply because when I tried to Build I got an error message regarding the same.

When I just there now tried to regenerate the error message to show you, the 'is' compiled OK !?

Here it is :-



I have more C# I need help with - but later. Am going to see some sport on my new Sky Sport 'Q' box.

Thanks again for your help so far.

Oh! I thought 'new()' was a C# way of instantiating - what does it mean to X# guys ?

Cheers,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1414

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Thanks Robert,

I am getting there okay it seems - BUT - there is some strange C# code (to me) in this code to deal with Generics and Nicks' data.

Can you help me with this :-



I have made an attempt as seen below :-



And so you can have text to work with below is both the X# and the original C# :-

============================================================================
public virtual method GetDataAsync(dataid as int) as Task<DSResult<VMO>>

return Task.Factory.StartNew<DSResult<VMO>>( { || ;
local result as DSResult<VMO>
try
local implied dal := DAL{}
local efdata := dal.DALGetData(dataid) as DSResult<DSO>

if (efdata.Result == DSResultType.Success)
local implied mapper := MAPPER{}
result := DSResult<VMO>{mapper.DSOtoVMO(efdata.Cargo)}
else
result := DSResult<VMO>{efdata.Result}
endif
catch e as Exception
result := DSResult<VMO>{DSResultType.Otherfailure, e}
end try
return result
} )
return result

========================================================================
public virtual Task<DSResult<VMO>> GetDataAsync(int dataid)
{
return Task.Factory.StartNew<DSResult<VMO>>(() =>
{
DSResult<VMO> result;
try
{
DAL dal = new DAL();
DSResult<DSO> efdata = dal.DALGetData(dataid);
if (efdata.Result == DSResultType.Success)
{
MAPPER mapper = new MAPPER();
result = new DSResult<VMO>(mapper.DSOtoVMO(efdata.Cargo));
}
else
{
result = new DSResult<VMO>(efdata.Result);
}
}
catch (Exception e)
{
result = new DSResult<VMO>(DSResultType.Otherfailure, e);
}
return result;
});
}Cheers,
==========================================================================================

TIA,

Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1415

  Chris's Avatar Chris Offline Posts: 1179
Hi Phil,

Just remove the ";" from the end of the first "return" line (3rd line of those you posted). All that code that follows the return statement is an anonymous method and we have decided that anonymous method's lines should not be separated with ";" (otherwise you would have to add a semicolon at the end of _every_ line).

Also remove the final "RETURN" (it's redundant, that anonymous method is what is already is being returned by the GetDataAsync method) and it should compile fine.

And yes, providing the source code was extremely helpful in testing my suggestions and making sure I am not saying something stupid :)

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1416

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Thanks Chris,

Seems to compile fine and give no red lines, wavy or straight ;-0)

I will be back with another small syntax issue or two in the morning.

But what you have just put straight for me I can also apply to a few more methods and new class or two, so BIG thanks !

Cheers,
Phil.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1417

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Hi again Chris,

Here is another code syntax snippet I will needs in the morning - original C# first :-



And now my not very clever attempt at X# :-



And finally some of my code for you to copy and paste :-

============================================================
public interface IGenericDataSource<VMO>
GetData(dataid as int) as DSResult<VMO>
FetchDataList() as DSResult<List<VMO>>
UpdateDB(DataAction edittype, VMO dataobject) as DSResult<VMO>

GetDataAsync(dataid as int) as Task<DSResult<VMO>>
FetchDataListAsync() as Task<DSResult<List<VMO>>>
UpdateDBAsync(edittype as DataAction, dataobject as VMO) as Task<DSResult<VMO>>
end interfaceCheers,
========================================================================================

Once again, if I get an answer to this I will be able to apply it in many places ;-)

Best regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1418

  Chris's Avatar Chris Offline Posts: 1179
Hi Phil,

You just need to put the METHOD keyword before each method name in the interface definition. Also adjust the parameters of the UpdateDB() method (they currently use the c# syntax in you sample) and it should compile fine.

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1419

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
Chris,

thanks, done, and now things are looking OK.

May be back to you again soon ;-0)

Fingers crossed,
Phil.
Wales, UK.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1420

  robert's Avatar robert Offline Posts: 1001
Phil
New()

is just an indication that the type has a public constructor without parameters.
We copied the C# term because we were not very creative at that time.

We should have probably changed it to
New{}
in stead but I am sure that people would have more problems then converting C# code to X#. It is already complicated enough at this moment, especially for generic classes.

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?

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1421

  wriedmann's Avatar wriedmann Away Posts: 1573
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1422

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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 :-



And here is your coded slightly amended :-



And here is my set-up in the Initialise method :-



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 :-



And now the issue that I found when setting up a string example test :-



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 :-



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.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1423

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1424

  NickFriend's Avatar NickFriend Offline Posts: 193
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!

Please Log in or Create an account to join the conversation.

Last edit: by NickFriend.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1425

  NickFriend's Avatar NickFriend Offline Posts: 193
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

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1426

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1427

  NickFriend's Avatar NickFriend Offline Posts: 193
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

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1428

  robert's Avatar robert Offline Posts: 1001
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1429

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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 :-





Regards to all,
Phil.
Wales, UK.
Attachments:

Please Log in or Create an account to join the conversation.

HELP please - syntax for new class ...... >> 2 years 3 months ago #1430

  wriedmann's Avatar wriedmann Away Posts: 1573
Hi Phil,

these days I like the short property syntax much more:
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:
property Items as T[] auto

Fortunately X# gives us a lot of possibilities to write code....

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2