Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Accessing an object's property by string in Xbasic?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Accessing an object's property by string in Xbasic?

    First, I didn't see any section in the forum to post questions specifically related to Xbasic, so I decided to post it here. Sorry if this is the wrong place.

    I have a situation where I have an object that has around 16 properties, each of which have objects as properties themselves. I know in JavaScript you can access an object via the direct variable:

    Code:
    ObjectName.ObjectProperty
    But in JS you can also access it via string:

    Code:
    ObjectName["ObjectProperty"]
    I know this is specific to JS, but is there any way I can access an object's property by string in Xbasic as well?

    Thanks!

    #2
    Re: Accessing an object's property by string in Xbasic?

    You can run javascript at the end of each xbasic function. You can use that javascript to do whatever you want. (Precede double quotes that you want to keep in a string with a \)

    However, you can't directly access those properties from xbasic.

    Xbasic runs on the server.

    You would have to expose those properties in a session variable or as values in another control to pass them to the xbasic function.

    Comment


      #3
      Re: Accessing an object's property by string in Xbasic?

      I have a situation where I have an object that has around 16 properties, each of which have objects as properties themselves. I know in JavaScript you can access an object via the direct variable:
      Where did you get this object... where did it come from... what is it?

      Comment


        #4
        Re: Accessing an object's property by string in Xbasic?

        This object is a custom made JSON string saved in the database that stores rates for accessorial charges which is then parsed in Xbasic using json_parse to turn it into an Xbasic object. For example, the object would have something like this when I use json_parse on the returned JSON string:

        Code:
        accessorialObject.DeliveryAppointment.rate
        But I also store on the server a list of accessorials in array format so I am able to loop through available accessorials. Here's my scenario:

        Code:
        ' accessorialArray is a simple array containing strings for each entry that correspond to the first level in the object shown below. For this example let's use:
        ' accessorialArray[1] = "test1"
        ' accessorialArray[2] = "test2"
        ' accessorialArray[3] = "test3"
        
        ' accessorialObject is the actual object containing details for each accessorial including the rate. For this example let's use:
        ' accessorialObject = 
        ' +test1.
        '   .rate = 2500
        '   .type = 'flat'
        ' +test2.
        '   .rate = 7500
        '   .type = 'perhundred'
        ' +test3.
        '   .rate = 5000
        '   .type = 'flat'
        
        FOR i = 1 to accessorialArray.size()
          ?"Rate is: " + accessorialObject.accessorialArray[i].rate + crlf()
        NEXT
        I know this wouldn't work because it would actually look for a subelement in accessorialObject named accessorialArray[], but I used it to visually represent my scenario.

        Sorry, perhaps this is bad design on my part with the mixing of arrays and objects in both javascript and xbasic, but for now I'm curious if there's a way to access an object property by a string. I'm still learning all this as I go along, specifically with Xbasic.

        Thanks!
        Last edited by DVD528; April 14, 2017, 05:09 PM. Reason: fixed first code snippet

        Comment


          #5
          Re: Accessing an object's property by string in Xbasic?

          Since it's JSON stored in a database column, could you post a string?

          Comment


            #6
            Re: Accessing an object's property by string in Xbasic?

            Certainly. These are randomized numbers and types used for testing, but they will still work perfectly fine.

            Code:
            {"InsidePickup":{"type":"flat","rate":6550},"LiftgateOrigin":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessPickup":{"type":"flat","rate":8500},"ResidentialPickup":{"type":"flat","rate":5515},"TradeshowPickup":{"type":"flat","rate":7500},"DeliveryAppointment":{"type":"flat","rate":5550},"InsideDelivery":{"type":"perhundred","rate":5400},"LiftgateDestination":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessDelivery":{"type":"flat","rate":2500},"NotifyBeforeDelivery":{"type":"flat","rate":4000},"ResidentialDelivery":{"type":"flat","rate":5515},"TradeshowDelivery":{"type":"flat","rate":7500},"ProtectFreezing":{"type":"perhundred","rate":2005},"MarkingTagging":{"type":"minmax","rate":15000,"min":50050,"max":60005},"SingleShipment":{"type":"perhundred","rate":2400},"Hazmat":{"type":"minmax","rate":1500,"min":10900,"max":29900}}
            Also, here's the array of names (in JS format) that I'm using to access each top-level element in the object:

            Code:
            ["InsidePickup","LiftgateOrigin","LimitedAccessPickup","ResidentialPickup","TradeshowPickup","DeliveryAppointment","InsideDelivery","LiftgateDestination","LimitedAccessDelivery","NotifyBeforeDelivery","ResidentialDelivery","TradeshowDelivery","ProtectFreezing","MarkingTagging","SingleShipment","Hazmat"]
            I change that into an Xbasic array by doing the following (although the rawArray string would be retrieved with sql_get_values rather than inserted directly):

            Code:
            dim rawArray as C = "[\"InsidePickup\",\"LiftgateOrigin\",\"LimitedAccessPickup\",\"ResidentialPickup\",\"TradeshowPickup\",\"DeliveryAppointment\",\"InsideDelivery\",\"LiftgateDestination\",\"LimitedAccessDelivery\",\"NotifyBeforeDelivery\",\"ResidentialDelivery\",\"TradeshowDelivery\",\"ProtectFreezing\",\"MarkingTagging\",\"SingleShipment\",\"Hazmat\"]"
            dim accArray[0] as C
            accArray.initialize(comma_to_crlf(strtran(substr(rawArray, 2, len(rawArray) - 2), "\"")))
            Let me know if you need anything else. Thanks!

            Comment


              #7
              Re: Accessing an object's property by string in Xbasic?

              for the first portion you can access in xbasic like this
              Code:
              dim jst as c
              jst = <<%str%
              {
              	"InsidePickup":{"type":"flat","rate":6550},
              	"LiftgateOrigin":{"type":"minmax","rate":2000,"min":15900,"max":39900},
              	"LimitedAccessPickup":{"type":"flat","rate":8500},
              	"ResidentialPickup":{"type":"flat","rate":5515},
              	"TradeshowPickup":{"type":"flat","rate":7500},
              	"DeliveryAppointment":{"type":"flat","rate":5550},
              	"InsideDelivery":{"type":"perhundred","rate":5400},
              	"LiftgateDestination":{"type":"minmax","rate":2000,"min":15900,"max":39900},
              	"LimitedAccessDelivery":{"type":"flat","rate":2500},
              	"NotifyBeforeDelivery":{"type":"flat","rate":4000},
              	"ResidentialDelivery":{"type":"flat","rate":5515},
              	"TradeshowDelivery":{"type":"flat","rate":7500},
              	"ProtectFreezing":{"type":"perhundred","rate":2005},
              	"MarkingTagging":{"type":"minmax","rate":15000,"min":50050,"max":60005},
              	"SingleShipment":{"type":"perhundred","rate":2400},
              	"Hazmat":{"type":"minmax","rate":1500,"min":10900,"max":29900}
              	}
              %str%
              dim pp as p
              pp = json_parse(jst)
              msgbox("",pp.insidePickup.type)
              msgbox("",""+pp.insidePickup.rate)
              msgbox("",pp.Hazmat.type)
              msgbox("",""+pp.Hazmat.rate)
              Last edited by GGandhi; April 14, 2017, 07:15 PM.
              thanks for reading

              gandhi

              version 11 3381 - 4096
              mysql backend
              http://www.alphawebprogramming.blogspot.com
              [email protected]
              Skype:[email protected]
              1 914 924 5171

              Comment


                #8
                Re: Accessing an object's property by string in Xbasic?

                Gandhi, I believe David mentioned that he can get at his pointer variable after a json_parse. I don't think that's his issue. He wants a variable way to get at his data.

                David, I believe the eval() function would be useful. It lets you put together strings and variables and evaluates it.

                Code:
                dim strObj as p = {"InsidePickup":{"type":"flat","rate":6550},"LiftgateOrigin":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessPickup":{"type":"flat","rate":8500},"ResidentialPickup":{"type":"flat","rate":5515},"TradeshowPickup":{"type":"flat","rate":7500},"DeliveryAppointment":{"type":"flat","rate":5550},"InsideDelivery":{"type":"perhundred","rate":5400},"LiftgateDestination":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessDelivery":{"type":"flat","rate":2500},"NotifyBeforeDelivery":{"type":"flat","rate":4000},"ResidentialDelivery":{"type":"flat","rate":5515},"TradeshowDelivery":{"type":"flat","rate":7500},"ProtectFreezing":{"type":"perhundred","rate":2005},"MarkingTagging":{"type":"minmax","rate":15000,"min":50050,"max":60005},"SingleShipment":{"type":"perhundred","rate":2400},"Hazmat":{"type":"minmax","rate":1500,"min":10900,"max":29900}}
                
                dim rawArray as C = "[\"InsidePickup\",\"LiftgateOrigin\",\"LimitedAccessPickup\",\"ResidentialPickup\",\"TradeshowPickup\",\"DeliveryAppointment\",\"InsideDelivery\",\"LiftgateDestination\",\"LimitedAccessDelivery\",\"NotifyBeforeDelivery\",\"ResidentialDelivery\",\"TradeshowDelivery\",\"ProtectFreezing\",\"MarkingTagging\",\"SingleShipment\",\"Hazmat\"]"
                dim accArray[0] as C
                accArray.initialize(comma_to_crlf(strtran(substr(rawArray, 2, len(rawArray) - 2), "\"")))
                
                dim myRate as c
                
                for i = 1 to accArray.size()
                	myRate = eval("strObj." + accArray[i] + ".rate")
                next i

                Comment


                  #9
                  Re: Accessing an object's property by string in Xbasic?

                  yes, now i see.
                  thanks for reading

                  gandhi

                  version 11 3381 - 4096
                  mysql backend
                  http://www.alphawebprogramming.blogspot.com
                  [email protected]
                  Skype:[email protected]
                  1 914 924 5171

                  Comment


                    #10
                    Re: Accessing an object's property by string in Xbasic?

                    Originally posted by Davidk View Post
                    Gandhi, I believe David mentioned that he can get at his pointer variable after a json_parse. I don't think that's his issue. He wants a variable way to get at his data.

                    David, I believe the eval() function would be useful. It lets you put together strings and variables and evaluates it.

                    Code:
                    dim strObj as p = {"InsidePickup":{"type":"flat","rate":6550},"LiftgateOrigin":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessPickup":{"type":"flat","rate":8500},"ResidentialPickup":{"type":"flat","rate":5515},"TradeshowPickup":{"type":"flat","rate":7500},"DeliveryAppointment":{"type":"flat","rate":5550},"InsideDelivery":{"type":"perhundred","rate":5400},"LiftgateDestination":{"type":"minmax","rate":2000,"min":15900,"max":39900},"LimitedAccessDelivery":{"type":"flat","rate":2500},"NotifyBeforeDelivery":{"type":"flat","rate":4000},"ResidentialDelivery":{"type":"flat","rate":5515},"TradeshowDelivery":{"type":"flat","rate":7500},"ProtectFreezing":{"type":"perhundred","rate":2005},"MarkingTagging":{"type":"minmax","rate":15000,"min":50050,"max":60005},"SingleShipment":{"type":"perhundred","rate":2400},"Hazmat":{"type":"minmax","rate":1500,"min":10900,"max":29900}}
                    
                    dim rawArray as C = "[\"InsidePickup\",\"LiftgateOrigin\",\"LimitedAccessPickup\",\"ResidentialPickup\",\"TradeshowPickup\",\"DeliveryAppointment\",\"InsideDelivery\",\"LiftgateDestination\",\"LimitedAccessDelivery\",\"NotifyBeforeDelivery\",\"ResidentialDelivery\",\"TradeshowDelivery\",\"ProtectFreezing\",\"MarkingTagging\",\"SingleShipment\",\"Hazmat\"]"
                    dim accArray[0] as C
                    accArray.initialize(comma_to_crlf(strtran(substr(rawArray, 2, len(rawArray) - 2), "\"")))
                    
                    dim myRate as c
                    
                    for i = 1 to accArray.size()
                    	myRate = eval("strObj." + accArray[i] + ".rate")
                    next i
                    That's perfect! That will work for what I need.

                    Thanks for all the help, and for sticking with me while I attempted to describe my issue, lol.

                    Much appreciated!

                    Comment


                      #11
                      Re: Accessing an object's property by string in Xbasic?

                      Good to hear... glad it worked out. I can see that based on the rate type you'll be able to go after specifics of the rate... if there are any beyond 'rate'. Perfect for a Select...Case statement.

                      Comment


                        #12
                        Re: Accessing an object's property by string in Xbasic?

                        the only reason you have an array in js format is to use it to iterate thru the object via xbasic ( you have a json object, you have an array containing the parent elements of the json objects), then if you change anything in the json list you will need to update the array too. instead you can actually use the object's property to get the array like this:
                        Code:
                        dim jst as c
                        jst = <<%str%
                        {
                        	"InsidePickup":{"type":"flat","rate":6550},
                        	"LiftgateOrigin":{"type":"minmax","rate":2000,"min":15900,"max":39900},
                        	"LimitedAccessPickup":{"type":"flat","rate":8500},
                        	"ResidentialPickup":{"type":"flat","rate":5515},
                        	"TradeshowPickup":{"type":"flat","rate":7500},
                        	"DeliveryAppointment":{"type":"flat","rate":5550},
                        	"InsideDelivery":{"type":"perhundred","rate":5400},
                        	"LiftgateDestination":{"type":"minmax","rate":2000,"min":15900,"max":39900},
                        	"LimitedAccessDelivery":{"type":"flat","rate":2500},
                        	"NotifyBeforeDelivery":{"type":"flat","rate":4000},
                        	"ResidentialDelivery":{"type":"flat","rate":5515},
                        	"TradeshowDelivery":{"type":"flat","rate":7500},
                        	"ProtectFreezing":{"type":"perhundred","rate":2005},
                        	"MarkingTagging":{"type":"minmax","rate":15000,"min":50050,"max":60005},
                        	"SingleShipment":{"type":"perhundred","rate":2400},
                        	"Hazmat":{"type":"minmax","rate":1500,"min":10900,"max":29900}
                        	}
                        %str%
                        
                        dim pp as p
                        pp = json_parse(jst)
                        txt = properties_enum(pp)
                        jstArray = *string_to_array(txt)
                        
                        dim j as n
                        dim myRate as c
                        
                        for j = 1 to jstArray.size()
                        myRate = eval("pp." + jstArray[j] + ".rate")
                        next j
                        you do not need a separate listing for the array, unless there is more to it than it seems to me here.

                        disclaimer: this is tested in version 11, 4 years old product.
                        thanks for reading

                        gandhi

                        version 11 3381 - 4096
                        mysql backend
                        http://www.alphawebprogramming.blogspot.com
                        [email protected]
                        Skype:[email protected]
                        1 914 924 5171

                        Comment


                          #13
                          Re: Accessing an object's property by string in Xbasic?

                          yes. you can use .data()

                          eg.

                          dim p as p
                          p.name = "fred"
                          ?p.name
                          = "fred"
                          ?p.data("name")
                          = "fred"

                          Comment


                            #14
                            Re: Accessing an object's property by string in Xbasic?

                            Nice...

                            Code:
                            for i = 1 to accArray.size()
                            	myRate = strObj.data(accArray[i] + ".rate")
                            next i
                            I'm going to ask Sarah to add this to the "Pointer and Dot Variables" section of the Documentation System

                            Comment


                              #15
                              Re: Accessing an object's property by string in Xbasic?

                              For example a5_mergeDataIntoTeplate() use .data() and it is documented there.

                              Comment

                              Working...
                              X