Forum > LCL

[solved] RadioGroup - get the name / string of the chosen item

<< < (2/2)

KodeZwerg:

--- Quote from: Nicole on September 20, 2022, 10:29:28 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---nyString:= RadioGroup1.Items.strings[RadioGroup1.ItemIndex];
--- End quote ---
Is actual the same like ->

--- Quote from: Arioch on September 19, 2022, 08:23:18 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---s: string; i: integer; begin   s := '';   i := rg.ItemIndex;    if (i >= 0) and (i < rg.Items.Count) then      s := rg.Items[i];
--- End quote ---
Please add his checks for safety purposes aswell. (eg: nothing selected = your code blow up)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if ((RadioGroup1.ItemIndex > -1) and (RadioGroup1.ItemIndex < RadioGroup1.Items.Count)) then  myString := RadioGroup1.Items.strings[RadioGroup1.ItemIndex]  else  myString := 'out of bounds.';

paweld:
@KodeZwerg: While checking whether ItemIndex is less than 0 is needed, because very often you can get an error here, checking whether ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count

KodeZwerg:

--- Quote from: paweld on September 20, 2022, 12:35:00 pm ---@KodeZwerg: While checking whether ItemIndex is less than 0 is needed, because very often you can get an error here, checking whether ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count

--- End quote ---
I do agree but upcoming code can be wrong, and this will catch developer made mistakes.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if index > -1 then  astring := items[1234]; // here it can blow, out of range/bounds

Arioch:

--- Quote from: paweld on September 20, 2022, 12:35:00 pm --- ItemIndex >= Count is completely unnecessary, because ItemIndex cannot be greater than or equal to Count

--- End quote ---

today it will not, tomorrow some update comes and it becomes possible

in this place it can not - in others it can be (i remember implementing OnItemHint for RxLib's checklistbox,  ItemIndex param would exactly be Count if the mouse was below the last item)

you better train good habbits uniformly, than memorize hundred places which spcecific sets of checks is needed  in every of them

better safe than sorry

if to take issues i'd rather have one with index > -1 as opposed to index >= 0  :-D

paweld:
@Arioch: Redundant unnecessary code is not a good habit. It only unnecessarily obscures the actual code.

Navigation

[0] Message Index

[*] Previous page

Go to full version