Solved

How to use property defined in other class

Posted on 2004-07-30
22
198 Views
Last Modified: 2010-04-23
I am pretty new in VB.NET world, could anybody tell me how to use the property defined in class A, while my coding is in module B?
0
Comment
Question by:cocoWang
  • 13
  • 9
22 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 11677461
You would need to declare an instance of class A in module B.

Presuming you have a class called classA which has a property named TheProperty, the code in module B would look something like this:

dim clsA as New classA

clsA.TheProperty = "Whatever value it takes"
0
 

Author Comment

by:cocoWang
ID: 11678026
Sorry my situation probably is quite complicated, let me try to explain more clearly, before I know how to use the property defined in class A, while  my coding is in module B, could you tell me if I have a local variable call 'path' defined in the sub main() within module 1, and I need to retrieve the value of path in sub called openconnection() in another class. I don't know if this is workable. If it does, then how can I retrieve its value? Somehow this two questions are related with what I am doing now, thank you very much for your help!
0
 
LVL 10

Expert Comment

by:Clif
ID: 11678159
The value of Path in the sub called openconnection() would have to be returned by openconnection(), also openconnection() would have to be a Public function in your class.

If possible...
what is the name of the class you refer to as class A?

Show me your declaration for openconnection() in your class.

Show me the return code for the openconnection() function.

Given the above information (and presuming I understand what you're looking to do), I can either show you what you need in main(), or I can point you in the right direction.
0
 

Author Comment

by:cocoWang
ID: 11678486
I have one public module called 'DirectorySearch' and a Public class called 'OCDbManager' The project that I am working on is to search the *.dbf db III table and read the record and then transfer data into SQL Server table, Let me cut and paste my coding may be you can easily pin point what is the problem, thank you very much for your time and help.
Imports System
Imports System.IO
Imports Microsoft.Data.Odbc
Public Module DirectorySearch

    Public Overloads Sub Main(ByVal args() As String)

        Dim path As String
        For Each path In args
            If System.IO.File.Exists(path) Then
                ' This path is a file
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub

    ' Process all files in the directory passed in, and recurse on any directories
    ' that are found to process the files they contain


    Public Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory, "ars_led.dbf")
        ' Process the list of files found in the directory
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)
        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    Public Sub ProcessFile(ByVal path As String)
       
        Dim connectionToDBFfile As New OCDbManager
        connectionToDBFfile.DBConnectionString = "Provider=Microsoft.Jet.ODBC.4.0;" & _
            " DataSource = " & path
        connectionToDBFfile.OpenConnection()
        connectionToDBFfile.getExpenses()

    End Sub
 
    Public Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
        Try
            System.IO.Directory.Move(sourcePath, destinationPath)
            System.Console.WriteLine("The directory move is complete.")
        Catch exp As ArgumentNullException
            System.Console.WriteLine("Path is a null reference.")
        Catch exp As System.Security.SecurityException
            System.Console.WriteLine("The caller does not have the " + _
                                       "required permission.")
        Catch exp As ArgumentException
            System.Console.WriteLine("Path is an empty string, " + _
                                    "contains only white spaces, " + _
                                    "or contains invalid characters.")
        Catch exp As System.IO.IOException
            System.Console.WriteLine("An attempt was made to move a " + _
                                    "directory to a different " + _
                                    "volume, or destDirName " + _
                                    "already exists.")
        End Try


    End Sub
End Module
Imports System.Data
Imports System.Data.Common
Imports Microsoft.Data.Odbc

Public Class OCDbManager
    Friend logFile As New Logger
    Dim da As IDataAdapter
    Dim ds As New DataSet
    Dim ocDataSet As New DataSet

    Dim dbConnection As New OdbcConnection(connectionString)

    Dim connectionString As String
    Dim sourceDbDirectoryValue As String

    Public Property SourceDbDirectory() As String
        Get
            Return Me.sourceDbDirectoryValue
        End Get

        Set(ByVal newSourceDbDirectoryValue As String)
            Me.sourceDbDirectoryValue = newSourceDbDirectoryValue
        End Set

    End Property

    Public Property DBConnectionString() As String
        Get
            Return Me.connectionString
        End Get

        Set(ByVal newConnectionString As String)
            connectionString = newConnectionString
        End Set

    End Property

    Public Sub New()
        '.....
        'Add Code to get con string from properties file.
        '.....
    End Sub

    Public Sub New(ByVal newConnectionString As String)

        Me.connectionString = newConnectionString
        Try
            dbConnection = New OdbcConnection(Me.connectionString)
            dbConnection.Open()
        Catch odbcException As OdbcException
            Dim errIndex As Integer
            Dim errMessage As String
            For errIndex = 0 To odbcException.Errors.Count - 1
                errMessage = "Index #" + errIndex.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Message: " + odbcException.Errors(errIndex).Message + ControlChars.CrLf
                errMessage = errMessage & "Native: " + odbcException.Errors(errIndex).NativeError.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Source: " + odbcException.Errors(errIndex).Source + ControlChars.CrLf
                errMessage = errMessage & "SQL: " + odbcException.Errors(errIndex).SQLState + ControlChars.CrLf
            Next errIndex

            MessageBox.Show(errMessage)
        End Try

    End Sub
    Public Sub OpenConnection()

        'Me.connectionString = "DRIVER={Microsoft FoxPro VFP Driver (*.dbf)};UID=;Deleted = Yes;Null = Yes;Collate = Machine;BackgroundFetch = Yes;Exclusive = No;SourceType = DBF;SourceDB=" + Me.sourceDbDirectoryValue
        'Me.connectionString = "Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path
        Try
            dbConnection = New OdbcConnection(Me.connectionString)
            dbConnection.Open()
        Catch odbcException As OdbcException
            Dim errIndex As Integer
            Dim errMessage As String
            For errIndex = 0 To odbcException.Errors.Count - 1
                errMessage = "Index #" + errIndex.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Message: " + odbcException.Errors(errIndex).Message + ControlChars.CrLf
                errMessage = errMessage & "Native: " + odbcException.Errors(errIndex).NativeError.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Source: " + odbcException.Errors(errIndex).Source + ControlChars.CrLf
                errMessage = errMessage & "SQL: " + odbcException.Errors(errIndex).SQLState + ControlChars.CrLf
            Next errIndex

            Debug.WriteLine(errMessage)
        End Try

    End Sub
I did successfully search and found the dbf table, but the connection is the problem.
0
 

Author Comment

by:cocoWang
ID: 11678510
Sorry forgot to tell you that I comment out the two connectionstring inside sub Openconnection, I do need the connectionstring there, but I just don't know which one works or which one is the correct one.
0
 
LVL 10

Expert Comment

by:Clif
ID: 11678639
Ok, first off the second connection string is the one you want.

Second, you will have to create a "Path" property in OCDbManager

    Dim pathValue As String
    Public WriteOnly Property path()
        Set(ByVal Value)
            pathValue = Value
        End Set
    End Property

Finally, if you use the avove code, you'll have to change the connection string to use pathValue:

Me.connectionString = "Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path
0
 

Author Comment

by:cocoWang
ID: 11678753
Based on the site http://www.connectionstrings.com/ the you gave me, I put  following string within OpenConnection sub:

Me.connectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" & path & ";"

Then I got error mark on path, and this 'path' is just as I mentioned in the beginning of my question, which is defined in sub Main of DirectorySearch module, and now I got the problem to retrieve the value from OpenConnection sub within class OCDbManager.
0
 
LVL 10

Expert Comment

by:Clif
ID: 11678923
Read my comment of 07/30/2004 12:08PM EDT

You will have to pass "path" to the class.
0
 

Author Comment

by:cocoWang
ID: 11679332
Could you tell me the meaning of 'Dbq' in my connectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" & path & ";" ?

Is it the actual path of the dbf file? If yes, then I did get the value of path to be 'C:\msite\site.001\ars_led.dbf' which is exactly the file I am looking for, but I got exception errormessage as below

errMessage = "Index #2
Message:[Microsoft][ODBC dBase Driver] '(Unkown)'  is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
Native: -1023
Source:
SQL:HY024

Do you have any idea what went wrong? FYI, I copy the following coding from OCDbManager to module DirectorySearch.

Friend logFile As New Logger
    Dim da As IDataAdapter
    Dim ds As New DataSet
    Dim ocDataSet As New DataSet

    Dim dbConnection As New OdbcConnection(connectionString)

    Dim connectionString As String
    Dim sourceDbDirectoryValue As String

    Public Property SourceDbDirectory() As String
        Get
            Return Me.sourceDbDirectoryValue
        End Get

        Set(ByVal newSourceDbDirectoryValue As String)
            Me.sourceDbDirectoryValue = newSourceDbDirectoryValue
        End Set

    End Property

    Public Property DBConnectionString() As String
        Get
            Return Me.connectionString
        End Get

        Set(ByVal newConnectionString As String)
            connectionString = newConnectionString
        End Set

    End Property
Public Sub OpenConnection()

        'Me.connectionString = "DRIVER={Microsoft FoxPro VFP Driver (*.dbf)};UID=;Deleted = Yes;Null = Yes;Collate = Machine;BackgroundFetch = Yes;Exclusive = No;SourceType = DBF;SourceDB=" + Me.sourceDbDirectoryValue
        'Me.connectionString = "Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path
        Try
            dbConnection = New OdbcConnection(Me.connectionString)
            dbConnection.Open()
        Catch odbcException As OdbcException
            Dim errIndex As Integer
            Dim errMessage As String
            For errIndex = 0 To odbcException.Errors.Count - 1
                errMessage = "Index #" + errIndex.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Message: " + odbcException.Errors(errIndex).Message + ControlChars.CrLf
                errMessage = errMessage & "Native: " + odbcException.Errors(errIndex).NativeError.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Source: " + odbcException.Errors(errIndex).Source + ControlChars.CrLf
                errMessage = errMessage & "SQL: " + odbcException.Errors(errIndex).SQLState + ControlChars.CrLf
            Next errIndex

            Debug.WriteLine(errMessage)
        End Try

    End Sub


0
 
LVL 10

Expert Comment

by:Clif
ID: 11679489
dbq is the database name (data source)

Don't use the connection string you are attempting to use.  It is for ADO.

Instead, use the one for .net

"Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path

Did you create a path property in your class?  Are you passing the path to your class?

I can see by your last post you are not using a single line of code I have suggested.
0
 

Author Comment

by:cocoWang
ID: 11679799
I changed the connectionstring to be as what you said "Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path, and the path value is the correct path I need when I use the debugger, The error message I got this time is 'Data source name not found and no default driver specified' Do I need to insert DSN = xxxxx inside the connectionstring? My understanding is the driver is ODBC.4.0, or am I wrong about the driver?

The path I traced is 'C:\msite\ar\site.010\ars_lel.dbf',  No, I did not create a path property in my class, since I already got the right path value, I just use it as a parameter to pass to other functions.

Could you tell me what line of code that you have suggested that I did not use, I did use the correct connectstring as what you mentioned before I compile the application this time. I may misunderstood you message, could you tell me more, since I am pretty new to VB.NET world, I need time to pick up some basic sense and knowledge.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:cocoWang
ID: 11679892
Sorry forgot to ask you this, do you mean the dbq should be the dsn name and not the actual path of the dbf file? I did create several data source name through Administrative Tool within Control panel,  but each dsn was created for certain dbf files under certain subdirectory, and I have about 100 subdirectories under the 'c:\msite\ar' subdirectory.

 I don't believe I have to create 100 dsn for all subdirectories in order to connect to the dbf files under each subdirectory, am I right? If I am wrong, what is the right way to create those dsn so no matter which subdirectory, the ODBC can always find the right dsn to connect table data?
0
 
LVL 10

Expert Comment

by:Clif
ID: 11680003
You shouldn't have to create any dsn's.  The connection string is a dsn-less connection directly to the foxpro database folder.
0
 

Author Comment

by:cocoWang
ID: 11680132
That is good news that I should not have to create any dsn, but how why I got the error message 'Data source name not found and no default driver specified'?
0
 
LVL 10

Expert Comment

by:Clif
ID: 11680233
Data Source is two words:
"Provider=Microsoft.Jet.ODBC.4.0;DataSource = " & path
should be
"Provider=Microsoft.Jet.ODBC.4.0;Data Source = " & path


Does the path you say:
'C:\msite\ar\site.010\ars_lel.dbf'
Lead to a file or a folder?  It should be a folder
0
 

Author Comment

by:cocoWang
ID: 11680401
The path is 'C:\msite\ar\site.010\ars_lel.dbf', which is the file not the folder.

I created a variable called 'source', then use LEFT function and set source either ='c:\msite\ar\site.010' or source= 'c:\msite\ar\site.010\' and change the DataSource spelling to 'Data Source', the coding is as following :

 Public Sub ProcessFile(ByVal path As String)
        Dim source = Left(path, 21)  ' I alo used Left(path, 20)
        connectionString = "Provider=Microsoft.Jet.ODBC.4.0;Data Source = " & source
        OpenConnection()
        getExpenses()
End sub
Public Sub OpenConnection()

        Try
            dbConnection = New OdbcConnection(connectionString)
            dbConnection.Open()
        Catch odbcException As OdbcException
            Dim errIndex As Integer
            Dim errMessage As String
            For errIndex = 0 To odbcException.Errors.Count - 1
                errMessage = "Index #" + errIndex.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Message: " + odbcException.Errors(errIndex).Message + ControlChars.CrLf
                errMessage = errMessage & "Native: " + odbcException.Errors(errIndex).NativeError.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Source: " + odbcException.Errors(errIndex).Source + ControlChars.CrLf
                errMessage = errMessage & "SQL: " + odbcException.Errors(errIndex).SQLState + ControlChars.CrLf
            Next errIndex

            Debug.WriteLine(errMessage)
        End Try
End sub

I still got the error message 'Data source name not found and no default driver specified'?  Any other suggestions?
I already increase the point to 50 now, this takes too much of your time, and I really appreciate your help!
0
 

Author Comment

by:cocoWang
ID: 11680517
Clif,

Do you have email address that I can send you the dbf file, and all my project coding if that can help you easier and quicker to find out where is the problem and certainly if you agree to do so.

I would be more than happy to send them to you provided you agree, please let me know your opinion. Thanks !
0
 

Author Comment

by:cocoWang
ID: 11680649
If you do not want me to send those files to you, do you happen to have some sample coding about how to connect db III tables data throgh ODBC drivers? That could be also another way to help me solve this qusestion. What do you think?
0
 
LVL 10

Accepted Solution

by:
Clif earned 50 total points
ID: 11681079
The path needs to be the folder where the file resides, not the file itself.

You might want to google on "Foxpro" "oledb"

You can also check out this site:
http://www.pstruh.cz/tips/detpg_asp-dbf-database.htm

I'm sorry, but I don't post my email address.

I'm sorry to say it's go home time.

Good luck.
0
 

Author Comment

by:cocoWang
ID: 11693197
Thanks for your help Clif, I finally figured out a way with my coworker to make it work. Hopefully you had a great weekend quality family time!
0
 
LVL 10

Expert Comment

by:Clif
ID: 11693209
So?

How did you make it work?
0
 

Author Comment

by:cocoWang
ID: 11699860
The trick is the connectionstring, here is the coding that works:

Public Sub OpenConnection(ByVal source As String)

        Me.connectionString = "DRIVER={Microsoft FoxPro VFP Driver (*.dbf)};UID=;Deleted = Yes;Null = Yes;Collate = Machine;BackgroundFetch = Yes;Exclusive = No;SourceType = DBF;SourceDB=" + source  'Me.sourceDbDirectoryValue
        Try
            dbConnection = New OdbcConnection(Me.connectionString)
            dbConnection.Open()
        Catch odbcException As OdbcException
            Dim errIndex As Integer
            Dim errMessage As String
            For errIndex = 0 To odbcException.Errors.Count - 1
                errMessage = "Index #" + errIndex.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Message: " + odbcException.Errors(errIndex).Message + ControlChars.CrLf
                errMessage = errMessage & "Native: " + odbcException.Errors(errIndex).NativeError.ToString() + ControlChars.CrLf
                errMessage = errMessage & "Source: " + odbcException.Errors(errIndex).Source + ControlChars.CrLf
                errMessage = errMessage & "SQL: " + odbcException.Errors(errIndex).SQLState + ControlChars.CrLf
            Next errIndex

            Debug.WriteLine(errMessage)
        End Try

    End Sub

The source is the subdirectory where the db III table resides.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now