Wring a VB.NET object to count lines in a text file

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Team VB.net,

I have multiple programs that need a tool to count lines in a text
files. I have been solving this problem through writing proceedures
but I hate cutting and pasting code.

Is there an easy way of making a "module" or object that I would be
able to pass a string variable "c:\MyTextFile" to and then have it run
this proceedure on it?


It seems like I need to write a COM to do this, but I have yet to find
an easy tutorial on this subject.....

Any direction or tutorial links you have would be greatly appreciated.

-Peter
 
Peter said:
Team VB.net,

I have multiple programs that need a tool to count lines in a text
files. I have been solving this problem through writing
proceedures but I hate cutting and pasting code.

What does writing proceudres have to do with cutting and pasting code? It's
sufficient to write it once.
Is there an easy way of making a "module" or object that I would
be able to pass a string variable "c:\MyTextFile" to and then have it
run this proceedure on it?

Yes, write a module or a class and add the procedure. I prefer a class.
It seems like I need to write a COM to do this, but I have yet to
find an easy tutorial on this subject.....

COM? No, you don't need COM.
Any direction or tutorial links you have would be greatly
appreciated.

Class CountTextFileLines
Public Shared Function Start(ByVal filename As String) As Integer
Dim fs As IO.FileStream
Dim sr As IO.StreamReader
Dim Result As Integer

fs = New IO.FileStream( _
filename, IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.Read _
)

sr = New IO.StreamReader(fs)

Do
If sr.ReadLine Is Nothing Then Exit Do
Result += 1
Loop

Return Result
End Function
End Class


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
I wanted to respond to one line...

Armin Zingler said:
What does writing proceudres have to do with cutting and pasting code? It's
sufficient to write it once.


Yes, write a module or a class and add the procedure. I prefer a class.


COM? No, you don't need COM.

Hehe... thats just funny,
 
ok. I build this and get a dll.

Now how do I refrence it?

I went to add refrence and found the dll.

What is the sytax I should be using to sent the data to this dll...

IE.. mydll("c:\test.text")
 
Well... your reference will give you the dll namespace, and then you just
execute a function within that DLL as if it was part of another class within
your program.

Given I don' t know what your DLL reference is like, the namespace, or the
methods you use I can't give you a specific example. However, I can do
this.

if your dll has the namespace myDLL (that is probably the project name for
you) and you add a reference you can access that. Now just the DLL is
useless (excuse me, I shouldn't use the word DLL as it can lead to some
confusion, its a .NET Assembly with the extension DLL. Many of the same
features but unlike classic DLL's (Regular DLL's or ActiveX DLL's) these can
only run in a managed environement.

so, you have MyDLL, and lets say you have MyFunction as a method of that
dll.

In your main program you would simply call

returnval = MyDLL.MyFunction("C:\testfile.txt")

this is all dependent on what you have in there though. =)

-CJ
 
Back
Top