Recent

Author Topic: [SOLVED]how to use a variable as a part of a programming code?  (Read 3464 times)

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Hi another question (my name really suites me 8-))

I need to know can we use a variable lets call it a integer variable as a part of a code
Something like this

Lable(a).caption=a;
{Its a lazarus project}

In here a means a variable integer
How to use that like that is there a way
================================

My second (some kind of a same looking) question

Can we use a string as a part of a programming code
Ex:-
      b:='lable'+(inttostr(a))+'.caption';
      (b):=c;
{C,a are integer}
{b is a string}

Here it doesnt work all happen is just b get = to c

Answer with example code or with a web link which can be understand easily as i was not a pro pascal programmer still, please :)
« Last Edit: August 02, 2017, 09:44:05 am by questionstoaskislot »

Handoko

  • Hero Member
  • *****
  • Posts: 5382
  • My goal: build my own game engine using Lazarus
Re: how to use a variable as a part of a programming code?
« Reply #1 on: August 02, 2017, 07:15:13 am »
This is not valid in Pascal:
Lable(a).caption=a;

This is valid in Pascal, but not the way you was thinking:
b:='lable'+(inttostr(a))+'.caption';

This is not valid in Pascal:
(b):=c;

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to use a variable as a part of a programming code?
« Reply #2 on: August 02, 2017, 07:23:31 am »
Ok i get it
And i get that lable was wrong and it should be label

But the thing is I don't want to check whether that my code was not right or wrong.I need to know how to do it , please

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to use a variable as a part of a programming code?
« Reply #3 on: August 02, 2017, 07:35:42 am »
There are no component arrays like the vb control arrays in lazarus. There are a couple of ways to accomplish a generic property assignment but they all have a speed penalty. For example
Code: Pascal  [Select][+][-]
  1. Function Tform1.SetLabelCaption(const aLabelName, aCaption:String);
  2. var
  3.   vCmp:TComponent;
  4. begin
  5.   vCmp := FindComponent(aLabelName);
  6.   if assigned(vCm) and (vCmp is TLabel) then TLabel(vCmp).Caption := aCaption Else raise Exception.create('Invalid label name '+aLabelName+'.');
  7. end;
  8.  
and there is the runtime type information you can use to change any published property to any thing you want as long as you know the property name and have a valid value for it but I wouldn't use it on daily bases my self.
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

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to use a variable as a part of a programming code?
« Reply #4 on: August 02, 2017, 08:12:41 am »
Good point but the thing is I don't know the property name correctly
It gonna be something  gave by user any other way.. :-\

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to use a variable as a part of a programming code?
« Reply #5 on: August 02, 2017, 08:28:06 am »
why would a user know anything about your properties? What are you writing?
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

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to use a variable as a part of a programming code?
« Reply #6 on: August 02, 2017, 09:06:44 am »
This is what i am writing

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to use a variable as a part of a programming code?
« Reply #7 on: August 02, 2017, 09:21:16 am »
Hey there is a way
We can use that
U are right taazz
See this

Var
   Vcmp:Tcomponent;
   Labelname:string;
Begin
   Labelname:=edit1.text;
   Vcmp:=FindComponent(labelname);
°°°°°

It work
Wooo hooo 8-)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to use a variable as a part of a programming code?
« Reply #8 on: August 02, 2017, 09:47:36 am »
This is what i am writing
sorry dude you have to do a lot better than that. Have fun.
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

Handoko

  • Hero Member
  • *****
  • Posts: 5382
  • My goal: build my own game engine using Lazarus
Re: [SOLVED]how to use a variable as a part of a programming code?
« Reply #9 on: August 02, 2017, 10:03:17 am »
@questionstoaskislot

Exposing control's properties to users is not a good idea, it may have security issue. You maybe don't understand now, you will know it in the future.

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: [SOLVED]how to use a variable as a part of a programming code?
« Reply #10 on: August 02, 2017, 06:04:34 pm »
Oh no no nooo
User never get to know the properties
It was just a example thing
You will get to know what i was doing soon very soon 8-)

 

TinyPortal © 2005-2018