2011-04-20 2 views
1

В PowerBuilder 12 Classic я пытаюсь динамически получить доступ/установить свойства/элементы структуры. есть идеи как это сделать?Динамическое изменение значений структуры PowerBuilder

Я пытаюсь создать утилиту разработчика, которая будет исследовать произвольную структуру (неизвестную во время компиляции), чтобы найти ее элементы. Это я могу получить имена и типы элементов, обратившись к свойству VariableList структуры ClassDefinition. Но знание имени и типа элемента не помогает мне, поскольку я не знаю, как получить доступ к элементам по имени.

Например, у меня есть структура st_person с двумя строковыми элементами: first_name и last_name. Есть ли способ выразить настройку значения? Что-то отдаленно вроде этого:

st_person l_person 
Any myStructure 

myStructure = l_person 
myStructure.setValue("first_name") = 'John' 
myStructure.setValue("last_name") = 'Smith' 

Я действительно надеюсь, что я не пропущу что-то очевидное.

ответ

2

Нет, вам ничего не хватает, если вы не программируете в PBNI. Из того, что я слышал о PBNI, вы должны это сделать, но я не являюсь надежным источником информации о PBNI.

Есть ли причина, по которой вы используете структуру? Это похоже на то, что вы хотите, рядом с хеш-таблицей или именованным набором элементов. Я создал свой собственный набор элементов, расположенный над одним из связанных списков PFC, где в каждом узле ключ - это имя, а данные ... ну, данные. Очень грубо, у меня есть an_get (строка as_name), возвращает any, и of_set (string as_name, любое aa_value). Таким образом, я могу

myList = CREATE n_cst_NamedItemSet 
myList.of_Set ("first_name", "John") 
myList.of_Set ("last_name", "Smith") 

и на другом конце, который получает это (это отлично подходит для OpenWithParm и других вещей, которые ожидают один параметр)

is_FirstName = of_Get ("first_name") 
is_LastName = of_Get ("last_name") 

сделать еще один шаг, я of_Defined (string as_name) для проверки существования и of_Get (string as_name, ref any aa_value) возвращает целое число, которое возвращает успех, если найден, чтобы включить такие вещи, как необязательные параметры и более свободные контракты между объектами.

Есть еще, но это должно вас заставить. Если вы не используете PFC, и вы не хотите вырезать связанные списки из него, вероятно, нет причин, по которым вы не можете построить это поверх пары параллельных неограниченных массивов: одна строка для ключей и одна Любая для данные. Или замените DataStore столбцом «Строковый ключ» (DataStore быстрее выполняет поиск ключей) и столбцом «Число» в качестве индекса в неограниченный массив Any.

Удача,

Terry.

+0

Я хотел бы, чтобы определить свои собственные механизмы, но я пытаюсь работать с другими произвольные структуры народов. Внутри у меня что-то похожее на то, что вы называете моими структурами данных. Спасибо хоть! –

+0

Вздох ... отчаяния ... для тебя. У меня был чат один раз с кем-то, кто grokked PBNI (я думаю, что это, возможно, было связано с экземплярами vars на NVOs, а не с элементами структуры), и он думал, что присвоение значений переменным на основе строки времени выполнения будет возможно, но он hasn ' Это еще не сделано. Может быть, если вы намного лучше на C, чем я, это может быть маршрут. Вздох. – Terry

+0

Думаю, я вижу, о чем говорил этот парень. Пора пытать мой K & R и добраться до него! –

1

Я добавляю еще один ответ, чтобы ответить на новую цель Хью (его комментарий май 20/2011) о преобразовании строк в перечислимые значения. У меня есть код, который генерирует код, который преобразует строки для перечисления и перечисления в строки, выполненные в стиле PFC (например, n_cst_conversion). Единственное, что вам нужно сделать, чтобы адаптироваться к вашей версии PowerBuilder, - это в Constructor обновить список перечисленных классификаций. (Обычно я получаю это из отчета из браузера объектов PB, выполняю регулярный поиск текста и заменяю его.) Это не идеально, но он добирается до следующего раза, когда вы делаете серьезное обновление.

Удача,

Terry.


n_cst_enumscripts

InstanceVariables

string is_EnumTypes[] 

конструктор событие возвращает длинный

is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "accessiblerole" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "alignment" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "argcallingconvention" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "arrangeopen" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "arrangetypes" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "band" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "border" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "borderstyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "button" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "charset" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "clipboardformat" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "connectprivilege" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "converttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "cputypes" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "datetimeformat" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "dbnotification" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "direction" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "displaysizemode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "dragmodes" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "dwbuffer" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "dwconflictresolution" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "dwitemstatus" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "encoding" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "errorreturn" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "exceptionaction" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "fileaccess" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "filelock" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "filemode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "filetype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "fillpattern" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "fontcharset" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "fontfamily" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "fontpitch" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "graxisdatatype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grcolortype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grdatatype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grgraphtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grlegendtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grobjecttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grresettype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grroundtotype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grscaletype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grscalevalue" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grsorttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grsymboltype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "grtictype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "helpcommand" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "htickmarks" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "icon" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkcollectionmode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkcompressionmode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkeditstatus" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkmode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkpentip" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkpersistenceformat" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkpiceditmode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "inkpicstatus" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "keycode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "languageid" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "languagesortid" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "libdirtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "libexporttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "libimporttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "linestyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "listviewview" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "location" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "mailfiletype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "maillogonoption" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "mailreadoption" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "mailrecipienttype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "mailreturncode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "maskdatatype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "menuitemtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "menumergeoption" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "menustyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "metadatatype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "moncaldisplaystate" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "moncalrepeattype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "object" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "olefunctioncalltype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "omactivatetype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "omactivation" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "omcontentsallowed" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "omdisplaytype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "omlinkupdateoptions" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "orientationtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "ostypes" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "paragraphsetting" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "parmtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "pbtypes" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "pdfmethod" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "pointer" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "profileroutinekind" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "registryvaluetype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "richtexttoolbaractivation" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "rowfocusind" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "saveastype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "savemetadata" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "scriptkind" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "seektype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "setpostype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "sizemode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "spacing" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "sqlfunction" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "sqlpreviewfunction" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "sqlpreviewtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "stgreadmode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "stgsharemode" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "syncprocesstype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "tabposition" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "textcase" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "textstyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "timerkind" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "toolbaralignment" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "toolbarstyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "traceactivity" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "tracecategory" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "treenavigation" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "trigevent" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "typecategory" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "userobjects" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "valschemetype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "varaccess" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "variablecardinalitytype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "variablekind" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "vtextalign" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "vtickmarks" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "webpagingmethod" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "weekday" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "windowanimationstyle" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "windowstate" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "windowtype" 
is_EnumTypes[UpperBound (is_EnumTypes) + 1] = "writemode" 

* Функция of_enumprototypes() возвращает строку *

integer li_EnumCount, li_Enum 
string ls_Prototypes 
TypeDefinition ltd_Enum 
EnumerationDefinition led_Enum 

ls_Prototypes = "Add this list to the forward prototypes section of pfc_n_cst_conversion~r~n~r~n" 

li_EnumCount = UpperBound (is_EnumTypes) 
FOR li_Enum = 1 TO li_EnumCount 
    ls_Prototypes += "public function string of_string (" + is_EnumTypes[li_Enum] + " aenum_Value)~r~n" 
NEXT 
FOR li_Enum = 1 TO li_EnumCount 
    ls_Prototypes += "public function integer of_" + is_EnumTypes[li_Enum] + " (string as_Value, " + is_EnumTypes[li_Enum] + " ae_Value)~r~n" 
NEXT 
ls_Prototypes += "~r~n~r~n" 

RETURN ls_Prototypes 

* Функция of_enumscripts() возвращает строку *

integer li_EnumCount, li_Enum, li_ValueCount, li_Value 
string ls_Scripts, ls_Name 
TypeDefinition ltd_Enum 
EnumerationDefinition led_Enum 

li_EnumCount = UpperBound (is_EnumTypes) 
FOR li_Enum = 1 TO li_EnumCount 
    ltd_Enum = FindTypeDefinition (is_EnumTypes[li_Enum]) 
    led_Enum = ltd_Enum 

    ls_Scripts += "public function string of_string (" + is_EnumTypes[li_Enum] + & 
     " aenum_Value);//////////////////////////////////////////////////////////////////////////////~r~n" + & 
     "//~r~n" + & 
     "//~tFunction:~t~tof_String~r~n" + & 
     "//~r~n" + & 
     "//~tAccess:~t~t~tpublic~r~n" + & 
     "//~r~n" + & 
     "//~tArguments:~r~n" + & 
     "//~tae_icon~t~t~tThe " + is_EnumTypes[li_Enum] +" value to be converted to a string.~r~n" + & 
     "//~r~n" + & 
     "//~tReturns: ~t~tstring~r~n" + & 
     "//~t~t~t~t~t~tA string representation of the " + is_EnumTypes[li_Enum] + " value.~r~n" + & 
     "//~t~t~t~t~t~tIf aenum_Value is NULL, the function returns NULL.~r~n" + & 
     "//~t~t~t~t~t~tIf aenum_Value is Invalid, the function returns '!'.~r~n" + & 
     "//~r~n" + & 
     "//~tDescription:~tConverts the " + is_EnumTypes[li_Enum] + " enumerated datatype to a~r~n" + & 
     "//~t~t~t~t~t~treadable string representation.~r~n" + & 
     "//~r~n" + & 
     "//////////////////////////////////////////////////////////////////////////////~r~n" + & 
     "string ls_Null~r~n~r~n// Check parameter~r~nIF IsNull (aenum_Value) THEN~r~n~tSetNull (ls_Null)~r~n~tRETURN ls_Null~r~nEND IF~r~n~r~n" 
    ls_Scripts += "CHOOSE CASE aenum_Value~r~n" 
    li_ValueCount = UpperBound (led_Enum.Enumeration) 
    FOR li_Value = 1 TO li_ValueCount 
     ls_Name = led_Enum.Enumeration[li_Value].Name 
     ls_Scripts += "~r~n~tCASE " + ls_Name + "!;RETURN ~"" + ls_Name + "~"~r~n" 
    NEXT 
    ls_Scripts += "~r~nEND CHOOSE~r~n~r~n// Invalid parameter~r~nRETURN ~"!~"~r~nend function~r~n~r~n" 
NEXT 

FOR li_Enum = 1 TO li_EnumCount 
    ltd_Enum = FindTypeDefinition (is_EnumTypes[li_Enum]) 
    led_Enum = ltd_Enum 

    ls_Scripts += "public function integer of_" + is_EnumTypes[li_Enum] + & 
       " (string as_Value, " + is_EnumTypes[li_Enum] + " ae_Value);//////////////////////////////////////////////////////////////////////////////~r~n" + & 
       "//~r~n" + & 
       "//~tFunction: ~t~tof_" + is_EnumTypes[li_Enum] + "~r~n" + & 
       "//~r~n" + & 
       "//~tAccess: ~t~tpublic~r~n" + & 
       "//~r~n" + & 
       "//~tArguments:~r~n" + & 
       "//~tas_Value~t~t~tThe string value to be converted to " + is_EnumTypes[li_Enum] + " datatype value.~r~n" + & 
       "//~tae_Value~t~t~tA " + is_EnumTypes[li_Enum] + " variable passed by reference which will~r~n" + & 
       "//~t~t~t~t~t~t~thold the " + is_EnumTypes[li_Enum] + " value that the string value was~r~n" + & 
       "//~t~t~t~t~t~t~tconverted to.~r~n" + & 
       "//~r~n" + & 
       "//~tReturns: ~t~tinteger~r~n" + & 
       "//~t~t~t~t~t~t1 if a successful conversion was made.~r~n" + & 
       "//~t~t~t~t~t~tIf as_Value value is NULL, function returns -1~r~n" + & 
       "//~t~t~t~t~t~tIf as_Value value is Invalid, function returns -1~r~n" + & 
       "//~r~n" + & 
       "//~tDescription: Converts a string value to a " + is_EnumTypes[li_Enum] + " data type value.~r~n" + & 
       "//~r~n" + & 
       "//////////////////////////////////////////////////////////////////////////////~r~n" + & 
       "//~r~n" + & 
       "//~tRevision History~r~n" + & 
       "//~r~n" + & 
       "//~tPFC OS Issues~r~n" + & 
       "//~t6~tAuto-generated intial version~r~n" + & 
       "//~t~tTerry Voth: http://www.techno-kitten.com~r~n" + & 
       "//~r~n" + & 
       "//////////////////////////////////////////////////////////////////////////////~r~n" + & 
       "//~r~n" + & 
       "//~tCopyright © 1996-1997 Sybase, Inc. and its subsidiaries. All rights reserved.~r~n" + & 
       "//~tAny distribution of the PowerBuilder Foundation Classes (PFC)~r~n" + & 
       "//~tsource code by other than Sybase, Inc. and its subsidiaries is prohibited.~r~n" + & 
       "//~r~n" + & 
       "//////////////////////////////////////////////////////////////////////////////~r~n" + & 
       "as_Value = Lower (as_Value)~r~n~r~n// Check parameter~r~nIF IsNull (as_Value) THEN~r~n~tRETURN -1~r~nEND IF~r~n~r~n" 
    ls_Scripts += "CHOOSE CASE as_Value~r~n" 
    li_ValueCount = UpperBound (led_Enum.Enumeration) 
    FOR li_Value = 1 TO li_ValueCount 
     ls_Name = led_Enum.Enumeration[li_Value].Name 
     ls_Scripts += "~r~n~tCASE ~"" + ls_Name + "~", ~"" + ls_Name + "!~"~r~n~t~tae_Value = " + ls_Name + "!~r~n~t~tRETURN 1~r~n" 
    NEXT 
    ls_Scripts += "~r~nEND CHOOSE~r~n~r~n// Invalid parameter~r~nRETURN -1~r~nend function~r~n~r~n" 
NEXT 

RETURN ls_Scripts 
Смежные вопросы