creating a class for asp

Memnoch1207
07-25-2002, 09:57 PM
Not sure if this is the correct forum to post this in, but here goes.
On many web pages (asp pages) there are tables which display records from a database. you can set the amount of records you want displayed on the page. Any records over the amount specified to display would create additional pages. Exactly like this forum which displays more pages as there become more posts. Example: (Previous [1, 2, 3] Next)

I want to create a class in VB to do this very thing. It should be accessible from any asp page, by doing an SSI statement such as
<!--#include virtual="/dir1/subdir/file.asp" -->

This class should accept 2 strings, 1 integer and 1 long (this is about all I can think of right now, that it would need) as below

string1 would accept a table name from the database
string2 would accept a SQL statement
integer would accept # of records to be displayed per page
long would accept total records from table.

then you could create a method that would calculate how many page links (the [1, 2, 3] from above) would be displayed.

I know it more indepth than this I was just hoping someone could point me in the right direction to get started. Not sure if I should do it as an ActiveX DLL or what.

Thanks for the help!:D

Derek Stone
07-25-2002, 10:13 PM
There is no class support in VBScript. You'd have to create an ActiveX DLL and instantiate it as a COM object.

Memnoch1207
07-25-2002, 10:24 PM
can you provide any links as to how this might be set up...like I said I'm not familar with creating classes in VB

Never mind I just found what I was looking for...thanks for the info Lunatic...I appreciate it!

Rezner
07-26-2002, 04:39 PM
Originally posted by Crazed Lunatic
There is no class support in VBScript. You'd have to create an ActiveX DLL and instantiate it as a COM object. Actually, you can create your own classes in both VBScript and ASP:

Here's a simple example I came up with for ASP that deals with fractions (I couldn't think of anything else):<%
Class Fraction

Public Num 'numerator
Public Den 'denominator

Public Function SetParts(n,d)
Num = n
Den = d
End Function

Public Function GetDecimal(intPrecision)
'round the decimal to the desired precision
GetDecimal = Round(Num / Den, intPrecision)
End Function

Public Function AddFractions(fAdd)
'simple fraction addition formula:
(a/b) + (c/d) = ((a*d)+(b*c)) / (b*d)

'return a string representing the solution
AddFractions = _
Num & "/" & Den & " + " & fAdd.Num & "/" & fAdd.Den & " = " & _
((Num * fAdd.Den) + (Den * fAdd.Num)) & "/" & (Den * fAdd.Den)

End Function

End Class

Dim f,f2

Set f = New Fraction : f.SetParts 1,3

Set f2 = New Fraction: f2.SetParts 4,5

Response.write f.AddFractions(f2)

%>

Memnoch1207
07-26-2002, 04:58 PM
k here is the class I created could some of you review the code for me and see I there is anything I have missed or any syntax errors...( i keep getting errors when calling it from another asp page)

class dbpaging
private tableName
private sqlString
private recordsDisplayed
private totalRecords
private pageCount

sub class_initialize
recordsDisplayed = 1
totalRecords = 0
pageCount = 1
end sub


sub class_terminate

end sub


private function check_data()
if len(tableName) = 0 then
call err.raise(8888, "dbpaging Class", "Table name must be specified.")
end if

if len(sqlString) = 0 then
call err.raise(8888, "dbpaging Class", "A SQL query string must be specified.")
end if

check_data = true
end function



public sub set_table_name(tableNamePayload)
if len(tableNamePayload) > 0 then
tableName = CStr(tableNamePayload)
else
call err.raise(8888, "dbpaging Class", "The table name is invalid.")
end if
end sub


public sub set_sqlString(sqlStringPayload)
if len(sqlStringPayload) > 0 then
sqlString = CStr(sqlStringPayload)
else
call db_query("SELECT * FROM " & tableName)
end if
end sub



public sub set_recordsDisplayed(recordsDisplayedPayload)
if len(recordsDisplayedPayload) >= 1 AND IsNumeric(recordsDisplayedPayload) then
recordsDisplayed = CInt(recordsDisplayedPayload)
else
call err.raise(8888, "dbpaging Class", "Records per page quantity not valid.")
end if
end sub



private sub set_totalRecords()
on error resume next
if len(tableName) > 0 then
set totalRecordsRs = db_query("SELECT COUNT(*) AS 'count' FROM " & tableName)
totalRecords = totalRecordsRs("count")
else
call err.raise(8888, "dbpaging Class", "You must set the tableName property before continuing.")
end if
end sub


public function calculate_totalPages()
call set_totalRecords()

' check to make sure that recordsDisplayed has a value

pageCount = totalRecords / recordsDisplayed

'return pageCount
end function


public function get_page()
if len(sqlString) = 0 then
call db_query("SELECT * FROM " & tableName)
end if

if db_state = false then call err.raise(8888, "dbpaging Class", "Cannot return a recordset unless the database connection is open.")

set rs = db_query(sqlString)

set get_page = rs

end function

' call the move() property
'set D1 = Server.CreateObject("Scripting.Dictionary")
'set D2 = Server.CreateObject("Scripting.Dictionary")
public sub move()
do until rs.eof OR iter = recordsDisplayed
D1.removeall
for each field in rs.fields
D1.add field.name, field.value
next
D2.add iter, D1
iter = iter + 1
rs.movenext
loop
end sub

end class

Rezner
07-26-2002, 05:03 PM
You're not going to be able to call it from another ASP page unless you #include the source file in that page.

(This is because it's not an object on the server)

Memnoch1207
07-26-2002, 05:16 PM
I have called it from the other page (test.asp)
here is the include statement
<!--#include virtual="/core/classes/dbpaging.asp" -->

Rezner
07-26-2002, 05:24 PM
"dbpaging" is the name of both the class and the asp file?

I'd maybe rename "dbpaging.asp" to "dbpaging.inc" and make sure there is nothing else in it but that class

Derek Stone
07-26-2002, 07:10 PM
I don't know what I was thinking when I said there was no class support. Looks like I need to catch an extra hour of sleep tonight...

bbolte
08-02-2002, 09:42 AM
be careful about naming include files with the ".inc" extension, they are not secure and can be read directly through the browser window if someone knows the path. it is better to name them ".asp" as they are parsed at the server.

Rezner
08-02-2002, 09:45 AM
Originally posted by bbolte
be careful about naming include files with the ".inc" extension, they are not secure and can be read directly through the browser window if someone knows the path. it is better to name them ".asp" as they are parsed at the server. Good point.

However, if you want to use .inc files then you can place them above the web root directory (commonly "wwwroot") and then reference them with a local path vice a virtual path. The system will be able to access them, but web users will not be able to download them.

bbolte
08-02-2002, 09:48 AM
that's a good idea rezner, hadn't ever tried that.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum