Setting a numeric or date field will not cause any problems, except that the value is set to zero for numeric fields.
That's actually a problem for my method, because if the type of the field is
Integer, for example and the key let's say equal '927fdb00-7c87-438f-bf78-ab1f15428440', then the following line of code:
- Code: Select all
field.AsString = key;
will cause the corresponding objects to show 927 (obj.ResolvedText will have value 927). This means that the following condition will not be true:
- Code: Select all
if (obj.ResolvedText == key)
As a result, my method will return empty list.
So, for
Integer fields I would prefer to use integer key, e.g. int.MinValue and use AsInteger property. Here is the modification of the code for integer fields:
- Code: Select all
/// <summary>
/// In the specified template find all the objects which hold references to the specified INTEGER field.
/// </summary>
public static List<TVPEObject> FindVpeObjects(this TVPETemplate tpl, string dataSourcePrefix, string integerFieldName)
{
var objs = new List<TVPEObject>();
TVPEField field = tpl.FindFieldObject(dataSourcePrefix, integerFieldName);
int origFiledValue = field.AsInteger;
int key = int.MinValue;
field.AsInteger = key;
for (int p = 0; p < tpl.PageCount; p++)
{
var page = tpl.PageObject(p);
for (int o = 0; o < page.VpeObjectCount; o++)
{
TVPEObject obj = page.VpeObject(o);
if (obj.ResolvedText == key.ToString())
objs.Add(obj);
}
}
field.AsInteger = origFiledValue;
return objs;
}
For the
Number fields, I would use double.MinValue as a key, but as soon as I assign this value to Number field using field.AsNumber property and try to get ResolvedText, I get AccessViolationException error:
- Code: Select all
/// <summary>
/// In dycodoc, create a template with a filed name NewField1 and type Number. Add a text object Text1 mapped to this field.
/// </summary>
/// <param name="tpl">Provide the loaded template.</param>
private void ReproduceError(TVPETemplate tpl)
{
TVPEField field = tpl.FindFieldObject("NewTable1", "NewField1");
field.AsNumber = double.MinValue;
TVPEObject obj = tpl.FindVpeObject("Text1");
string s = obj.ResolvedText; //AccessViolationException {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}
}
This error does not block me actually. This is because I realized that I need my method (FindVpeObjects) only for Text fields. The reason why I need this method is to support different languages, but for Integer and Number fields, there is nothing to translate. (For Boolean fields this method will not work because there are only 2 possible values. For Date Time fields it is not possible to find all the objects mapped to a particular filed using my approach because I don't see how to get and set Date Time Format of a field programmatically.)