Forum: Mikrocontroller und Digitale Elektronik C# Verständnisfrage


von Mario (Gast)


Lesenswert?

Hallo,

bin keine C# Experte und ich habe folgenden Code vor mir und verstehe 
den zweifach parametrisierten Kontruktor nicht.
Um diesen zu nutzen was muss ich da übergeben und welchen Zweck hat das 
ganze?

public class Communication : IDisposable
    {
        private SerialPort sp;
        public delegate void N_Line(String s);
        private N_Line n_Line;

  //Contructor
        public Communication(String comport, N_Line n_LineDelegate)
            : this(comport)
        {
            n_Line += n_LineDelegate;
        }

        public Communication(String comport)
        {
            sp = new SerialPort(comport);
        }

von Peter II (Gast)


Lesenswert?

Mario schrieb:
> Um diesen zu nutzen was muss ich da übergeben und welchen Zweck hat das
> ganze?

ist doch offensichtlich. Um das Objekt zu verwenden muss man entweder 
einen Comport übergeben oder Comport + LineDelegate.

von Adam P. (adamap)


Lesenswert?

Der erste Konstruktor
1
public Communication(String comport, N_Line n_LineDelegate)
2
       : this(comport)
ruft über "this" den zweiten Konstruktor auf.

von Mario (Gast)


Lesenswert?

Das mit dem Comport ist mir klar.
Aber was ist mit dem Delegaten?
Hast du mir da ein Beispiel wie der auszusehen hat?

von Peter II (Gast)


Lesenswert?

Mario schrieb:
> Aber was ist mit dem Delegaten?

das ist vergleichbar mit Funktionszeigern in C.

https://msdn.microsoft.com/de-de/library/17sde2xt(v=vs.110).aspx

von Sven W. (Gast)


Lesenswert?

Hallo Mario,
der Konstruktor erwartet eine Funktion mit der Signatur, die zum 
Delegate passt. Hier mal ein Beispiel.
1
    class Program
2
    {
3
        static void Main(string[] args)
4
        {
5
            Communication com = new Communication("COM1", RufMichAn);
6
            com.RufMalAn("Hallo...");
7
8
            Console.WriteLine("Mach mal Pause...");
9
            Console.ReadKey();
10
        }
11
12
        static void RufMichAn(string s)
13
        {
14
            Console.WriteLine(s);
15
        }
16
    }
17
18
    public class Communication
19
    {
20
        public delegate void N_Line(String s);
21
22
        private SerialPort sp;
23
        private N_Line     n_Line;
24
25
        public Communication(String comport, N_Line n_LineDelegate) : this(comport)
26
        {
27
            n_Line += n_LineDelegate;
28
        }
29
30
        public Communication(String comport)
31
        {
32
            sp = new SerialPort(comport);
33
        }
34
35
        public void RufMalAn(string s)
36
        {
37
            n_Line(s);
38
        }
39
    }
40
41
    public class SerialPort
42
    {
43
        public SerialPort(String comport)
44
        {
45
46
        }
47
    }

MfG Sven

von Can-Tak (Gast)


Lesenswert?

1
class Foo
2
{
3
  public void Bar()
4
  {
5
    var comA = new Communication(@"\\.\COM42", blub);
6
    var comB = new Communication(@"\\.\COM99", bla);
7
  }
8
9
  private void blub(string s)
10
  {
11
    // ...
12
  }
13
14
  private void bla(string s) => Console.WriteLine(s);
15
}

von Mario (Gast)


Lesenswert?

Jetzt hab ich es verstanden!
Vielen Dank!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.