Link to home
Start Free TrialLog in
Avatar of cwigley
cwigley

asked on

OO Question

 I am relatively new to the app programming in .net. That being said I was wondering if someone could help me out with a OO related question.
 
Code in question. I have deleted out the non-relevant functions.

LOG.VB
Imports System.Data.OleDb
Imports System.IO

Public Class log

    Private sql As String
    Private command As OleDbCommand
    Private conn As OleDbConnection
    Private reader As OleDbDataReader
    Private pst_connection As String
    Private job_id As Integer
    Private job_instance As Integer

   Private Sub getConnection()
        conn = New OleDb.OleDbConnection(pst_connection)
        conn.Open()
    End Sub

    Function getnext()
        getConnection()

        '--- Clean-up ---
        sql = " DELETE pst_job_log WHERE job_instance_id IN (SELECT DISTINCT (job_instance_id)  FROM pst_job_log WHERE stamp < SYSDATE - 60)"
        command = New OleDbCommand(sql, conn)
        command.ExecuteNonQuery()

        '--- Get ID ---
        sql = " SELECT job_instance_id.NEXTVAL FROM DUAL"
        command = New OleDbCommand(sql, conn)
        reader = command.ExecuteReader(CommandBehavior.CloseConnection)
        reader.Read()
        getnext = reader.GetValue(0)
        reader.Close()
    End Function

    Function setup(ByVal file_name As String, ByVal jobid As Integer)
        Dim files As New file_functions()
        pst_connection = files.read_tf(file_name)
        job_id = jobid
        job_instance = getnext()
    End Function

    Function add(ByVal message As String)
        getConnection()

        '--- Add Message ---
        message = Replace(message, "'", "''")
        sql = " INSERT INTO PST.PST_JOB_LOG ( JOB_ID, STAMP, MESSAGE,  MESSAGE_ID, JOB_INSTANCE_ID, TIME) "
        sql = sql & " VALUES (" & job_id & ",sysdate,'" & message & "' ,msg_id.nextval," & job_instance & ",to_char(sysdate, 'hh:mi:ss'))"
        command = New OleDbCommand(sql, conn)
        command.ExecuteNonQuery()

        command.Dispose()
        conn.Close()
    End Function
End Class


FILE_FUNCTIONS.VB
Imports System.IO

Public Class file_functions
..........
    Function check(ByVal filename As String, ByVal minutes As Integer)
        Dim lf As New log()
       lf.add("File check...") <----- "Connection string not initialized."
    ................................
    End Function

End Class

I have this compiled into a .dll. When I call the check function I get Connection string not initialized. I am sure I am just missing a concept here and was wondering if someone could offer an explanation.
Avatar of bramsquad
bramsquad
Flag of United States of America image

it relates to this statement

pst_connection = files.read_tf(file_name)

you dont have a valid connection string thats being passed to your connection.....from the looks of it (youre omitting the read_tf() function which would tell us what your connection string looks like......but im under the assumption you are just returning a filepath.

check out www.connectionstrings.com

this should lead you in the right place to initialize your connection.


Avatar of cwigley
cwigley

ASKER

If I instantiate the log class iteself then the connection string initializes just fine.

Testing Example:
vbscript

This works just fine.
'----------------------
const job_id=15
set lf=createobject("script_tools_v2.log")
set file=createobject("script_tools_v2.file_functions")
lf.setup "c:\scripts\pst_connection.txt", job_id
lf.add "This is a test of the .net message logging dll."

If I then add the next line I get my connection string could not be initialized. I figure this is because the values that the function setup calls are not being referenced. This next line will work if I take the call the to add function out of the code. So it is blowing on trying to call the add function from within the file_functions class.

file.check("filename.txt")
what is the value of the variable pst_connection at the time you get the error message?

AW
Avatar of cwigley

ASKER

It is empty. How can I pass this value from the log class to the file_functions class? I am trying to keep from having to pass in this value everytime I want to write to the db from another function.
Avatar of cwigley

ASKER

 Ok. I don't how good a pratice this is but I created a module and declared the pst_connection as a public variable(Global). So when I call the setup function it gets populated and persists the value through the various classes. I will still award points if someone can show me a better way to handle this.
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial