Delphi-PRAXiS
Seite 1 von 4  1 23     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Bedingt Prozedur aufrufen (https://www.delphipraxis.net/107213-bedingt-prozedur-aufrufen.html)

CRoSSi 24. Jan 2008 09:10


Bedingt Prozedur aufrufen
 
Hallo,
ich bin neu hier und habe mal ne frage..

weiß jemand wie man eine prozedur bedingt einbinden kann? Also wie man sie aufruft weiß ich, das ist nicht das Problem.
hatte mir das so ca vorgestellt: if var=1 then procedure usw.. hoffe mein Problem ist nachvollziehbar.

Danke schonmal für antworten
Christoph

DeddyH 24. Jan 2008 09:12

Re: Bedingt Prozedur aufrufen
 
Meinst Du so?
Delphi-Quellcode:
if var = 1 then
  Machwas
else
  MachwasAnderes;
P.S.: Willkommen in der DP :dp:

CRoSSi 24. Jan 2008 09:27

Re: Bedingt Prozedur aufrufen
 
ne das ist ja klar.. man muss die przedur doch definiern, sodass man die aufrufen kann.. und ich meien die definition dass man die irgendwie in ne if packt. Konkret würde ich des so meinen:

Delphi-Quellcode:
if var=1 then
   begin
   procedure PortOut(Port : Word; Data : Byte);stdcall; external 'io.dll';
   end;
Bekomme sonst ne Fehlermeldung wo der port Treiber nicht installiert ist.

Die Muhkuh 24. Jan 2008 09:30

Re: Bedingt Prozedur aufrufen
 
Dann musst Du die externen Funktionsaufrufe dynamisch laden und vorher prüfen, ob die Porttreiber installiert sind.

Das, was Du jetzt meinst, wird so nicht funktionieren.

Klaus01 24. Jan 2008 09:33

Re: Bedingt Prozedur aufrufen
 
Guten Morgen,

vielleicht kannst Du mit defines arbeiten:

Zitat:

Zitat von DelphiHilfe
Type Conditional compilation
Syntax {$IF expression}
Remarks

Compiles the source code that follows it if expression is True. expression must conform to Object Pascal syntax and return a Boolean value; it may contain declared constants, constant expressions, and the functions Defined and Declared.
For example,

{$DEFINE CLX}

const LibVersion = 2.1;

{$IF Defined(CLX) and (LibVersion > 2.0) }
... // this code executes
{$ELSE}
... // this code doesn't execute

{$IFEND}

{$IF Defined(CLX) }
... // this code executes
{$ELSEIF LibVersion > 2.0}
... // this code doesn't execute
{$ELSEIF LibVersion = 2.0}
... // this code doesn't execute
{$ELSE}
... // this code doesn't execute
{$IFEND}

The special functions Defined and Declared are available only within $IF and $ELSEIF blocks. Defined returns True if the argument passed to it is a defined conditional symbol. Declared returns True if the argument passed to it is a valid declared Pascal identifier visible within the current scope.

The $IF and $ELSEIF directives are terminated with $IFEND, unlike other conditional directives that use the $ENDIF terminator. This allows you to hide $IF blocks from earlier versions of the compiler (which do not support $IF or $ELSEIF) by nesting them within old-style $IFDEF blocks. For example, the following construction would not cause a compilation error:

{$UNDEF NewEdition}

{$IFDEF NewEdition}
{$IF LibVersion > 2.0}
...
{$IFEND}
{$ENDIF}

$IF supports evaluation of typed constants, but the compiler doesn't allow typed constants within constant expressions. As a result,

const Test: Integer = 5;

{$IF SizeOf(Test) > 2}

...

is valid, while

const Test: Integer = 5;

{$IF Test > 2 } // error

...

generates a compilation error.

If your code needs to be portable between various versions of Delphi (as well as Kylix), you will need to test whether or not this directive is supported by the compiler. You can surround your code with the following directives:

$IFDEF conditionalexpressions

. // code including IF directive
. // only executes if supported
$ENDIF


DeddyH 24. Jan 2008 09:33

Re: Bedingt Prozedur aufrufen
 
Schau Dir dazu mal z.B. diesen Thread an.

[edit] @Klaus: Ich denke, bedingte Kompilierung ist nicht das, was er sucht. [/edit]

Die Muhkuh 24. Jan 2008 09:35

Re: Bedingt Prozedur aufrufen
 
Klaus,

das geht leider nicht, da er mit den DEFINES nicht überprüfen kann, ob die Porttreiber installiert sind ;)

CRoSSi 24. Jan 2008 09:36

Re: Bedingt Prozedur aufrufen
 
:wiejetzt:
wie mache ich das?
mir reicht das schon wenn var=1 ist dass er dann die die funktion aufruft? weil var ist entweder 1 oder 0 bei 0 soll er einfach weiter machen und bei 1 soll die funktion geladen werden. Mehr brauche ich ja nicht. Das würde locker reichen.. Aber da ist das große fragezeichen wie macht man sowas am besten?

thx schonmal

oki 24. Jan 2008 09:41

Re: Bedingt Prozedur aufrufen
 
Zitat:

Zitat von CRoSSi
weil var ist entweder 1 oder 0 bei 0 soll er einfach weiter machen und bei 1 soll die funktion geladen werden. Mehr brauche ich ja nicht. Das würde locker reichen.. Aber da ist das große fragezeichen wie macht man sowas am besten?

thx schonmal

Was heißt bei dir geladen? Du meinst nicht zufällig ausführen?

Wenn Die Funktion geladen werden soll (aus DLL?), dann mußt du sie wie vorher schon gesagt dynamisch einbinden. Gib aber erst mal mehr Infos was du mit laden meinst.

Gruß oki

[edit] Sorry, hatte den zweiten Post übersehen. Also aus DLL! Dann mußt du dynamisch laden und vor der Procedurezuweisung prüfen. [/edit]

DeddyH 24. Jan 2008 09:43

Re: Bedingt Prozedur aufrufen
 
Schau Dir entweder meinen verlinkten Thread an oder arbeite dieses Tutorial von Olli mal durch.


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:55 Uhr.
Seite 1 von 4  1 23     Letzte »    

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