Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a Class from a Windows Application

Status
Not open for further replies.

traceytr

Programmer
Mar 13, 2001
94
US
I’ve placed some logic (in progress) in a Windows Application Form because I’m new to .NET and I knew I could test the logic there. My goal is to place the same logic in an independent Class to be called from Windows Application.

Later, when I’m confident the logic in the Class is correct – I want to call the same Class from a Windows Service.

I’m totally new to VB.NET, and I stumbled on the syntax for creating the Class, even after reading through tutorials, MSDN, etc. I posted the dumbest question earlier, and there were two responses right away which got me past the issue. (Thanks again.)

I’ve hit another obstacle, and the references and examples I’m using don’t really address what I’m trying to accomplish.

Okay…

This is the code I have within my Windows Application Form1. I moved the logic into a Class and built the .DLL. Now I’d like to call the class from the Windows Application – from within the Sub myWatcher_Changed event where it was originally written – see below. (I’m using the FileSystemWatcher to watch a directory. Never mind the logic at this point. There are probably one hundred better ways to write it, but for now it seems to work -- famous last words, I know.)

My question is (finally) how do I execute the code which is now in a Class? I know this is a very long post -- thank you for taking the time to read it.


Code:
Private Sub myWatcher_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles myWatcher.Changed

'ALL OF THE CODE BELOW IS MOVED INTO THE CLASS, AND 
'I WANT TO CALL IT FROM HERE, 
'WITHIN THE myWatcher_Changed EVENT.

'MY QUESTION IS HOW DO I EXECUTE THE FOLLOWING 
'CODE WHICH IS NOW IN A CLASS.

        Dim strFooFooFileName, strFileSequence As String
        Dim strFileDate, strYear, strMonth, strDay As String
        Dim strDate As String = Mid(CStr(Now), 1, 10)   
        Dim strFileTime, strHour, strMinute, strSecond, strAMorPM As String
        Dim strTime As String = CStr(TimeOfDay)         
        Dim shtPosition As Short
        strFileDate = strDate

        'Get the month
        shtPosition = InStr(strDate, "/")   ' Returns 2 for a 1 digit month, 3 for a two digit month
        If shtPosition = 2 Then   '1 digit month        
            strMonth = "0" & Mid(strDate, 1, 1)
            strDate = Mid(strDate, 3, 10)
        ElseIf shtPosition = 3 Then '2 digit month      
            strMonth = Mid(strDate, 1, 2)
            strDate = Mid(strDate, 4, 10)
        End If

        'Get the day
        shtPosition = InStr(strDate, "/")   ' Returns 2 for a 1 digit day, 3 for a two digit day
        If shtPosition = 2 Then   '1 digit day          
            strDay = "0" & Mid(strDate, 1, 1)
            strDate = Mid(strDate, 3, 10)
        ElseIf shtPosition = 3 Then '2 digit day
            strDay = Mid(strDate, 1, 2)
            strDate = Mid(strDate, 6, 10)   'two digit year
        End If

        'Get the year
        strYear = Trim(strDate)
        'Concatenate year, month, day to use as part of the file name.
        strFileDate = Trim(strYear) & Trim(strMonth) & Trim(strDay)

        'Get the hour
        shtPosition = InStr(strTime, ":")   ' Returns 2 for a 1 digit hour, 3 for a two digit hour
        If shtPosition = 2 Then   '1 digit month        
            strHour = "0" & Mid(strTime, 1, 1)
            strTime = Mid(strTime, 3, 10)
        ElseIf shtPosition = 3 Then '2 digit month      
            strHour = Mid(strTime, 1, 2)
            strTime = Mid(strTime, 4, 10)
        End If

        'Get the minute
        strMinute = Mid(strTime, 1, 2)
        'Get the second
        strSecond = Mid(strTime, 4, 2)
        'Get AM or PM
        strAMorPM = Microsoft.VisualBasic.Right(Trim(strTime), 2)

        'Concatenate hour and minute to use as part of the file name.
        strFileTime = Trim(strHour) & Trim(strMinute) & Trim(strSecond)

'When the file drops into the Watched Directory, an 
'event is raised here.
        'Get the FileName, make it upper case
        strFooFooFileName = e.Name.ToUpper

        If strFooFooFileName.StartsWith("FOOISSUE") Then
            strFileSequence = Mid(strFooFooFileName, 8, 4)

            Try
                'Create an instance of StreamReader to 
                'read from a file.
                Dim sr As StreamReader = New StreamReader("C:\BLURP\ISSUES\UGH\" & strFooFooFileName)
                Dim sw As StreamWriter = New StreamWriter("C:\BLURP\ISSUES\i" & strFileDate & "_" & strFileTime & strAMorPM & "_" & strFileSequence & ".dat")

                Dim line As String
                Dim column1 As String
                Dim column2 As String
                Dim column3 As String
                Dim column4 As String
                Dim column5 As String
                Dim column6 As String
                Dim column7 As String
                Dim column8 As String
                Dim column9 As String

'Read and display the lines from the file until the 
'end of the file is reached.
                Do
                    'get line from FooFoo file
                    line = sr.ReadLine()
                    If line <> "" Then
                        column1 = "62"
                        column2 = Space(20)
                        column3 = Mid(line, 20, 10)
                        column4 = Mid(line, 33, 10)
                        column5 = Mid(line, 46, 10)
                        column6 = Mid(line, 58, 6)
                        column7 = Mid(line, 64, 22)
                        column8 = Space(4)
                        If line.StartsWith("CN ") Then
                            column9 = "V"
                        Else
                            column9 = Space(1)
                        End If
                        sw.WriteLine(column1 & column2 & column3 & column4 & column5 & column6 & column7 & column8 & column9)
                    End If
                Loop Until line Is Nothing
                sr.Close()
                sw.Close()

                'Move the file from the UGH directory to the UGH\ARCHIVE directory.
                File.Move("C:\BLURP\ISSUES\UGH\" & strFooFooFileName, "C:\BLURP\ISSUES\UGH\ARCHIVE\" & strFileDate & "_" & strFileTime & strAMorPM & "_" & strFooFooFileName)

            Catch ex As Exception
                'Let the user know what went wrong.
                Console.WriteLine("The file could not be read.")
            End Try

        End If

        'I MOVED ALL OF THE ABOVE CODE INTO THE CLASS

End Sub
 
If you built it into it's own class library (DLL), you'll want to add that project to your solution (so you'll have two projects), and then in your main project add a reference to it (right-click on references, select Add, choose to add a Project reference)

Once you've added it (or if you added the class to your existing project), you'll need to create an instance of your class with the New operator and assign it to a variable of the type of your class (this is the big secret of object-orientedness -- when you create a class, you're creating a new Type, which can be used to create variables). Afterwards, just call your method on your instance, just like as if it was something from the framework.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hello again,

For simplicity, I would copy (or build) the class into the windows app, do the testing, then build the new app (service) using a copy of the class... (as mentioned before)

Or, with out testing my idea, reference the dll that contains the class, and use the class from it. Something on the lines of "Dim MyClass As New MyDll.MyClass(AnyArgs)" then call "MyClass.MyFunction(AnyArgs)" within the sub.

I have to do the same thing within the next few days with a web proxy service, so I will be testing my idea. I'll pass on my findings...
 
Sorry it has taken me so long to respond. Thank you very much for all the advice. I've created the class in the Windows App, tested, and then built the service copying the class into the service. I'm happy to say the service is working, but it does need a bit of work. I've been testing by copying in 50 text files at a time into the directory that's being watched, and it works like a charm when running as a Windows app. However it misses a few files now and then when running as a service. (I think it misses a few if I drop the files in at the same time a scheduled task is running on the PC, but I can't say for sure.) When it misses the files, the FileSystemWatcher_changed event doesn't fire again for those same files, so they stay put in the directory I'm watching. I should probably use the FileSystemWatcher_created event as well, and add a timer to the app as was suggested before. Other than that, things are moving forward. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top