Syntax help with dynamic class properties

D

Dan

Is it possible to dynamically create and populate properties? Below is
my class. I would like to define properties for each of the items in my
two arrays without actually defining each one. (There are over 100 in
total). I would to have them set up the same as i have "form_id" below,
but im not sure what the syntax to do it is (without actually typing
each one).

' declare form_id
private form_id


' set get/let properties for form_id ------------------------
Public Property Get form_id()
form_id = m_form_id
End Property
Public Property Let form_id(p_Data)
m_form_id = p_Data
session("form_id") = p_Data
End Property

'
-----------------------------------------------------------------------
' below this point is where i need help

'declare the dynamic properties
private aryTableA
private aryTableB

' the values in these arrays are the field names in the db tables.
aryTableA = array("dog","cat","bird")
aryTableB = array("pizza","hamburger","hotdog")


For $i = 0 To ubound(aryTableA)
private m_ & $i ' if this can't be declared dynamically thats
ok
public property Get $i()
$i = m_ & i
end property
public property Let $i(p_Data)
m_ & $i = p_Data
end property
Next


' ------------------------------------------
' and from here, i im just not sure how to use the variable in my
update statement



public function saveChanges
...
For i = 0 to ubound(aryTableA)
strSQL = strSQL & "; UPDATE table_A set " & i & " = '" & m_ &
$i & "' WHERE form_id = " & m_form_id
....
Next
...
end function
 
M

Mark J. McGinty

Dan said:
Is it possible to dynamically create and populate properties? Below is
my class. I would like to define properties for each of the items in my
two arrays without actually defining each one. (There are over 100 in
total). I would to have them set up the same as i have "form_id" below,
but im not sure what the syntax to do it is (without actually typing
each one).

' declare form_id
private form_id


' set get/let properties for form_id ------------------------
Public Property Get form_id()
form_id = m_form_id
End Property
Public Property Let form_id(p_Data)
m_form_id = p_Data
session("form_id") = p_Data
End Property

'
-----------------------------------------------------------------------
' below this point is where i need help

'declare the dynamic properties
private aryTableA
private aryTableB

' the values in these arrays are the field names in the db tables.
aryTableA = array("dog","cat","bird")
aryTableB = array("pizza","hamburger","hotdog")


For $i = 0 To ubound(aryTableA)
private m_ & $i ' if this can't be declared dynamically thats
ok
public property Get $i()
$i = m_ & i
end property
public property Let $i(p_Data)
m_ & $i = p_Data
end property
Next


' ------------------------------------------
' and from here, i im just not sure how to use the variable in my
update statement



public function saveChanges
...
For i = 0 to ubound(aryTableA)
strSQL = strSQL & "; UPDATE table_A set " & i & " = '" & m_ &
$i & "' WHERE form_id = " & m_form_id
....
Next
...
end function

I'm not going to pretend I quite get what you're trying to do, if the whole
point of the class is to generate a SQL UPDATE statement, then my initial
take is you've massively over-thought the paradigm. :) Also I don't think
its legal to use ordinals to specify target fields in an update statement
(but that's beside the point.)

I think a 2-dimension array would be a better fit, and the code would be way
easier to read. The array can be redimensioned dynamically with ReDim, and
the field assignments could be generated in a loop.

But design concerns aside, the literal answer to your question is, the
Execute function will execute dynamically generated VBS code, as if that
code statically existed at file scope in the script. (I hesitate to offer
that information, though... the design you've described is rather
frightening, and I'd hate to be responsible for helping you to further it.)
:)

-Mark
 
A

Anthony Jones

Dan said:
Is it possible to dynamically create and populate properties? Below is
my class. I would like to define properties for each of the items in my
two arrays without actually defining each one. (There are over 100 in
total). I would to have them set up the same as i have "form_id" below,
but im not sure what the syntax to do it is (without actually typing
each one).

' declare form_id
private form_id


' set get/let properties for form_id ------------------------
Public Property Get form_id()
form_id = m_form_id
End Property
Public Property Let form_id(p_Data)
m_form_id = p_Data
session("form_id") = p_Data
End Property

'
-----------------------------------------------------------------------
' below this point is where i need help

'declare the dynamic properties
private aryTableA
private aryTableB

' the values in these arrays are the field names in the db tables.
aryTableA = array("dog","cat","bird")
aryTableB = array("pizza","hamburger","hotdog")


For $i = 0 To ubound(aryTableA)
private m_ & $i ' if this can't be declared dynamically thats
ok
public property Get $i()
$i = m_ & i
end property
public property Let $i(p_Data)
m_ & $i = p_Data
end property
Next


' ------------------------------------------
' and from here, i im just not sure how to use the variable in my
update statement



public function saveChanges
...
For i = 0 to ubound(aryTableA)
strSQL = strSQL & "; UPDATE table_A set " & i & " = '" & m_ &
$i & "' WHERE form_id = " & m_form_id
....
Next
...
end function

Dan,

I agree with Mark this whole approach seems somewhat ill conceived. The
problem here is that whilst VBscript offers a Class construct its usefulness
is very limited in the ASP context since objects created from them do not
survive between requests.

Also whilst Mark didn't directly state it the approach appears dangerous
since it seems likely that a lot of what you are concatenating in to a SQL
string will have originated on the client. This leads to SQL Injection
security to problems.

My advice would be to go and learn XML. Use XML as the medium in which the
state of your object is held. Have it updated client side and post it to
the server via XMLHTTP (some people call this AJAX but it isn't). Learn SQL
Server's OPENXML feature and have an SP simply take the XML posted from the
client to perform the update.

If client side Javascript is out of the question then I'd still recommend
building XML from Form fields serverside and then passing the XML to SQL
server.

Anthony.
 
D

Dan

Thank you both for your responses.
(and yes, that does sound exactly like ajax without the javascript)
Allow me to clear up my situation.

This is a fairly small app and is for internal office use only. It is
also something that I am trying to spit out fairly quickly and don't
plan on working with it ever again. This is being written as a favor
for someone (who understands that this is not production quality code,
and that i am not an asp programmer by any means).

Here is the overall design of the program.
-- the application view --
1.An Access db holds all the data in 3 tables. (i wont go into the
design here, as it does not seem too important).
2. A web page allows you to create a new entry into the database (which
is spread between the 3 tables).

-- the code view -- (editing an existing entry)

1. The user picks an entry to edit. (select form_id from...)
2. The code will create a new class, and store the form_id in it.
myclass.form_id = Request.form("form_id")
3. The class retrieves the the data from the database.
myclass.populateFromDB()
which selects all the data from the 3 tables WHERE form_id =
myclass.form_id
Now the class is populated with values from the db.

4. now in asp i can change the values for the class properties to
whatever. The person coding here never has to directly query the db,
they just play with the class.

5. if all is well, they can run myclass.updateDB()
for each property, we check to see if the value has changed. If it
has changed, we run and update statement in sql.


I know that this method has flaws. I'm not shooting for performance
here, (and for that matter not security either). In the code, the
database connection so far is transparent. Working on the actual asp
pages I see function calls to a few classes and not much more. So long
story short, im trying to make the class handle any database activity,
and the coder deals with just the class.

This is all working well and good for a table with 10 fields. However,
there are over 100 fields (95% of them are yes/no values) bewteen these
3 tables.

just defining the Let and Get properties is bringing the class file
close to 1M in size.

If i were starting from the begining I would welcome different ideas,
but for right now the only thing causing a problem is the actual number
of fields that need to be updated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top