Wednesday, March 2, 2016

Classic ASP - Using Arrays as in Lists in C#

We develop applications using different technologies. Some are new and some are not that new. In 90's asp was very famous before .Net comes out from Microsoft.

But still you find certain applications run in classic ASP & sometimes need fixes too.

Today we talk about how an Array of objects can be returned from a method so that it can be used anywhere in the application. As an example, look at the below code.

 this.PersonArray= new Array()  
 function SelPerson(idCompany) {    
   try {  
     this.PersonArray=new Array()  
     var l = this.PersonArray.length  
     var rst = new dalPerson().SelPerson(idCompany)  
     if (rst && rst.State && !rst.EOF)   
     {  
       while (!rst.EOF)   
       {  
         l = this.PersonArray.length  
         this.PersonArray[l] = new Object()  
         this.PersonArray[l].idPerson= rst.Fields("idPerson").Value  
         this.PersonArray[l].firstName= rst.Fields("firstName").Value  
         this.PersonArray[l].lastName= rst.Fields("lastName").Value  
         rst.moveNext()  
       }  
       rst.Close(); rst = null;  
     } else return false      
   } catch (e) { Response.Write("SelPerson: " + e.description); return false; }  
 }  

SelPerson is a method that fills the global "PersonArray" that can be accessible from anywhere where the class is referenced.

This method uses data access object "dalPerson" and its "SelPerson" method that returns a RecordSet. Looping through, we add the values to PersonArray and then close the recordset.

Wherever this method is used, can iterate through the array and get the work done as below;

 for (var n = 0; n < objPerson.PersonArray.length; n++) {  
   //var idPerson = objPerson.PersonArray[n].idPerson   
 }  

So don't bring your Recordset to the page and let them to be used only inside your DAL or at least on BLL.

Love the code. Not the technology.

Happy coding :)

No comments:

Post a Comment