X# Example Project for various DotNet functionality

This forum is meant for anything you would like to share with other visitors
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

X# Example Project for various DotNet functionality

Post by ic2 »

Hello Volkmar,
VR wrote:Some of the examples use features from the latest x# compiler. I'll add that info in to the readme.
As a quick fix, you can delete the folders, that contain files with errors.
Yes, now can I start the (remaining) various options by uncommenting them from the Program.prg. Very useful.

I should of course update my X# but I haven't used any of the additions not already present in Vulcan and I am always inclined to keep using something that works until I find some new option that I really welcome. That saves compiler errors in newer versions and DLL updates at clients, in the case of X#, and more than once septs back in other program's updates.

But maybe I find something useful in your samples, when reloading, which requires 2.8.

One quick question: I never used a Dictionary. Do you agree with me that you can do the same thing with a List but only slightly shorter?

Code: Select all

        List<Part> parts = new List<Part>();
        parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });

        int index parts.Find(x => x.PartId.Contains("1334")));
        ? parts[index].PartName

I saw I did once create a Github account and I've given your post 5 stars too.

Dick
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Example Project for various DotNet functionality

Post by wriedmann »

Hi Dick,
a list and a dictionary are two different things (ok, a dictionary is a somewhat specialized form of a list).
First of all, a dictionary is not simple a list, but has unique keys and attributed values.
The great thing is that you can access dictionary values using the key like an array index.
Please look here: https://www.tutorialsteacher.com/csharp ... dictionary
Of course, the value of a dictionary element can be every class, but beware that the keys are unique and not every character is accepted for the key value.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
VR
Posts: 102
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

X# Example Project for various DotNet functionality

Post by VR »

Hello Dick,

as Wolfgang already described, a dictionary is a specialized data structure. One key difference is, that searching in a list takes O(n) while searching in a dictionary takes O(1). That means, that searching in a list gets slower, when more elements are added to the list, while it stays "the same" for dictionaries.

Here you can find more info on list vs dictionary: http://net-informations.com/faq/general ... y-list.htm

Volkmar
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

X# Example Project for various DotNet functionality

Post by Chris »

To add to what Wolfgang and Volkmar said, for 10 or 20 items, it's probably better just using a List. But if you need a large amount of items and speed of search is important, then a Dictionary is EXTREMELY faster than a List. A SortedList is somewhere in between a List and a Dictionary regarding searching performance.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

X# Example Project for various DotNet functionality

Post by Karl-Heinz »

Hi Volkmar

very nice work !

maybe you could add to your EmumExamples class the possibilty to identify an Answer enum member by a string value.

Code: Select all

		// Get the corresponding Answer enum member from the string "NO"
		IF Enum.TryParse( "NO" , TRUE, OUT MyAnswer ) 
			WriteEnum(MyAnswer)
		ENDIF


btw. you have one more star click ;-)

regards
Karl-Heinz
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

X# Example Project for various DotNet functionality

Post by robert »

Volkmar,
May I add a suggestion:
If you change the code for the Invoicebuilder to return

Code: Select all

as IEnumerable<Invoice>

instead of List<Invoice>
and then remove the result variable
and instead of adding the Invoices to the list use

Code: Select all

yield return invoiceItem
and replace the last line of the method with

Code: Select all

yield exit
Then you can really show that Linq delays the creations of the elements in the collection until the last moment and you're not even creating a List.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

X# Example Project for various DotNet functionality

Post by ic2 »

Hello Volkmar, Chris, Wolfgang,

Ok, good to know, thanks. Conclusion: using a dictionary instead of a list makes coding slightly shorter but performance, with many elements, much higher.

If there would be no real advantages it is easier to stick to less options instead of using them all which will also take more time to look up how they are supposed to work. .Net is not very consistent in how to use functions/methods/classes; properties basically doing the same are called different throughout them and therefor as a programmer you will need to do more lookup work for not often used functions.

Dick
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

X# Example Project for various DotNet functionality

Post by FFF »

the other way round: if your "list" is LONG, Dictionary is a LOT faster...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
HansjoergP
Posts: 141
Joined: Mon Jul 25, 2016 3:58 pm
Location: Italy

X# Example Project for various DotNet functionality

Post by HansjoergP »

It is also a lot easier to get an element with the key and not to search for the index and than get the element.
User avatar
ArneOrtlinghaus
Posts: 412
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

X# Example Project for various DotNet functionality

Post by ArneOrtlinghaus »

I am also using the examples from my colleague Volkmar to adopt some new programming techniques - it is not so simple to leave over 20 years of programming behind.

I am also trying to use methods of the standard types like strings instead of using the VO functions. Of course nothing is gratis - when having index variables as in string.substring, counting starts from 0 and not from 1...
local i as int
local c := "xyz" as string
for i := 1 upto c.Length
cx := c.Substring(i-1, 1)
...
Arne
Post Reply