Recent

Author Topic: Passing strings  (Read 3492 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Passing strings
« on: January 19, 2018, 10:08:54 pm »
I have a function to write a logfile setup as such:
Code: Pascal  [Select][+][-]
  1. function write_bot_killer_log(var sDataToWrite : String): boolean;


When I call the function such as this:

Code: Pascal  [Select][+][-]
  1. logging.write_bot_killer_log('Invalid BotKiller key pressed');

It is wanting a variable. Is there a way I can pass just text instead? it is set as string, so I am not sure what I'm missing!
 
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Passing strings
« Reply #1 on: January 19, 2018, 10:09:46 pm »
Sorry I posted this in wrong place -> tried to move but i couldnt!
-Nugax

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Passing strings
« Reply #2 on: January 19, 2018, 10:11:24 pm »
Just remove the "var" from your function's parameter specification.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Passing strings
« Reply #3 on: January 19, 2018, 10:13:13 pm »
Defining the parameter sDataToWrite as var instructs the compiler that you intent to change the data passed in that parameter and you want that changes to persist when you exit the function (aka return the changes to the caller).  if you have intention to change the data passed then define it as const or if you do not have any intention to return the changes then simple remove the var definition.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Passing strings
« Reply #4 on: January 20, 2018, 09:32:29 am »
use procedure instead of function
Code: Pascal  [Select][+][-]
  1.     procedure write_bot_killer_log(const sDataToWrite : String);
  2.  
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Passing strings
« Reply #5 on: January 20, 2018, 10:55:29 am »
No the function is a boolean: can be used to test write success or fail. Much better than procedure, actually... 8-)
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Passing strings
« Reply #6 on: January 20, 2018, 02:44:57 pm »
Why? If it will giving a result back, it's usefull. Otherwise you use memory without purposes.
In his case it write something to a log and nothing more.   
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Passing strings
« Reply #7 on: January 20, 2018, 02:47:59 pm »
Why? If it will giving a result back, it's usefull. Otherwise you use memory without purposes.
In his case it write something to a log and nothing more.

All the more reason to check if you actually succeeded. Otherwise your log becomes unusable...  It is simply good practice.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Passing strings
« Reply #8 on: January 20, 2018, 02:59:41 pm »
Check for what. Hello, I wrote somehing in a log. I gonna tell you that with true.
Code: Pascal  [Select][+][-]
  1. if not log('hello') then
  2.   showmessage('log isn't working')
  3.  
  4. function log(const aValue : string) : boolean;
  5. begin
  6.  if lines.add(aValue) = -1 then
  7.     result := true
  8.  else
  9.   result := false;
  10. end;  
  11.  

This is inefficient.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Passing strings
« Reply #9 on: January 20, 2018, 03:00:54 pm »
Check for what. Hello, I wrote somehing in a log. I gonna tell you that with true.
Code: Pascal  [Select][+][-]
  1. if not log('hello') then
  2.   showmessage('log is not  working');
  3.  
  4. function log(const aValue : string) : boolean;
  5. begin
  6.   if lines.add(aValue) = -1 then
  7.      result := true
  8.   else
  9.    result := false;
  10. end;  
  11.  

This is inefficient.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Passing strings
« Reply #10 on: January 20, 2018, 05:08:20 pm »
No the function is a boolean: can be used to test write success or fail. Much better than procedure, actually... 8-)

I wanted to know if it wrote succesfully -> if it didnt, maybe the file has issues, etc.
Thats why its a func
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Passing strings
« Reply #11 on: January 20, 2018, 05:15:07 pm »
I used rwrite(log); for opening the file -> I assume this creates a new file. What should I be looking at for appending, etc.
-Nugax

 

TinyPortal © 2005-2018