Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Brauche Nachhilfe in Case (https://www.delphipraxis.net/294-brauche-nachhilfe-case.html)

Daniel B 29. Jun 2002 12:53


Brauche Nachhilfe in Case
 
Hi,

ich habe 3-4 solcher abfragen/änderungen. Sowas lässt sich doch bestimmt mit case einfacher lösen. Ich verstehe es einfach nicht. Vielleicht kann mir jemand helfen.
Vielen Dank.


Code:
  if MainFormMC2004.ComPort.Port = 'COM1' then
  begin
    ComboBoxPort.ItemIndex:=0;
  end
  else if MainFormMC2004.ComPort.Port = 'COM2' then
  begin
    ComboBoxPort.ItemIndex:=1;
  end
  else if MainFormMC2004.ComPort.Port = 'COM3' then
  begin
    ComboBoxPort.ItemIndex:=2;
  end
  else if MainFormMC2004.ComPort.Port = 'COM4' then
  begin
    ComboBoxPort.ItemIndex:=3;
  end
  else if MainFormMC2004.ComPort.Port = 'COM5' then
  begin
    ComboBoxPort.ItemIndex:=4;
  end
  else if MainFormMC2004.ComPort.Port = 'COM6' then
  begin
    ComboBoxPort.ItemIndex:=5;
  end
  else if MainFormMC2004.ComPort.Port = 'COM7' then
  begin
    ComboBoxPort.ItemIndex:=6;
  end
  else if MainFormMC2004.ComPort.Port = 'COM8' then
  begin
    ComboBoxPort.ItemIndex:=7;
  end;

MrSpock 29. Jun 2002 13:01

Hallo Daniel,

bad news :cry: :

case benötigt einen Selector, der ein ordinaler Typ sein muss, STRINGs werden nict unterstützt.

Hättest du einen eigenen Aufzählungstyp:

Code:
type
   tComPorts=(COM1, COM2, COM3);
könntest du case verwenden.

Daniel B 29. Jun 2002 13:03

Ich hab keine Aufzählung. Naja, trotzdem Danke.

MrSpock 29. Jun 2002 13:07

Hallo Daniel,

als Nachtrag:

du könntest entweder eine Funktion schreiben, die den Index zurückgibt und dann diese bei der Zuweisung aufrufen:

Code:
ComPortBox.ItemIndex := GetIndex(MainFormMC2004.ComPort.Port);
wobei GetIndex im OPrinzip wie in deinem Code definiert wird. Eventuell ließe sich auch die Tag Eigenschaft von ComPort benutzen. Wenn es dir gelänge das Tag immer mit dem Inhalt von "Port" zu synchronisieren. Dann wäre deine Zuweisung:

Code:
ComPortBox.ItemIndex := MainFormMC2004.ComPort.Tag
Leider kenne ich die ComPort Komponente nicht und kann deshalb keine tieferen Details zeigen.

sakura 29. Jun 2002 13:12

Im diesem speziellen Fall könntest Du folgendes tun
Code:
case IntToStrDef(Copy(MainFormMC2004.ComPort.Port, 3, 2), -1) of
 -1:; //Fehler
  1: Port 1
  2: Port 2
  3: Port 3
  ...
end;

Christian Seehase 29. Jun 2002 13:25

Moin Daniel B,

oder so:

Code:
ComboBoxPort.ItemIndex := StrToInt(AnsiLastChar(MainFormMC2004.ComPort.Port))-1;

sakura 29. Jun 2002 13:31

Zitat:

Zitat von Christian Seehase
Moin Daniel B,

oder so:

Code:
ComboBoxPort.ItemIndex := StrToInt(AnsiLastChar(MainFormMC2004.ComPort.Port))-1;

Hätte ich wirklich dahin geschaut, was Daniel erreichen wollte, hätte ich vielleicht auch Deine Idee gebracht - aber der Schrei nach case hat mich geblendet. ohohoh...

Daniel B 29. Jun 2002 13:43

Zitat:

Zitat von Christian Seehase
Code:
ComboBoxPort.ItemIndex := StrToInt(AnsiLastChar(MainFormMC2004.ComPort.Port))-1;

Hmm... es scheint zu gehen, aber verstehen tue ich es nicht wirklich.
Dieses AnsiLastChar macht mir zuschaffen.

Call AnsiLastChar to obtain locate the last character in a string. This function supports multi-byte character sets (MBCS). :?: :roll: :?:

RomanK 29. Jun 2002 13:47

AnsiLastChar gibt die Letzte stelle deines Strings aus. Also hier: com1 also 1 und dann mit StrtoInt nach interger und noch -1 und schon kann man direkt zuweisen.

sakura 29. Jun 2002 13:51

Zitat:

Zitat von FuckRacism
AnsiLastChar gibt die Letzte stelle deines Strings aus. Also hier: com1 also 1 und dann mit StrtoInt nach interger und noch -1 und schon kann man direkt zuweisen.
Vergleichbar

Oops, das ist falsch. AnsiLastChar gibt das letzte Zeichen eines Strings als Char bzw. WideChar (MCBS) zurück. Erst die Funktion StrToInt erledigt die Umwandlung.


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:26 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz