array of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shermola1
    New Member
    • Nov 2006
    • 5

    #1

    array of objects

    I'm trying to create an array of objects based on some code by DB McGee in an earlier post and I'm having issues. Once the for loop is complete, my objects no longer contain any data. To be more specific, there are five Response.write lines that I'm using for testing purposes. The first four all return data. The last one ("herewego:" ) does not. I'm sure I'm missing something stupid, but I can't waste any more time on this. There is no error, just emptyness. The code is below. Any help would be appreciated! Thanks!!!

    Code:
    function fHolidays(holidays, date, state) {
    	this.holidays = holidays;
    	this.date = date;
    	this.state = state;
    }
    
    // store each object in an 'array of objects':
    var myHolidays = new Array();
    var jsHolidaysList = getADOObject("EXECUTE pHolidaysList");
    iCount = jsHolidaysList.recordCount;
    for (i=0; i<iCount; i++) {
    	myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
    	Response.write("Holidays: " + myHolidays[i].holidays + "<br>");
    	Response.write("Date: " + myHolidays[i].date + "<br>");
    	Response.write("State: " + myHolidays[i].state + "<br>");
    	Response.write("i: " + [i] + "<br>");
    	jsHolidaysList.moveNext();
    }
    jsHolidaysList.close();
    jsHolidaysList = null;
    Response.write("Herewego: " + myHolidays[0].holidays);
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Originally posted by shermola1
    I'm trying to create an array of objects based on some code by DB McGee in an earlier post and I'm having issues. Once the for loop is complete, my objects no longer contain any data. To be more specific, there are five Response.write lines that I'm using for testing purposes. The first four all return data. The last one ("herewego:" ) does not. I'm sure I'm missing something stupid, but I can't waste any more time on this. There is no error, just emptyness. The code is below. Any help would be appreciated! Thanks!!!

    Code:
    function fHolidays(holidays, date, state) {
    	this.holidays = holidays;
    	this.date = date;
    	this.state = state;
    }
     
    // store each object in an 'array of objects':
    var myHolidays = new Array();
    var jsHolidaysList = getADOObject("EXECUTE pHolidaysList");
    iCount = jsHolidaysList.recordCount;
    for (i=0; i<iCount; i++) {
    	myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
    	Response.write("Holidays: " + myHolidays[i].holidays + "<br>");
    	Response.write("Date: " + myHolidays[i].date + "<br>");
    	Response.write("State: " + myHolidays[i].state + "<br>");
    	Response.write("i: " + [i] + "<br>");
    	jsHolidaysList.moveNext();
    }
    jsHolidaysList.close();
    jsHolidaysList = null;
    Response.write("Herewego: " + myHolidays[0].holidays);
    Should

    Code:
    Response.write("Herewego: " + myHolidays[0].holidays);
    Be

    Code:
    Response.write("Herewego: " + myHolidays[i].holidays);

    Comment

    • shermola1
      New Member
      • Nov 2006
      • 5

      #3
      Originally posted by AricC
      Should

      Code:
      Response.write("Herewego: " + myHolidays[0].holidays);
      Be

      Code:
      Response.write("Herewego: " + myHolidays[i].holidays);

      Even though
      Code:
      Response.write("Herewego: " + myHolidays[i].holidays);
      is outside of the loop? That doesn't make any sense to me. At that point the value of i will only be equal to the last value and therefore will only write one value. Since (in this case) there are three possible values, I need all three of them to write out. Plus, even though I specified the value of i, since that value exists, it should still display. Right?

      Sorry for the "stream of consciousness," but I'm just trying to work through this...

      Thanks again!

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        response.write is asp..
        document.write is javascript.

        You have a mixture of asp and javascript
        getADOObject is not javascript
        You cannot mix ASP and javascript in this fashion.

        Comment

        • shermola1
          New Member
          • Nov 2006
          • 5

          #5
          Originally posted by iam_clint
          response.write is asp..
          document.write is javascript.

          You have a mixture of asp and javascript
          getADOObject is not javascript
          You cannot mix ASP and javascript in this fashion.
          Cannot or should not? The response.write works everywhere *except* the last iteration ("herewego: "), so obviously it works with Javascript. getADOObject is a javascript function I've written, so it is javascript, but it is not part of the problem, it works fine - it gets the data from the datasource. The problem is that the array becomes empty after the "for loop." I use very similar code in other places very successfully, just not with the array of objects. Thanks for your efforts so far!

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            I have never seen a javascript work with response.write thats ASP, its really strange that it is working, are we missing some of the code?

            Comment

            • shermola1
              New Member
              • Nov 2006
              • 5

              #7
              Originally posted by iam_clint
              I have never seen a javascript work with response.write thats ASP, its really strange that it is working, are we missing some of the code?
              The only thing you're not seeing is the function behind getADOObject. I'll post that below, just so you can see it. Also, this is server-side javascript, not client-side. I assumed that was obvious, but I just want to be clear, just in case.

              I actually found one problem, fixed it and created another problem. If you look at the code, you will see that there is jsHolidaysList. moveNext(), but there is no if (!jsHolidaysLis t.eof) at the beginning. If I remove the moveNext statement, I get data in the final Response.write, but unfortunately it is only for the first record. If I add the "if (!jsHolidaysLis t.eof)" to the beginning of the statement, the data disappears again. I'm so confused...


              Code:
              function getADOObject(theSQL, theDSN, theConnectionObject) {
               if (theDSN == null) theDSN = Session("Connection");
               if (theConnectionObject == null) theConnectionObject = "ConnectionObject";
                var theConnection = new ActiveXObject("ADODB.connection");
               if (!theConnection.state) try {
                theConnection.open(theDSN);
               } catch(e) {
                pushError(e, "  Cannot connect to database.");
                return null;
               }
               try {
                var theADO = new ActiveXObject("ADODB.recordset");
                theADO.open(theSQL, theConnection, 3);
                return theADO;
               } catch(e) {
                pushError(e, theSQL);
                return null;
               }
              }

              Comment

              • shermola1
                New Member
                • Nov 2006
                • 5

                #8
                Okay, I've figured out the problem (with help from a friend). It was actually very simple. I needed to change
                Code:
                myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
                to:
                Code:
                myHolidays[i] = new fHolidays(jsHolidaysList("Holidays").value, jsHolidaysList("Date").value, jsHolidaysList("State").value);
                That was enough to fix the problem. I was passing the actual field object to the myHolidays constructor instead of the values.

                Thanks to everyone for their help!

                Originally posted by shermola1
                The only thing you're not seeing is the function behind getADOObject. I'll post that below, just so you can see it. Also, this is server-side javascript, not client-side. I assumed that was obvious, but I just want to be clear, just in case.

                I actually found one problem, fixed it and created another problem. If you look at the code, you will see that there is jsHolidaysList. moveNext(), but there is no if (!jsHolidaysLis t.eof) at the beginning. If I remove the moveNext statement, I get data in the final Response.write, but unfortunately it is only for the first record. If I add the "if (!jsHolidaysLis t.eof)" to the beginning of the statement, the data disappears again. I'm so confused...


                Code:
                function getADOObject(theSQL, theDSN, theConnectionObject) {
                 if (theDSN == null) theDSN = Session("Connection");
                 if (theConnectionObject == null) theConnectionObject = "ConnectionObject";
                  var theConnection = new ActiveXObject("ADODB.connection");
                 if (!theConnection.state) try {
                  theConnection.open(theDSN);
                 } catch(e) {
                  pushError(e, "  Cannot connect to database.");
                  return null;
                 }
                 try {
                  var theADO = new ActiveXObject("ADODB.recordset");
                  theADO.open(theSQL, theConnection, 3);
                  return theADO;
                 } catch(e) {
                  pushError(e, theSQL);
                  return null;
                 }
                }

                Comment

                Working...