Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

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.
0
cwigley
Asked:
cwigley
  • 3
  • 2
1 Solution
 
bramsquadCommented:
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.


0
 
cwigleyAuthor Commented:
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")
0
 
Arthur_WoodCommented:
what is the value of the variable pst_connection at the time you get the error message?

AW
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cwigleyAuthor Commented:
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.
0
 
cwigleyAuthor Commented:
 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.
0
 
Arthur_WoodCommented:
try this approach:

1) modify you log class, to have the setup function test to see if the connection has already been read - and only read it if it has not already been read:

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



then, in your File_Fiunctions 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.setup() ' only create a new connection if one does not already exist
       lf.add("File check...") <----- "Connection string not initialized."
    ................................
    End Function

End Class

as it was, since you create a NEW log object, it does NOT have an assocaited connection, hence the error.

AW

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now