-
MY PROJECTS
-
Recent Posts
- SpringBoot.Making our first starter and autoconfiguration
- Spring. Creating main annotation to start business logic
- Spring.Reading from properties file
- Spring.How to define spring version inside springBoot ?
- SpringBoot App inside Docker
- Kafka.FirstExperience
- Docker.MySql and Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0
- Gradle.Tips
- Spring.AppConfig
- Leetcode.Best-time-to-buy-and-sell-stock
- LeetCode.ClimbStairs.Fibbonacci
- Leetcode.Roman-to-integer
- LeetCode.Palindrome-number
- Java.DesignPatterns.Lightweight
- Java.DesignPatterns.Proxy
- Java.DesignPatterns.Facade
- Java.DesignPatterns.Composite
- Java.DesignPatterns.Bridge
- Java. Strange OOP thing
- Java.Classpath
Categories
- Aptana
- Azure
- C#
- DataSnap
- DBExpress
- Delphi
- Delphi и сети
- Delphi. Язык программирования
- ExtJS
- FastReport
- FireDAC
- FireMonkey
- GIT
- ICS
- IDE
- IIS
- Indy
- InnoSetup
- javascript
- jQuery
- JSON
- LiveBindings
- MSHTML
- MySQL
- PHP
- REST
- Ribbons
- SMS
- SQL инструкции
- SVN
- TRichView
- UniGui
- WebBroker
- WinAPI
- Windows
- Алгоритмы
- Без рубрики
- Деревья
- Ищу ответ
- Компонентостроение
- Мои компоненты
- Начальный уровень
- Обработка исключений
- Парсинг
- Потоки(Threads)
- Регулярные выражения
- Тестирование приложений
Author Archives: Stanislav_Panteleev
C#. Delegates. Events. Good Example Bad Example
Good example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Events2 { class Program { static void Main(string[] args) { Car c = new Car(); Car.SomeDelegate d = new Car.SomeDelegate(SomeEvent1Handler); c.SomeEvent1 += d; // added EventHandler c.SomeEvent1 -= d; // removed EventHandler Car.SomeDelegate d2 = new Car.SomeDelegate(SomeEvent2Handler); c.SomeEvent2 += d2; // added EventHandler // c.SomeEvent2 -= d2; // removed EventHandler //c.SomeEvent1 += new Car.SomeDelegate(SomeEvent1Handler); // another way to assign Event handler //c.SomeEvent2 += new Car.SomeDelegate(SomeEvent2Handler); c.SomeMethod(); Console.ReadLine(); } public static void SomeEvent1Handler(string msg) { Console.WriteLine("SomeEvent1Handler "+ msg); } public static void SomeEvent2Handler(string msg) { Console.WriteLine("SomeEvent2Handler "+ msg); } } public class Car { public delegate void SomeDelegate(string msg); public event SomeDelegate SomeEvent1; public event SomeDelegate SomeEvent2; public void SomeMethod() { //some code if (SomeEvent1 != null) SomeEvent1("someMessage"); // fire Event Handler //some code if (SomeEvent2 != null) SomeEvent2("someMessage2"); // fire Event Handler } } } |
Bad Example not Ok variant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EventsBadExample { class Program { static void Main(string[] args) { Car c = new Car(); c.listOfHandlers = new Car.SomeDelegate(SomeHandler); // assigned c.listOfHandlers = new Car.SomeDelegate(SomeHandler2); // re assigned, bad !!! c.Accelerate(10); c.listOfHandlers.Invoke("123"); // <<< direct call, bad !!! Console.ReadLine(); } static void SomeHandler(string message) { Console.WriteLine(message); } static void SomeHandler2(string message) { Console.WriteLine(message+ " 2"); } } public class Car { public delegate void SomeDelegate(string message); public SomeDelegate listOfHandlers; // <<< must be private !!! public void Accelerate(int delta) { if (listOfHandlers != null) listOfHandlers("some message"); } } } |
Posted in C#
Comments Off on C#. Delegates. Events. Good Example Bad Example
C#. Delegate generic emulation
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EmulationOfGenerics { public delegate void SomeDelegate(object arg); class Program { static void Main(string[] args) { SomeDelegate d = new SomeDelegate(MyTarget); d("some string"); SomeDelegate d2 = MyTarget; d2(9); Console.ReadLine(); } static void MyTarget(object arg) { if (arg is int) { int i = (int)arg; Console.WriteLine("++arg is: {0} ", ++i); } if (arg is string) { string s = (arg as string); Console.WriteLine("arg is in UpperCase: {0} ", s.ToUpper()); } } } } |
Posted in C#
Comments Off on C#. Delegate generic emulation
C#. Generic delegates
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GenericDelegates { public delegate void SomeGenericDelegate<T>(T arg); class Program { static void Main(string[] args) { SomeGenericDelegate<string> strTarget = new SomeGenericDelegate<string>(StringTarget); strTarget("someText"); SomeGenericDelegate<int> intTarget = new SomeGenericDelegate<int>(IntTarget); intTarget(9); Console.ReadLine(); } static void StringTarget(string arg) { Console.WriteLine(arg.ToUpper());} static void IntTarget(int arg) { Console.WriteLine("++arg is {0}:", ++arg); } } } |
Posted in C#
Comments Off on C#. Generic delegates
C#. Delegate Covariance
123 not clear what the sense… of getting delegate objs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DelegateCovariance { class Program { public delegate Car ObtainCarDelegate(); public delegate SportsCar ObtainSportsCarDelegate(); public delegate Car ObtainVehicleDelegate(); public static Car GetBasicCar() { return new Car("123", 100, 55);} public static SportsCar GetSportsCar(){return new SportsCar("123", 100, 55);} static void Main(string[] args) { ObtainCarDelegate targetA = new ObtainCarDelegate(GetBasicCar); Car c = targetA(); Console.WriteLine("Obtained a {0} ",c); // Console.ReadLine(); ObtainSportsCarDelegate targetB = new ObtainSportsCarDelegate(GetSportsCar); SportsCar sc = targetB(); Console.WriteLine("Obtained a {0} ", sc); // Console.ReadLine(); //covariance here - it uses inheritance of Car and Sports Car ObtainVehicleDelegate targetA1 = new ObtainVehicleDelegate(GetBasicCar); Car c1 = targetA1(); Console.WriteLine("Obtained a {0} ", c1); ObtainVehicleDelegate targetB1 = new ObtainVehicleDelegate(GetSportsCar); Car c2 = targetB1(); Console.WriteLine("Obtained a {0} ", c2); Console.ReadLine(); } } public class Car { public int ID { get; set; } public int Speed { get; set; } public string Name { get; set; } public Car(string name, int speed, int id) { Name = name; Speed = speed; ID = id; } public Car() { } } public class SportsCar : Car { public SportsCar() { } public SportsCar(string name, int speed, int id) : base(name, speed, id) { } } class SomeClass { public delegate Car ObtainCarDelegate(); private ObtainCarDelegate CarlistOfHandlers; public void RegisterHandlerCarDelegate(ObtainCarDelegate method) { CarlistOfHandlers += method; } // delegate block public delegate void SomeDelegate(string Amessage); private SomeDelegate listOfHandlers; public void RegisterHandler(SomeDelegate method) { listOfHandlers += method; } public void UnRegisterHandler(SomeDelegate method) { listOfHandlers -= method; } //do something in class public void SomeMethod() { //some code if (listOfHandlers != null) { listOfHandlers("hello from delegate method"); } //some code } public void SomeCarMethod() { //some code if (listOfHandlers != null) { } //some code } } } |
Posted in C#
Comments Off on C#. Delegate Covariance
C#. Delegates. Very simple example
Delegates similar to NotifyEvents or procedures of object in Delphi, We can register or unregister handlers in calling code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegates { class Program { static void Main(string[] args) { SomeClass sc = new SomeClass(); sc.RegisterHandler(new SomeClass.SomeDelegate(DelegateHandler)); sc.RegisterHandler(new SomeClass.SomeDelegate(DelegateHandler2)); sc.UnRegisterHandler(new SomeClass.SomeDelegate(DelegateHandler2)); sc.SomeMethod(); // will fire list of handlers Console.ReadLine(); } public static void DelegateHandler(string Amessage) { Console.WriteLine("Delegate handler message="+Amessage); } public static void DelegateHandler2(string Amessage) { Console.WriteLine("Another handler message = "+Amessage); } } class SomeClass { // delegate block public delegate void SomeDelegate(string Amessage); private SomeDelegate listOfHandlers; public void RegisterHandler(SomeDelegate method) {listOfHandlers += method;} public void UnRegisterHandler(SomeDelegate method) { listOfHandlers -= method; } //do something in class public void SomeMethod() { //some code if (listOfHandlers != null) { listOfHandlers("hello from delegate method"); } //some code } } } |
Posted in C#
Comments Off on C#. Delegates. Very simple example
C#. SimpleDelegate
Delegate execute code in additional thread in Sync or Async modes, main methods Invoke() – sync mode BeginInvoke() – async mode EndInvoke() async mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleDelegate { class Program { static void Main(string[] args) { SimpleMath sm = new SimpleMath(); BinaryOp b = new BinaryOp(sm.Add); Console.WriteLine("direct delegate call "+b(10, 10)); // or Console.WriteLine("invoke call "+b.Invoke(10, 10)); DisplayDelegateInfo(b); Console.ReadLine(); } static void DisplayDelegateInfo(Delegate delObj) { foreach (Delegate d in delObj.GetInvocationList()) { Console.WriteLine("Method Name: {0}",d.Method); // method name Console.WriteLine("Type Name: {0}", d.Target); // type name } } } public delegate int BinaryOp(int x, int y); public class SimpleMath { public int Add(int x, int y) { return x + y; } public int Subtract(int x, int y) { return x - y; } } } |
Posted in C#
Comments Off on C#. SimpleDelegate
C#. GenericsWhereKeyWord
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GenericsWhereKeyWord { class Program { static void Main(string[] args) { } } public struct Point<T> where T: new() // where T:struct,where T:class, where T:BaseClassName, where T:InterfaceName { private T x; private T y; public Point(T X, T Y) { x = X; y = Y; } public T X { get { return x; } set { x = value; } } public T Y { get { return y; } set { y = value; } } public override string ToString() { //return string.Format({"[{0},{1}]",x,y }); return string.Format("{0},{1}", x, y); } public void Reset() { x = default(T); // numbers will be set to 0, objects will be set to null y = default(T); // } } } |
Posted in C#
Comments Off on C#. GenericsWhereKeyWord
C#. Custom generic Structures and classes
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomGenericStruct { class Program { static void Main(string[] args) { } } public struct Point<T> { private T x; private T y; public Point(T X, T Y) { x = X; y = Y; } public T X { get { return x; } set { x = value; }} public T Y { get { return y; } set { y = value; } } public override string ToString() { //return string.Format({"[{0},{1}]",x,y }); return string.Format("{0},{1}", x, y); } public void Reset() { x = default(T); // numbers will be set to 0, objects will be set to null y = default(T); // } } } |
Posted in C#
Comments Off on C#. Custom generic Structures and classes
C#. Custom generic methods
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomGenerics { class Program { static void Main(string[] args) { int a = 1; int b = 2; Swap<int>(ref a,ref b); Console.WriteLine(a+""+b); string s1 = "1"; string s2 = "2"; Swap<string>(ref s1, ref s2); Console.WriteLine(s1 + "" + s2); Console.ReadLine(); } static void Swap<T>(ref T a, ref T b) { T temp; temp = a; a = b; b = temp; } } } |
Posted in C#
Comments Off on C#. Custom generic methods
C#. SortedSet<>
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortedSetGenerics { class Program { static void Main(string[] args) { UseSortedSet(); Console.ReadLine(); } static void UseSortedSet() { SortedSet<Person> peopleSortedSet = new SortedSet<Person>(new SortpeopleByAge()) { new Person { Age = 123, FirstName = "123", LastName = "123" }, new Person { Age = 1234, FirstName = "1234", LastName = "1234" }, new Person { Age = 12345, FirstName = "12345", LastName = "12345" }, }; foreach (Person p in peopleSortedSet) { Console.WriteLine(p); // elements sorted by age } } public class Person { public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } public override string ToString() { return ("Age="+Age); // return base.ToString(); } } class SortpeopleByAge : IComparer<Person> { public int Compare(Person x, Person y) { if (x.Age > y.Age) { return 1; } if (x.Age > y.Age) { return -1; } else return 0; //throw new NotImplementedException(); } } } } |
Posted in C#
Comments Off on C#. SortedSet<>