Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naming Convention of Object 1

Status
Not open for further replies.

kyletreyfield

Technical User
Jun 12, 2008
27
US
I have a script that injests data from a webhook. I don't have any control over the field names that come in. The field name contains a dash, which I think VFP is interpreting as a subtraction symbol in the object name so it complains. I'm wondering if there is a workaround. Here is the code:

IF TYPE("loObj2.fields.comments_upload-1_file")=="U"==.f.
STORE loObj2.fields.comments_upload-1_file to upload_file_var
ELSE
STORE "" TO upload_file_var
ENDIF

The problem is on the second line. Anyone have ideas on what I can do?
 
That test on line one is a bit odd
do you mean
Code:
IF TYPE("loObj2.fields.comments_upload-1_file")<>"U"
 
You could put the entire test in a TRY-CATCH-ENDTRY construct. In the CATCH, it would be invalid with the dash.

Code:
TRY
   IF TYPE("loObj2.fields.comments_upload-1_file")=="U"
      STORE loObj2.fields.comments_upload-1_file to upload_file_var
   ELSE
      STORE "" TO upload_file_var
   ENDIF
CATCH
   STORE "" TO upload_file_var
ENDTRY

Note that I removed the ==.F. test in the IF statement.
 
I think your problem is in the object name => there is a hyphen in there which looks more like an equation
than a name!

Code:
loObj2.fields.comments_upload-1_file

Maybe use
Code:
loObj2.fields.comments_upload1_file
 
After you ingest the data can you drop into the debugger and analyse the object to check the properties and their names?

Reason I ask is that I don't think you can have a property in VFP with a hyphen in it's name, probably for this very reason, that a hyphen in the middle of regular code is going to be interpreted as the subtract operator.

To test I tried a couple of things, first, very simply creating a test object and then using ADDPROPERTY() to try to put a property or two on the object with a hyphen in the name and it throws an Error 1470 "Incorrect property name". If it had worked I would have been surprised but my next step was going to be to try to use GETPEM() to reference the value instead of just using regular references, but since it doesn't work it won't get that far. *** The following code will error multiple times to prove this point ***

Code:
loTest = CreateObject("Empty")

AddProperty(loTest, "Prop_Underscore", 11)
AddProperty(loTest, "Prop-Hyphen", 42)
AddProperty(loTest, "comments_upload-1_file", "Hello, World")

Clear

? "Test"

? GetPem(loTest, "Prop_Underscore")
? loTest.Prop_Underscore

? GetPem(loTest, "Prop-Hyphen")
? loTest.Prop-Hyphen

? GetPem(loTest, "comments_upload-1_file")
? loTest.comments_upload-1_file

Then I wondered if there was any kind of backdoor way of getting an object with a property with a hyphen in the name and I tried JSON. The JSON method I used is from NfJsonRead and that is intelligent enough to implicitly convert the hyphens to underscores to avoid this particular aspect of VFP. The JSON:

JSON:
{"Prop_Underscore":11,"Prop-Hyphen":42}

...got converted to an object where both properties had underscores in the names, so that answered that.

The fact that you say you are getting onto the second line does suggest that somehow you are getting the property with that name but I'm not sure how, the few ways I've tried have all failed.
 
Why not instead of STORE you just say
upload_file_var = loObj2.fields.comments_upload-1_file

I would think it would be okay either way.

Also, what is the error you are getting?

BTW, is fields a collection on that loObj2 object? If so, that may be the issue and you have to make sure which fields object you need.
 
The normal way to deal with invalid characters in the names of object, fields, etc. would be with a simple STRTRAN(). To demonstrate:

Code:
lcBad = "loObj2.fields.comments_upload-1_file"
lcGood = STRTRAN(lcBad, "-", "_")

This will change all hyphens to underscores, which would be valid.

That said, it's not clear how you are getting the "bad" name in the first place.

Mike
 
The normal way to deal with invalid characters in the names of object, fields, etc. would be with a simple STRTRAN(). To demonstrate:

Code:
lcBad = "loObj2.fields.comments_upload-1_file"
lcGood = STRTRAN(lcBad, "-", "_")

This will change all hyphens to underscores, which would be valid.

That said, it's not clear how you are getting the "bad" name in the first place.

Mike
Thank you Mike. As I said the "comments_upload-1_file" name comes from an outside party's API. I need to put it into an object but I can't change the name.
 
Is loObj2 a javascript object coming from internet explorer automation?
Or do you parse json into loObj2?

When using Marco Plazas nfjson it would translate such names into VFP names when building up the VFP object from json, even if the json has other names.
 
Last edited:
You indicated that the name comes from an outside source and you cannot change it. Why is this? Does this name have to be returned? Possibly you could use a property to store the name "as is" and then convert as Mike indicated. When communicating back, then use the name from the property value.
 
I have a script that injests data from a webhook. I don't have any control over the field names that come in. The field name contains a dash, which I think VFP is interpreting as a subtraction symbol in the object name so it complains. I'm wondering if there is a workaround. Here is the code:

IF TYPE("loObj2.fields.comments_upload-1_file")=="U"==.f.
STORE loObj2.fields.comments_upload-1_file to upload_file_var
ELSE
STORE "" TO upload_file_var
ENDIF

The problem is on the second line. Anyone have ideas on what I can do?

Kyle,

How is loObj2 created?

Is the fields object a collection? Can it be iterated, or can its members be addressed individually? For instance, loObj2.fields(0) or loObj2.fields.item(0) (or 1, if one-based), instead of using the field name? Or as a reference in a FOR EACH loop, as in FOR EACH m.FieldValue IN loObj2.fields?
 
I have a script that injests data from a webhook. I don't have any control over the field names that come in. The field name contains a dash, which I think VFP is interpreting as a subtraction symbol in the object name so it complains. I'm wondering if there is a workaround. Here is the code:

I'd second Greg's suggestion. That's where I was going. Store their name, use your own that doesn't freak out VFP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top