Adding Custom Properties to Objects in Classic ASP

I the last blog post Converting Recordset into Objects in Classic ASP we created a public function initialize(rs) which takes in a single row from the database and creates a single class object account.  Our goal is to add a new property named fullname where we combine firstname and lastname

We will update the public function initialize(rs) as follows:

 

public function initialize(rs)

dim row
set row = New account

   row.id = rs("id")
   row.guid = rs("guid")
   row.lastName = rs("lastName")
   row.firstName = rs("firstName")
   row.fullname = rs("firstName") & ", " & rs("firstName") 
   row.email = rs("email")
   row.password = rs("password")
   row.passwordSalt = rs("passwordSalt")
   row.clientId = rs("clientId")
   row.administrator = rs("administrator")
   row.lastLogin = rs("lastLogin")
   row.created = rs("created")
   row.modified = rs("modified")
   row.active = rs("active")

set initialize = row

end function

Updated the UL code with a new LI item for fullname.

<%
Dim rsAccount
set rsAccount = getall()
%>

<ul>

<%
  While (NOT rsAccount.EOF)

  Dim account
  call account = initialize(rsAccount)

 %>
 
   <li><%=account.id%></li>
   <li><%=account.guid%></li>
   <li><%=account.lastname%></li>
   <li><%=account.firstname%></li>
   <li><%=account.fullname%></li>
   <li><%=account.email%></li>
   <li><%=account.password%></li>
   <li><%=account.passwordSalt%></li>
   <li><%=account.clientId%></li>
   <li><%=account.administrator%></li>
   <li><%=account.lastLogin%></li>
   <li><%=account.created%></li>
   <li><%=account.modified%></li>
   <li><%=account.active%></li>
 
 <% 
  rsAccount.MoveNext()
  Wend
 %>

</ul>

If you run the code as is, you will get an error Variable is undefined: 'fullname'

All we need to do is make sure the account Class a property defined for fullname.

Class account

  public id
  public guid
  public lastName
  public firstName
  public fullname 
  public email
  public password
  public passwordSalt
  public clientId
  public administrator
  public lastLogin
  public created
  public modified
  public active

End Class

Now your code should run without an error. You now have an account object with a property fullname.

If you are a looking for Classic ASP Experts or upgrading to .NET from Classic ASP , give us a call.

 

Converting Recordset into Objects in Classic ASP

Now that you know how to create a Class and Objects in Classic ASP, we will look at Converting Recordset into Objects in Classic ASP.

Lets create a Class account which has several properties. I usually use the database table account and copy the model so the Class matches the database model

Class account

  public id
  public guid
  public lastName
  public firstName
  public email
  public password
  public passwordSalt
  public clientId
  public administrator
  public lastLogin
  public created
  public modified
  public active

End Class

Here I am creating a getall function to return all the rows in the account table in the database.

public function getall()

   dim sql
   sql = "select * from account"

   dim rs
   set rs = conn.execute(sql)
   set getall = rs

end function

When I started with Classic ASP, I would have written a simple While Loop

<%
Dim rsAccount
set rsAccount = getall()
%>

<ul>

<%
  While (NOT rsAccount.EOF)
 %>
 
   <li><%=rsAccount("id")%></li>
   <li><%=rsAccount("guid")%></li>
   <li><%=rsAccount("lastname")%></li>
   <li><%=rsAccount("firstname")%></li>
   <li><%=rsAccount("email")%></li>
   <li><%=rsAccount("password")%></li>
   <li><%=rsAccount("passwordSalt")%></li>
   <li><%=rsAccount("clientId")%></li>
   <li><%=rsAccount("administrator")%></li>
   <li><%=rsAccount("lastLogin")%></li>
   <li><%=rsAccount("created")%></li>
   <li><%=rsAccount("modified")%></li>
   <li><%=rsAccount("active")%></li>
 
 <% 
  rsAccount.MoveNext()
  Wend
 %>

</ul>

The above code would write to the screen a <UL> foreach account row and <LI> foreach field/record in the database.

Object-Oriented Programming Approach

An OOP [Object-Oriented Programming] approach would be to initialize each account record using the account Class.

Instead of getting each data element using Recordset rsAccount("lastName"), we will use another function to map the Recordset row into our account object.

The public function initialize(rs) takes in a single row from the database and creates a single class object account.

 

public function initialize(rs)

dim row
set row = New account

   row.id = rs("id")
   row.guid = rs("guid")
   row.lastname = rs("lastname")
   row.firstname = rs("firstname")
   row.email = rs("email")
   row.password = rs("password")
   row.passwordSalt = rs("passwordSalt")
   row.clientId = rs("clientId")
   row.administrator = rs("administrator")
   row.lastLogin = rs("lastLogin")
   row.created = rs("created")
   row.modified = rs("modified")
   row.active = rs("active")

set initialize = row

end function

Updated code combining Recordset with Class and Initializing each row into an object.

<%
Dim rsAccount
set rsAccount = getall()
%>

<ul>

<%
  While (NOT rsAccount.EOF)

  Dim account
  set account = initialize(rsAccount)

 %>
 
   <li><%=account.id%></li>
   <li><%=account.guid%></li>
   <li><%=account.lastname%></li>
   <li><%=account.firstname%></li>
   <li><%=account.email%></li>
   <li><%=account.password%></li>
   <li><%=account.passwordSalt%></li>
   <li><%=account.clientId%></li>
   <li><%=account.administrator%></li>
   <li><%=account.lastLogin%></li>
   <li><%=account.created%></li>
   <li><%=account.modified%></li>
   <li><%=account.active%></li>
 
 <% 
  rsAccount.MoveNext()
  Wend
 %>

</ul>

If you are a looking for Classic ASP Experts or upgrading to .NET from Classic ASP , give us a call.

 

How to create a Class and Objects in Classic ASP

Creating Objects in programming is one the best ways to have code that is easily maintainable. With new Classic ASP clients, I have found creating Classes and Objects is a very good way to begin refactoring legacy code.

Class trip

    public arrive_date
    public depart_date

End Class

Here we have created a Class trip with 2 properties arrive_date and depart_date. Now, when we need to access the arrival date, you can access the values as

myobject.arrive_date
myobject.depart_date

Before we can access or use the trip Class, we need to create a new instance of a class. The newly instantiated trip Class, which is now an object with the properties, that we can use.

 

Class trip

    public arrive_date
    public depart_date

End Class



dim objTrip
set objTrip = new trip

objTrip.arrive_date = '02/11/2017'
objTrip.depart_date = '02/16/2017'

Now that we have created a new instance of our trip and assigned values to each property, we can write some simple HTML to retrieve the values

<p>

I will be arriving on <%=objTrip .arrive_date%> and leaving on <%=objTrip .depart_date%>

<p>

 

If you are a looking for Classic ASP Experts or upgrading to .NET from Classic ASP , give us a call.