Forum > General
convert enum to string and back
TBug:
Hello to all,
1. How do I get the amount of posible enum-values from the type 'TComponentStyle'?
2. How can I convert an enum-value from TComponentStyle to a string, or get the enum-value from the string 'csInheritable'?
crosspost - german lazarusforum
http://www.lazarusforum.de/viewtopic.php?f=10&t=9137
.
Cyrax:
1. High(TComponentStyle) - Low(TComponentStyle).
2. Convert to string : WriteStr(AStringVariable, AComponentStyleVariable)
Convert from string : ReadStr(AStringVariable, AComponentStyleVariable)
TBug:
Thank you,
but the first part of your answer doesn't work.
--- Quote from: Cyrax on October 28, 2015, 09:59:03 pm ---1. High(TComponentStyle) - Low(TComponentStyle).
--- End quote ---
lInt := (High(TComponentStyle) - Low(TComponentStyle));
IDE-Output:
--- Quote ---udatamoduleglobalfunc.pas(778,35) Error: Operation "-" not supported for types "<enumeration type>" and "<enumeration type>"
--- End quote ---
I need to do something like this:
--- 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";}};} ---function HGTAnchorKindToString(AValue: TAnchorKind): String;begin Result := GetEnumName(TypeInfo(TAnchorKind),Ord(AValue));end; function HGStringToTAnchorKind(AValue: String): TAnchorKind;begin Result := TAnchorKind(GetEnumValue(TypeInfo(TAnchorKind),AValue));end;
--- 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";}};} ---procedure TDM_GlobalFunc.ListTAnchorKindAssignTo(ADest: TStrings);var lEnumValue: TAnchorKind;begin if (FListTAnchorKind.Count = 0) then begin for lEnumValue := Low(TAnchorKind) to High(TAnchorKind) do begin FListTAnchorKind.Add(HGTAnchorKindToString(lEnumValue)); end; end; ADest.Assign(FListTAnchorKind);end;
--- 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";}};} ---procedure TF_HGFormPanel.UpdateComponentValues;var lEnumValueAnchorKind: TAnchorKind;begin for lEnumValueAnchorKind := Low(TAnchorKind) to High(TAnchorKind) do begin lIndex := CLB_Anchors.Items.IndexOf(HGTAnchorKindToString(lEnumValueAnchorKind)); if (lIndex >= 0) then begin CLB_Anchors.Checked[lIndex] := (lEnumValueAnchorKind in MyComponent.Anchors); end; end; end;
This works with e.g. TControlCellAlign but not with TComponentStyle or any kind of type declared like:
--- 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";}};} ---TComponentStyle = set of (csInheritable, csCheckPropAvail, csSubComponent, csTransient);
Bart:
--- Quote from: TBug on October 28, 2015, 11:07:03 pm ---
--- Quote from: Cyrax on October 28, 2015, 09:59:03 pm ---1. High(TComponentStyle) - Low(TComponentStyle).
--- End quote ---
lInt := (High(TComponentStyle) - Low(TComponentStyle));
IDE-Output:
--- Quote ---udatamoduleglobalfunc.pas(778,35) Error: Operation "-" not supported for types "<enumeration type>" and "<enumeration type>"
--- End quote ---
--- End quote ---
Try (untested):
--- Code: --- lInt := Ord((High(TComponentStyle))- Ord(Low(TComponentStyle)));
--- End code ---
Bart
taazz:
--- 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";}};} ---function GetSetValues(const aSet:PTypeInfo):string;var vData1 : PTypeData; vData2 : PTypeData; vCntr : Integer;begin Result := ''; if aSet^.Kind = tkSet then begin vData1 := GetTypeData(aSet); vData2 := GetTypeData(vData1^.CompType); for vCntr := vData2^.MinValue to vData2^.MaxValue do Result := Result+ GetEnumName(vData1^.CompType,vCntr)+LineEnding; end;end;
Navigation
[0] Message Index
[#] Next page