Autor Beitrag
braincom654
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 18.07.12 15:43 
Hallo Community,

ich habe folgendes Problem:

Zwar habe ich eine Klasse geschrieben:
ausblenden volle Höhe C#-Quelltext
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:
class Zeile
    {
        #region variablen
        public string protokoll;
        public string status;
        public IPEndPoint localEndPoint;
        public IPEndPoint remoteEndPoint;
        public string countryCode;
        public string countrFullName;
        public string ip;
        public string oldStatus;
        public int runde;

        public string date;

        char[] chars = new char[] { 'Q''W''E''R''T''Z''U''I''O''P''Ü''A''S''D''F''G''H''J''K''L''Ö''Y''X''C''V''B''N''M' };

        #endregion

        #region Constractor(s)
        public Zeile(string protocol, string statu, IPEndPoint local, IPEndPoint remote)
        {
            this.protokoll = protocol;
            this.localEndPoint = local;
            this.remoteEndPoint = remote;
            this.status = statu;
            if (statu.Contains("™"))
            {
                this.status = statu.Replace("™""Ö");
            }
        }
        public Zeile(IPAddress ip, string location, string date)
        {
            this.date = date;
            this.ip = ip.ToString();
            //this.countrFullName = location[0];
            this.countryCode = location;//[1];
        }
        #endregion
    }

Doch in meinem Programm wenn ich sie benutze tritt etwas seltsames auf, der Code ist in einer foreach welche eine List voller Zeile durchgeht:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
Zeile test1 = item;
test1.oldStatus = "old";

Zeile test2 = item;
test2.oldStatus = "new";

In diesem Beispiel hat dann test1.oldstatus auch den wert "new" und sogar das item bekommt den wert! Ich verstehe nicht warum item.oldstatus sollte da noch empty sein und test1.oldstatus sollte "old" sein.

Bitte um Hilfe, ich denke ist irgendwo nur ein Denkfehler^^.

Danke schonmal ;)

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Moderiert von user profile iconTh69: Titel geändert.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 18.07.12 15:53 
Zitat:
Zeile test1 = item;


Wenn du Klasseninstanzen einer anderen Variablen zuweist wird keine Kopie erzeugt sondern du erstellst nur eine weitere Referenz auf die gleiche Instanz. In deinem Beispiel schauen die test1, test2 und item Variablen also auf die gleiche Klasseninstanz und damit sieht du logischerweise immer die gleichen Daten.

Für diesen Beitrag haben gedankt: braincom654
braincom654 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 18.07.12 16:07 
Moderiert von user profile iconNarses: Komplett-Zitat des letzten Beitrags entfernt.

Ah okay, wusste ich bisher noch gar nicht. Vielen Danke.

Das heißt
ausblenden C#-Quelltext
1:
Zeile test1 = new Zeile(item);					

würde das problem schon lösen ;D

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 18.07.12 16:32 
Zitat:
Das heißt

Zeile test1 = new Zeile(item);

würde das problem schon lösen


Wenn du denn einen entsprechenden Constructor geschrieben hast der die Daten kopiert ja. Von allein nein.
braincom654 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 18.07.12 16:41 
Jap habe ich schnell geschrieben, funktioniert jetzt einwandfrei.

Vielen dank nochmals Ralf ;)
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Mi 18.07.12 17:11 
Leite doch von dem Interface ICloneable ab.
Damit implementierst du dann die Methode Clone. Die gibt zwar ein object zurück, aber damit kommt man klar.
Das ist, denke ich, sinnvoller, als einen Konstruktor zu schaffen, der genau das tut.