fbpx
Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1

TOPIC: Tuples - Getting more for your money !?

Tuples - Getting more for your money !? 2 years 3 months ago #1475

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

In following up my Generics research work at the moment, I came across 'Tuples'.

Yes, apparently 'Tuples' are seen as the new sexy thing in C#, for a lot of folks that is.

Now that we have in our xBase armoury all that C# con do, I thought I would digress from my Generics for a moment, and have a look at the syntax required within X# for Tuple use.

It seems like Tuples (structures) can be used simply when we need to return from a method (for example) more than one item of data - more than one value, object or collections.

Have any of you started to use Tuples ? And if so have you any nice examples you can share with the rest of the X# Forum ?

Here below are some images of Tuples - details and working code in use :-



Yes, 'Items' are what are within Tuples - Item1, Items2, ........ etc.

Now a little test of my own making :-



After seeing the definition above, lets now see some testing code using the structure :-





The notes in MSDN show how many variations of Tuples that we can have :-



And here is some detail of just one of those, it is that for three items :-



Can anyone take us further with Tuples and their use - and - can any one warn us about "over-use", you know, the sort of things we all do when finding something new.

I think the phrase is something along the lines of "... when what you have in your hand is a hammer, everything looks like a nail ..."

Remember Interfaces ??? They got a bit 'done to death' as I recall ;-0)

Any contributions - good, bad, verbal, visual etc., etc.. ???

Cheers, and Best regards,
Phil.
Wales, UK.

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

Tuples - Getting more for your money !? 2 years 3 months ago #1480

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

yes, I had come across tuples some time ago, but never used them. They can be useful if you need more values as return value and don't like to define an own class for it. I see several uses for them....
In VO, I had used an array without all the advantages of strong typing.
In X#, for passing back and for multiple values, I use a Dictionary<string,object>, but of course I loose strong typing of the members, but can name them.

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.

Tuples - Getting more for your money !? 2 years 3 months ago #1484

  lumberjack's Avatar lumberjack Offline Posts: 465
Wolfgang,

Are you using Dictionary<string, object> to use a string property inside the object to access it?

There is a class for that KeyedCollection<K, V>
CLASS MyKeyCollection INHERIT KeyedCollecion<STRING, MyClass>
  METHOD GetKeyForItem(o AS MyClass) AS STRING
  RETURN o:Name
END CLASS
That is all code you have to write to use it.

Have also done some enhancement to it to work on certain classes to allow for using different properties in a class to build it on depending the requirement.
CLASS MyKeyCollection INHERIT KeyedCollecion<STRING, MyClass>
  EXPORT Key AS STRING
  CONSTRUCTOR(k AS STRING)
    SUPER()
    Key := k
  RETURN
  PROTECTED METHOD GetKeyForItem(o AS MyClass) AS STRING
  RETURN o[Key]
END CLASS
CLASS MyClass
  PROTECT kvp AS KeyValuePair<STRING, STRING>
  PROPERTY SELF[k AS STRING] AS STRING GET SELF[k] SET SELF[k] := VALUE
END CLASS
The above a bit over simplified but is really powerful.

Johan
______________________
Johan Nel
George, South Africa

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

Tuples - Getting more for your money !? 2 years 3 months ago #1486

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

no, my parameters can have every type of value, therefore I have typed the dictionary as <string,object>.

Basically I'm using this for my messaging between viewmodels or views.

My message class is defined as
class MessengerMessage implements IMessengerMessage
  protect _cMessage as string
  protect _oSender as IMessengerClient
  protect _oReceiver as IMessengerClient
  protect _oData as object
  protect _oParameters as Dictionary<string,object>

and have the possibility to access the parameter values directly:
public method AddParameter( cName as string, oValue as object ) as void 
	
if _oParameters == null
  _oParameters := Dictionary<string,object>{}
endif
if _oParameters:ContainsKey( cName )
  _oParameters[cName] := oValue
else
  _oParameters:Add( cName, oValue )
endif
	
return
	
public virtual method GetParameter( cName as string ) as object
local oValue as object
	
if _oParameters:ContainsKey( cName )
  oValue := _oParameters[cName]
else
  oValue := null
endif
	
return oValue

so the internal implementation of the various parameters in the message class is hidden to the user.
Of course, the GetParameter() method could be implemented using generics....

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.

Tuples - Getting more for your money !? 2 years 3 months ago #1487

  lumberjack's Avatar lumberjack Offline Posts: 465
Hi Wolfgang,
I use a very similar approach to you. For your specific example I don't have a Get/PutParameter() method but implement it as follows as a property, less code to type... o["PropertyName"]
PROPERTY SELF[name AS STRING] AS OBJECT
  GET
    // your GetParameter code here
  END  GET
  SET
    // Your PutParameter code here
  END SET
END PROPERTY

Johan
______________________
Johan Nel
George, South Africa

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

Tuples - Getting more for your money !? 2 years 3 months ago #1488

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

thanks! This was again a demonstration that I have a LOT to learn!

Your solution seems much better to my that mine... Maybe I change my interface and implementation - there is not so much code to change.... And thanks to interfaces and strong typing the compiler gives me the places where I have to change my code.

Wolfgang

P.S. maybe I'm too fixed at my VO programming style <g>
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.

Tuples - Getting more for your money !? 2 years 3 months ago #1493

  lumberjack's Avatar lumberjack Offline Posts: 465
Wolfgang,
You welcome.
What I actually do is adding my "late bound" parameters as part of the constructor. Then if I get/set the parameter by accident incorrectly by typo it will throw an error in the GET / SET, parameter not found.

Nothing wrong with the VO thinking, I still have a late bound assembly in .net that use the NoIVarGet()/NoIVarPut()/NoMethod() methods of VO. Have in that a wrapper class around macro compiled codeblocks so that I can do
{|o| o:Par1 + o:Par2 * o:Par3 } // Where o is the codeblock itself.

Johan
______________________
Johan Nel
George, South Africa

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

Tuples - Getting more for your money !? 2 years 2 months ago #1505

  robert's Avatar robert Offline Posts: 1001
Phil,

C# 7 has added Tuples in a lot of other places as well. You can specify that a method returns a tuple of strings like this:
(string, string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, middle, last); // tuple literal
}
and the calling code
var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item3}.");

the following code gives the tuple elements a name
(string first, string middle, string last) LookupName(long id) // tuple elements have names
And you can use them by their name
var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");

Nikos is in the process of merging our codebase with the C# 7 Roslyn codebase. That will allow us to add features like this to X# as well.
"All" we have to do is define a proper syntax and glue our parser front end to the Roslyn backend.

Maybe something like this
FUNCTION LookupName(id AS LONG) AS (STRING,STRING,STRING)

or
FUNCTION LookupName(id AS LONG) AS ( First AS STRING, Middle AS STRING, Last AS STRING)

That way tuples are becoming much like Anonymous classes.

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.

Tuples - Getting more for your money !? 2 years 2 months ago #1506

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

Very interesting - thanks for the heads up.

I like (and prefer) the second syntax as it removes the need for the coder to get the position of the data/variable correct.

One thing I did not like CSV stuff for was the weak link between place and meaning.

And that is why we all like Classes - the data position is taken out of the equation.

Just now struggling with Interfaces and Generics to make my own Data Access system. hard the first time you get your head around this ;-0((

Cheers,
Phil.

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

  • Page:
  • 1