From f49e6a26c6efe1081718c40c53900849c38514d5 Mon Sep 17 00:00:00 2001 From: wienkop <uwe.wienkop@th-nuernberg.de> Date: Tue, 1 Jun 2021 11:15:50 +0200 Subject: [PATCH] 2021-06-01 Delegates&Events --- 10-1 DelegateVariable/Button.cs | 21 ++++++ 10-1 DelegateVariable/Program.cs | 29 +++++++- 10-1 GenerischeListeMitSuche/GenericList.cs | 8 +-- 10-3 WpfButton/10-3 WpfButton.csproj | 10 +++ 10-3 WpfButton/App.xaml | 9 +++ 10-3 WpfButton/App.xaml.cs | 17 +++++ 10-3 WpfButton/AssemblyInfo.cs | 10 +++ 10-3 WpfButton/MainWindow.xaml | 13 ++++ 10-3 WpfButton/MainWindow.xaml.cs | 33 +++++++++ 10-Ubg HashDict-Di/10-Ubg HashDict-Di.csproj | 9 +++ 10-Ubg HashDict-Di/HashDict.cs | 71 ++++++++++++++++++++ 10-Ubg HashDict-Di/KeyValuePaar.cs | 16 +++++ 10-Ubg HashDict-Di/Program.cs | 27 ++++++++ 10-Ubg HashDict-Mo/HashDict.cs | 21 +++--- 10-Ubg HashDict-Mo/Program.cs | 2 +- Prog2WienkopSS2021.sln | 16 ++++- 16 files changed, 294 insertions(+), 18 deletions(-) create mode 100644 10-1 DelegateVariable/Button.cs create mode 100644 10-3 WpfButton/10-3 WpfButton.csproj create mode 100644 10-3 WpfButton/App.xaml create mode 100644 10-3 WpfButton/App.xaml.cs create mode 100644 10-3 WpfButton/AssemblyInfo.cs create mode 100644 10-3 WpfButton/MainWindow.xaml create mode 100644 10-3 WpfButton/MainWindow.xaml.cs create mode 100644 10-Ubg HashDict-Di/10-Ubg HashDict-Di.csproj create mode 100644 10-Ubg HashDict-Di/HashDict.cs create mode 100644 10-Ubg HashDict-Di/KeyValuePaar.cs create mode 100644 10-Ubg HashDict-Di/Program.cs diff --git a/10-1 DelegateVariable/Button.cs b/10-1 DelegateVariable/Button.cs new file mode 100644 index 0000000..651686e --- /dev/null +++ b/10-1 DelegateVariable/Button.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _10_1_DelegateVariable +{ + delegate void MyFunction(string s); + class Button + { + public /*event*/ MyFunction Callback; + // event ~ Einschränkung der Zugriffsmöglichkeiten von + // außerhalb der Klasse auf nur noch += bzw. -= + + public void Print(string s) + { + Callback?.Invoke(s); + // print = null; ~ Nur innerhalb der Klasse erlaubt + } + } + +} diff --git a/10-1 DelegateVariable/Program.cs b/10-1 DelegateVariable/Program.cs index f5ae6ae..346f0c4 100644 --- a/10-1 DelegateVariable/Program.cs +++ b/10-1 DelegateVariable/Program.cs @@ -4,9 +4,36 @@ namespace _10_1_DelegateVariable { class Program { + static void AusgabeInGrossbuchstaben(string s) + { + Console.WriteLine(s.ToUpper()); + } + static void AusgabeInKleinbuchstaben(string s) + { + Console.WriteLine(s.ToLower()); + } static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Button t1 = new Button(); + Button t2 = new Button(); + + t1.Print("Erster Versuch"); + + t1.Callback += Console.WriteLine; + t1.Callback += AusgabeInGrossbuchstaben; + t1.Callback = AusgabeInKleinbuchstaben; + t1.Callback += delegate (string s) { Console.WriteLine($"*** {s} ***"); }; + t1.Callback += s => { Console.WriteLine($"--- {s} ---"); }; + //t1.print -= AusgabeInGrossbuchstaben; + //t1.print += t1.print; + + //t1.print -= s => { Console.WriteLine($"--- {s} ---"); }; + // Anonyme Methoden & Lambda-Ausdrücke können nicht aus der Liste entfernt werden! + + t1.Print("Hallo"); + + // t2.print += t1.print; Ebenfalls nicht erlaubt + //t2.Print("Welt"); } } } diff --git a/10-1 GenerischeListeMitSuche/GenericList.cs b/10-1 GenerischeListeMitSuche/GenericList.cs index 36181df..d73f64e 100644 --- a/10-1 GenerischeListeMitSuche/GenericList.cs +++ b/10-1 GenerischeListeMitSuche/GenericList.cs @@ -183,13 +183,13 @@ namespace _10_1_GenerischeListeMitSuche for (LItem item = first; item != null; item = item.next) yield return item.data; } - public delegate bool MeinFilter(T var); - //public delegate bool Predicate(T var); + //public delegate bool MeinFilter(T var); + //public delegate bool Predicate(T var); Bereits von Microsoft vordefiniert - public IEnumerable<T> Filter(Predicate<T> filter) + public IEnumerable<T> Filter(Predicate<T> filterFunktion) { for (LItem item = first; item != null; item = item.next) - if (filter(item.data)) // Filterfunktion + if (filterFunktion(item.data)) // Filterfunktion yield return item.data; } } diff --git a/10-3 WpfButton/10-3 WpfButton.csproj b/10-3 WpfButton/10-3 WpfButton.csproj new file mode 100644 index 0000000..dd4d853 --- /dev/null +++ b/10-3 WpfButton/10-3 WpfButton.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> + + <PropertyGroup> + <OutputType>WinExe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_3_WpfButton</RootNamespace> + <UseWPF>true</UseWPF> + </PropertyGroup> + +</Project> diff --git a/10-3 WpfButton/App.xaml b/10-3 WpfButton/App.xaml new file mode 100644 index 0000000..644e43e --- /dev/null +++ b/10-3 WpfButton/App.xaml @@ -0,0 +1,9 @@ +<Application x:Class="_10_3_WpfButton.App" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:local="clr-namespace:_10_3_WpfButton" + StartupUri="MainWindow.xaml"> + <Application.Resources> + + </Application.Resources> +</Application> diff --git a/10-3 WpfButton/App.xaml.cs b/10-3 WpfButton/App.xaml.cs new file mode 100644 index 0000000..096bf09 --- /dev/null +++ b/10-3 WpfButton/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace _10_3_WpfButton +{ + /// <summary> + /// Interaction logic for App.xaml + /// </summary> + public partial class App : Application + { + } +} diff --git a/10-3 WpfButton/AssemblyInfo.cs b/10-3 WpfButton/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/10-3 WpfButton/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/10-3 WpfButton/MainWindow.xaml b/10-3 WpfButton/MainWindow.xaml new file mode 100644 index 0000000..04922d8 --- /dev/null +++ b/10-3 WpfButton/MainWindow.xaml @@ -0,0 +1,13 @@ +<Window x:Class="_10_3_WpfButton.MainWindow" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:local="clr-namespace:_10_3_WpfButton" + mc:Ignorable="d" + Title="MainWindow" Height="450" Width="800"> + <Grid> + <Button Content="Mein Button" HorizontalAlignment="Left" Margin="117,91,0,0" VerticalAlignment="Top" Click="Button_Click"/> + + </Grid> +</Window> diff --git a/10-3 WpfButton/MainWindow.xaml.cs b/10-3 WpfButton/MainWindow.xaml.cs new file mode 100644 index 0000000..861b830 --- /dev/null +++ b/10-3 WpfButton/MainWindow.xaml.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace _10_3_WpfButton +{ + /// <summary> + /// Interaction logic for MainWindow.xaml + /// </summary> + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + + private void Button_Click(object sender, RoutedEventArgs e) + { + + } + } +} diff --git a/10-Ubg HashDict-Di/10-Ubg HashDict-Di.csproj b/10-Ubg HashDict-Di/10-Ubg HashDict-Di.csproj new file mode 100644 index 0000000..07a3eaa --- /dev/null +++ b/10-Ubg HashDict-Di/10-Ubg HashDict-Di.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_Ubg_HashDict_Di</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/10-Ubg HashDict-Di/HashDict.cs b/10-Ubg HashDict-Di/HashDict.cs new file mode 100644 index 0000000..8274546 --- /dev/null +++ b/10-Ubg HashDict-Di/HashDict.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _10_Ubg_HashDict_Di +{ + // Erstellen Sie eine Klasse HashDict, welches ebenfalls Schlüssel-/Wertpaare abspeichern kann. + // Allerdings sollen die Schlüssel-/Wertpaare nicht in einer Liste, sondern in einem + // hinreichend großen Feld abgelegt werden. + // Der Schlüssel soll direkt in einen Feldplatz umgerechnet werden (Hashfunktion), etwaige + // Konflikte sind geeignet zu behandeln. + // Für die Generierung eines Hashcodes gibt es bereits in Object eine Methode: + // int GetHashCode() + // Erstellen Sie + // - indexer-Zugriff (get;set) + // - GetEnumerator() + // - Contains-Methode + class HashDict<K,V> where K:class,IComparable<K> + { + K[] keys; + V[] values; + int length; + + public HashDict(int length=100) + { + this.length = length; + keys = new K[length]; + values = new V[length]; + } + + private int SearchInd(K key) + { + int ind = Math.Abs(key.GetHashCode()) % length; + while (keys[ind] != null && keys[ind].CompareTo(key) != 0) + ind = (ind + 1) % length; + return ind; + } + + public V this[K key] + { + get + { + int ind = SearchInd(key); + if (keys[ind] == null) + throw new Exception($"Der Schlüssel {key} wurde nicht gefunden"); + return values[ind]; + } + set + { + int ind = SearchInd(key); + keys[ind] = key; + values[ind] = value; + } + } + + public IEnumerator<KeyValuePaar<K,V>> GetEnumerator() + { + for (int i = 0; i < length; i++) + if (keys[i] != null) + yield return new KeyValuePaar<K,V>(keys[i], values[i]); + } + public override string ToString() + { + string res = ""; + for (int i = 0; i < length; i++) + if (keys[i] != null) + res += keys[i].ToString() + values[i].ToString() + "\n"; + return res; + } + } +} diff --git a/10-Ubg HashDict-Di/KeyValuePaar.cs b/10-Ubg HashDict-Di/KeyValuePaar.cs new file mode 100644 index 0000000..7827ef0 --- /dev/null +++ b/10-Ubg HashDict-Di/KeyValuePaar.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Text; + +namespace _10_Ubg_HashDict_Di +{ + // "Wienkop" | 1614 + class KeyValuePaar<K, V> + { + public K key; + public V val; + public KeyValuePaar(K key, V val) { this.key = key;this.val = val; } + public override string ToString() => $"{key}: {val}"; + } +} diff --git a/10-Ubg HashDict-Di/Program.cs b/10-Ubg HashDict-Di/Program.cs new file mode 100644 index 0000000..72eb46e --- /dev/null +++ b/10-Ubg HashDict-Di/Program.cs @@ -0,0 +1,27 @@ +using System; + +namespace _10_Ubg_HashDict_Di +{ + class Program + { + static void Main(string[] args) + { + HashDict<string, string> telbuch = new HashDict<string, string>(10); + + telbuch["Wienkop"] = "1614"; + telbuch["Otsa"] = "1855"; + telbuch["xyz"] = "3451"; + + Console.WriteLine(telbuch["Wienkop"]); + //Console.WriteLine(telbuch["zzz"]); + Console.WriteLine("-----------"); + + //foreach (var item in telbuch) + //{ + // Console.WriteLine($"Name: {item.key} -- TelNr: {item.val}"); + //} + + Console.WriteLine(telbuch); + } + } +} diff --git a/10-Ubg HashDict-Mo/HashDict.cs b/10-Ubg HashDict-Mo/HashDict.cs index aff565d..b439868 100644 --- a/10-Ubg HashDict-Mo/HashDict.cs +++ b/10-Ubg HashDict-Mo/HashDict.cs @@ -15,7 +15,7 @@ namespace _10_Ubg_HashDict_Mo // - indexer-Zugriff (get;set) // - GetEnumerator() // - Contains-Methode - class HashDict<K,V> where K:IComparable<K> + class HashDict<K,V> where K:class,IComparable<K> { K[] keys; V[] values; @@ -28,26 +28,27 @@ namespace _10_Ubg_HashDict_Mo this.length = length; } + private int SearchInd(K key) + { + int ind = Math.Abs(key.GetHashCode()) % length; + while (keys[ind] != null && keys[ind].CompareTo(key) != 0) + ind = (ind + 1) % length; + return ind; + } public V this[K key] { get { - int ind= Math.Abs(key.GetHashCode()) % length; - while (keys[ind] != null && keys[ind].CompareTo(key) != 0) - ind = (ind + 1) % length; - + int ind = SearchInd(key); if (keys[ind] == null) throw new Exception($"Der Schlüssel {key} wurde nicht gefunden"); return values[ind]; } set { - int ind = Math.Abs(key.GetHashCode()) % length; - while (keys[ind] != null && keys[ind].CompareTo(key) != 0) - ind = (ind + 1) % length; - + int ind = SearchInd(key); keys[ind] = key; - values[ind] = value; + values[ind] = value; } } public IEnumerator<KeyValuePaar<K, V>> GetEnumerator() diff --git a/10-Ubg HashDict-Mo/Program.cs b/10-Ubg HashDict-Mo/Program.cs index bae822c..3cee4e7 100644 --- a/10-Ubg HashDict-Mo/Program.cs +++ b/10-Ubg HashDict-Mo/Program.cs @@ -23,7 +23,7 @@ namespace _10_Ubg_HashDict_Mo foreach (var item in telbuch) { Console.WriteLine($"Name: {item.key} -- TelNr: {item.val}"); - } + } } } } diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index c77393c..146b443 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -77,9 +77,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-3 Intro Delegates", "09- EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-Ubg HashDict-Mo", "10-Ubg HashDict-Mo\10-Ubg HashDict-Mo.csproj", "{BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10-2 DelegateVariable", "10-1 DelegateVariable\10-2 DelegateVariable.csproj", "{675BC204-45BA-4E76-80D3-5899EF960624}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-2 DelegateVariable", "10-1 DelegateVariable\10-2 DelegateVariable.csproj", "{675BC204-45BA-4E76-80D3-5899EF960624}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10-1 GenerischeListeMitSuche", "10-1 GenerischeListeMitSuche\10-1 GenerischeListeMitSuche.csproj", "{7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-1 GenerischeListeMitSuche", "10-1 GenerischeListeMitSuche\10-1 GenerischeListeMitSuche.csproj", "{7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-Ubg HashDict-Di", "10-Ubg HashDict-Di\10-Ubg HashDict-Di.csproj", "{C903192E-44CA-42C3-A136-65E13026ECB1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10-3 WpfButton", "10-3 WpfButton\10-3 WpfButton.csproj", "{11636803-CB01-4A29-ADAE-F03F3F38ED70}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -243,6 +247,14 @@ Global {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Release|Any CPU.Build.0 = Release|Any CPU + {C903192E-44CA-42C3-A136-65E13026ECB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C903192E-44CA-42C3-A136-65E13026ECB1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C903192E-44CA-42C3-A136-65E13026ECB1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C903192E-44CA-42C3-A136-65E13026ECB1}.Release|Any CPU.Build.0 = Release|Any CPU + {11636803-CB01-4A29-ADAE-F03F3F38ED70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {11636803-CB01-4A29-ADAE-F03F3F38ED70}.Debug|Any CPU.Build.0 = Debug|Any CPU + {11636803-CB01-4A29-ADAE-F03F3F38ED70}.Release|Any CPU.ActiveCfg = Release|Any CPU + {11636803-CB01-4A29-ADAE-F03F3F38ED70}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab