Recent

Author Topic: Accesin to a property by a string  (Read 4639 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 743
    • My github
Accesin to a property by a string
« on: August 27, 2010, 05:23:53 pm »
Hello, my program reads a file like this:
Quote
atmega328.upload.protocol=stk500
atmega328.upload.maximum_size=30720
atmega328.upload.speed=57600
atmega328.bootloader.low_fuses=0xFF
atmega328.bootloader.high_fuses=0xDA

I read one line, and I got (after remove some parts):

Quote
String1 := 'upload.protocol'
data1 := 'stk500'


I have a object with like this:

Quote
MYobject.upload.protocol
Myobject.bootloader.low
(etc. The object have  property called like the lines)

Well I want to assign Data1 to MYobject.upload.protocol

I thought:
 
Quote
IF String1 = 'upload.protocol' Then
   MYobject.upload.protocol := data1;
ElseIf (...)

But Can I access to the properties, in other way more easy?, like:
Quote
MyObject.[String1]:=Data1


I hope I explain... because It's a very complicate explain this problem for me in english

/BlueIcaro

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Accesin to a property by a string
« Reply #1 on: August 27, 2010, 07:44:21 pm »
Serial communication with an atmega328? I prefer PIC's  :D

I can think if several solutions:
Use an array property like TStringList.Values[...]. This makes using the class easier, but you'll still have to check the string value of the parm within the class.

Use some sort of RTTI. But that's probably a bit messy because you're distributing the parms over several classes.

Swap responsibilities: give the classes that need parameter values (upload, bootloader) access to the settings repository, so they can request the values when needed.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

BlueIcaro

  • Hero Member
  • *****
  • Posts: 743
    • My github
Re: Accesin to a property by a string
« Reply #2 on: August 27, 2010, 09:57:15 pm »
Thank you for your answer. I think I don't have choice, I'll have to hard-code.

I'm making a new IDE for Arduino, because the original it's code in Java, and in my opnion it's a little bit limited. So I'm trying to do a new IDE, like Lazarus. By the moment the proyect is very new, only a few forms, and code.

Quote
Serial communication with an atmega328? I prefer PIC's
Well, I have a board called Arduino, check the www.arduino.cc.

/BlueIcaro

Leledumbo

  • Hero Member
  • *****
  • Posts: 8523
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Accesin to a property by a string
« Reply #3 on: August 28, 2010, 06:26:52 am »
TStringList supports Key=Value pairs, so if you do:
Code: [Select]
StringList.Add('MyKey=MyValue');
You can get MyValue or assign MyKey with:
Code: [Select]
MyValue := StringList.Values['MyKey'];

StringList.Values['MyKey'] := MyValue;
You can also use true map data structure with TFPGMap from fgl unit.

 

TinyPortal © 2005-2018