468,512 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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

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
Nov 20 '05 #1
5 7165
"Peter" <pe***@mclinn.com> schrieb
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
Nov 20 '05 #2
I wanted to respond to one line...

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
"Peter" <pe***@mclinn.com> schrieb
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.


Hehe... thats just funny,
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

Nov 20 '05 #3
"CJ Taylor" <no****@blowgoats.com> schrieb
I wanted to respond to one line...


And why don't you only quote the one line? SCNR to ask.

COM? No, you don't need COM.


Hehe... thats just funny,

Ok, "...not for this purpose."

:)
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
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")

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
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

"Peter McLinn" <pm*****@mclinn.com> wrote in message
news:Ov**************@TK2MSFTNGP09.phx.gbl...
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")

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Ling Lee | last post: by
4 posts views Thread by Jerivix Entadi | last post: by
28 posts views Thread by John Salerno | last post: by
3 posts views Thread by waynejr25 | last post: by
16 posts views Thread by lovecreatesbea... | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.