By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

array of objects

P: 5
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!!!

Expand|Select|Wrap|Line Numbers
  1. function fHolidays(holidays, date, state) {
  2.     this.holidays = holidays;
  3.     this.date = date;
  4.     this.state = state;
  5. }
  6.  
  7. // store each object in an 'array of objects':
  8. var myHolidays = new Array();
  9. var jsHolidaysList = getADOObject("EXECUTE pHolidaysList");
  10. iCount = jsHolidaysList.recordCount;
  11. for (i=0; i<iCount; i++) {
  12.     myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
  13.     Response.write("Holidays: " + myHolidays[i].holidays + "<br>");
  14.     Response.write("Date: " + myHolidays[i].date + "<br>");
  15.     Response.write("State: " + myHolidays[i].state + "<br>");
  16.     Response.write("i: " + [i] + "<br>");
  17.     jsHolidaysList.moveNext();
  18. }
  19. jsHolidaysList.close();
  20. jsHolidaysList = null;
  21. Response.write("Herewego: " + myHolidays[0].holidays);
  22.  
Nov 27 '06 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 1,892
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!!!

Expand|Select|Wrap|Line Numbers
  1. function fHolidays(holidays, date, state) {
  2.     this.holidays = holidays;
  3.     this.date = date;
  4.     this.state = state;
  5. }
  6.  
  7. // store each object in an 'array of objects':
  8. var myHolidays = new Array();
  9. var jsHolidaysList = getADOObject("EXECUTE pHolidaysList");
  10. iCount = jsHolidaysList.recordCount;
  11. for (i=0; i<iCount; i++) {
  12.     myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
  13.     Response.write("Holidays: " + myHolidays[i].holidays + "<br>");
  14.     Response.write("Date: " + myHolidays[i].date + "<br>");
  15.     Response.write("State: " + myHolidays[i].state + "<br>");
  16.     Response.write("i: " + [i] + "<br>");
  17.     jsHolidaysList.moveNext();
  18. }
  19. jsHolidaysList.close();
  20. jsHolidaysList = null;
  21. Response.write("Herewego: " + myHolidays[0].holidays);
  22.  
Should

Expand|Select|Wrap|Line Numbers
  1. Response.write("Herewego: " + myHolidays[0].holidays);
Be

Expand|Select|Wrap|Line Numbers
  1. Response.write("Herewego: " + myHolidays[i].holidays);
Nov 28 '06 #2

P: 5
Should

Expand|Select|Wrap|Line Numbers
  1. Response.write("Herewego: " + myHolidays[0].holidays);
Be

Expand|Select|Wrap|Line Numbers
  1. Response.write("Herewego: " + myHolidays[i].holidays);

Even though
Expand|Select|Wrap|Line Numbers
  1. 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!
Nov 28 '06 #3

iam_clint
Expert 100+
P: 1,208
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.
Nov 28 '06 #4

P: 5
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!
Nov 28 '06 #5

iam_clint
Expert 100+
P: 1,208
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?
Nov 28 '06 #6

P: 5
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 (!jsHolidaysList.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 (!jsHolidaysList.eof)" to the beginning of the statement, the data disappears again. I'm so confused...


Expand|Select|Wrap|Line Numbers
  1. function getADOObject(theSQL, theDSN, theConnectionObject) {
  2.  if (theDSN == null) theDSN = Session("Connection");
  3.  if (theConnectionObject == null) theConnectionObject = "ConnectionObject";
  4.   var theConnection = new ActiveXObject("ADODB.connection");
  5.  if (!theConnection.state) try {
  6.   theConnection.open(theDSN);
  7.  } catch(e) {
  8.   pushError(e, "  Cannot connect to database.");
  9.   return null;
  10.  }
  11.  try {
  12.   var theADO = new ActiveXObject("ADODB.recordset");
  13.   theADO.open(theSQL, theConnection, 3);
  14.   return theADO;
  15.  } catch(e) {
  16.   pushError(e, theSQL);
  17.   return null;
  18.  }
  19. }
Nov 28 '06 #7

P: 5
Okay, I've figured out the problem (with help from a friend). It was actually very simple. I needed to change
Expand|Select|Wrap|Line Numbers
  1. myHolidays[i] = new fHolidays(jsHolidaysList("Holidays"), jsHolidaysList("Date"), jsHolidaysList("State"));
  2.  
to:
Expand|Select|Wrap|Line Numbers
  1. myHolidays[i] = new fHolidays(jsHolidaysList("Holidays").value, jsHolidaysList("Date").value, jsHolidaysList("State").value);
  2.  
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!

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 (!jsHolidaysList.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 (!jsHolidaysList.eof)" to the beginning of the statement, the data disappears again. I'm so confused...


Expand|Select|Wrap|Line Numbers
  1. function getADOObject(theSQL, theDSN, theConnectionObject) {
  2.  if (theDSN == null) theDSN = Session("Connection");
  3.  if (theConnectionObject == null) theConnectionObject = "ConnectionObject";
  4.   var theConnection = new ActiveXObject("ADODB.connection");
  5.  if (!theConnection.state) try {
  6.   theConnection.open(theDSN);
  7.  } catch(e) {
  8.   pushError(e, "  Cannot connect to database.");
  9.   return null;
  10.  }
  11.  try {
  12.   var theADO = new ActiveXObject("ADODB.recordset");
  13.   theADO.open(theSQL, theConnection, 3);
  14.   return theADO;
  15.  } catch(e) {
  16.   pushError(e, theSQL);
  17.   return null;
  18.  }
  19. }
Nov 29 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.