-
How do I convert this simple VB 6.0 into VB .NET
How do I write this in VB .net?
Private Sub Command1_Click()
Dim sTemp As String
Dim WordLen As Integer
Dim i As Integer
Dim result As Integer
WordLen = Len(Text1.Text)
If WordLen = 0 Then Exit Sub
Open "C:\input.txt" For Input As 1
Do While Not EOF(1)
Line Input #1, sTemp
For i = 1 To Len(sTemp) - WordLen
If Mid(sTemp, i, WordLen) = Text1.Text Then
result = result + 1
End If
Next i
Loop
Close 1
Text2.Text = result
-
Re: How do I convert this simple VB 6.0 into VB .NET
"Sarah" <sarahadams74@yahoo.com> wrote:
>
>How do I write this in VB .net?
>
>Private Sub Command1_Click()
>Dim sTemp As String
>Dim WordLen As Integer
>Dim i As Integer
>Dim result As Integer
>
>WordLen = Len(Text1.Text)
>
>If WordLen = 0 Then Exit Sub
>
>Open "C:\input.txt" For Input As 1
> Do While Not EOF(1)
> Line Input #1, sTemp
> For i = 1 To Len(sTemp) - WordLen
> If Mid(sTemp, i, WordLen) = Text1.Text Then
> result = result + 1
> End If
> Next i
> Loop
>Close 1
>
>Text2.Text = result
Have you tried running the upgrade wizard on it? For all its flaws, I've
found that the upgrade wizard will often give you the equivalent .NET code.
It's worked for some file access code I had.
-
Re: How do I convert this simple VB 6.0 into VB .NET
"Sarah" <sarahadams74@yahoo.com> wrote:
>
>How do I write this in VB .net?
>
>Private Sub Command1_Click()
>Dim sTemp As String
>Dim WordLen As Integer
>Dim i As Integer
>Dim result As Integer
>
>WordLen = Len(Text1.Text)
>
>If WordLen = 0 Then Exit Sub
>
>Open "C:\input.txt" For Input As 1
> Do While Not EOF(1)
> Line Input #1, sTemp
> For i = 1 To Len(sTemp) - WordLen
> If Mid(sTemp, i, WordLen) = Text1.Text Then
> result = result + 1
> End If
> Next i
> Loop
>Close 1
>
>Text2.Text = result
Here is what the upgrade wizard produced:
Dim sTemp As String
Dim WordLen As Short
Dim i As Short
Dim result As Short
WordLen = Len(Text1.Text)
If WordLen = 0 Then Exit Sub
FileOpen(1, "C:\input.txt", OpenMode.Input)
Do While Not EOF(1)
sTemp = LineInput(1)
For i = 1 To Len(sTemp) - WordLen
If Mid(sTemp, i, WordLen) = Text1.Text Then
result = result + 1
End If
Next i
Loop
FileClose((1))
Text2.Text = CStr(result)
-
Re: How do I convert this simple VB 6.0 into VB .NET
Sarah,
This is one way - I have bailed on VB6 compatibility file IO because the
peformance is terrible - Use file streams instead. I like to impliment stuff
like this as a function, this is of course user preference. Please respond
to this kind act by helping others with programming problems in which you
can contribute and they can learn.
A wise condiderate soul
'-------------------------------------------------------------
Imports System.IO
Imports System.String
Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Command1_Click.Click
Dim SRcogo As StreamReader
Dim sTemp As String
Dim iLineCnt As Int32
Dim WordLen As Short
Dim i As Short
Dim result As Short
WordLen = Len(Text1.Text)
If WordLen = 0 Then Exit Sub
'Open the file stream
Try
SRcogo = File.OpenText(Text1.Text)
'Read the first line to see if anything there - I do this you may not
need to
sLine = SRcogo.ReadLine
'Rewind file
SRcogo.BaseStream.Seek(0, SeekOrigin.Begin)
SRcogo.DiscardBufferedData()
Catch e As Exception
MessageBox.Show(e.Message)
'Return (-1)
End Try
'I like to display a wait cursor - file may be large
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
'I also like to count lines so I can see where a line scan error occurs
iLineCnt= 0
'Scan file line by line until EOF
While SRcogo.Peek() > -1
Try
'Read a line of data
sTemp = SRcogo.ReadLine
iLineCnt = iLineCnt + 1
'Here I would investigate how to use the String class and all
'its cool methods
For i = 1 To Len(sTemp) - WordLen
If Mid(sTemp, i, WordLen) = Text1.Text Then
result = result + 1
End If
Next i
'You can ignore this stuff
'sLine = SRcogo.ReadLine
'There should always be at least 3 args returned
'lCnt = srvParse2Array(sLine, sDelim, sArray)
'If lCnt < 3 Then Err().Raise(551) 'If true invalid args
Catch When Err.Number = 551
Err.Clear()
'Invalid args - is OK to continue scanning file
Catch e As Exception
System.Windows.Forms.Cursor.Current = Cursors.Default
MsgBox("Error scanning input ASCII file at line " & iLineCnt + 1 &
vbCrLf & "Check input file for invalid data.", MsgBoxStyle.Critical + MsgBoxStyle.OKOnly,
"ASCII Scan")
Exit While
End Try
End While
System.Windows.Forms.Cursor.Current = Cursors.Default
SRcogo.Close() 'Close the input ASCII file that was scanned
Text2.Text = CStr(result)
End Sub
-
Re: How do I convert this simple VB 6.0 into VB .NET
"TLN1" <vb.@127.0.0.1> wrote:
>
>Sarah,
>
>This is one way - I have bailed on VB6 compatibility file IO because the
>peformance is terrible - Use file streams instead. I like to impliment stuff
>like this as a function, this is of course user preference. Please respond
>to this kind act by helping others with programming problems in which you
>can contribute and they can learn.
>
>A wise condiderate soul
>
>
>
>'-------------------------------------------------------------
>Imports System.IO
>Imports System.String
>
>
>
>Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
>Handles Command1_Click.Click
>
>Dim SRcogo As StreamReader
>Dim sTemp As String
>Dim iLineCnt As Int32
>Dim WordLen As Short
>Dim i As Short
>Dim result As Short
>
>
>WordLen = Len(Text1.Text)
>
>If WordLen = 0 Then Exit Sub
>
>'Open the file stream
>Try
> SRcogo = File.OpenText(Text1.Text)
> 'Read the first line to see if anything there - I do this you may not
>need to
> sLine = SRcogo.ReadLine
> 'Rewind file
> SRcogo.BaseStream.Seek(0, SeekOrigin.Begin)
> SRcogo.DiscardBufferedData()
> Catch e As Exception
> MessageBox.Show(e.Message)
> 'Return (-1)
>End Try
>
>'I like to display a wait cursor - file may be large
>Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
>'I also like to count lines so I can see where a line scan error occurs
>iLineCnt= 0
>
>'Scan file line by line until EOF
>While SRcogo.Peek() > -1
> Try
>
> 'Read a line of data
> sTemp = SRcogo.ReadLine
> iLineCnt = iLineCnt + 1
>
> 'Here I would investigate how to use the String class and all
> 'its cool methods
> For i = 1 To Len(sTemp) - WordLen
> If Mid(sTemp, i, WordLen) = Text1.Text Then
> result = result + 1
> End If
> Next i
>
> 'You can ignore this stuff
> 'sLine = SRcogo.ReadLine
> 'There should always be at least 3 args returned
> 'lCnt = srvParse2Array(sLine, sDelim, sArray)
> 'If lCnt < 3 Then Err().Raise(551) 'If true invalid args
>
> Catch When Err.Number = 551
> Err.Clear()
> 'Invalid args - is OK to continue scanning file
>
> Catch e As Exception
> System.Windows.Forms.Cursor.Current = Cursors.Default
> MsgBox("Error scanning input ASCII file at line " & iLineCnt + 1 &
>vbCrLf & "Check input file for invalid data.", MsgBoxStyle.Critical + MsgBoxStyle.OKOnly,
>"ASCII Scan")
> Exit While
>
> End Try
>
>End While
>
>System.Windows.Forms.Cursor.Current = Cursors.Default
>SRcogo.Close() 'Close the input ASCII file that was scanned
>
>Text2.Text = CStr(result)
>
>End Sub
>
>
It's the modern tradeoff between performance and clear concise code I guess.
This is probably faster, but my example is understandable at a higer level

-
Re: How do I convert this simple VB 6.0 into VB .NET
Dave,
Thanks so much for your response! Where do I get a wizard? And how would
I convert the info (in text form) into a vb for conversion?
"Dave" <dave_doknjas@yahoo.ca> wrote:
>
>"Sarah" <sarahadams74@yahoo.com> wrote:
>>
>>How do I write this in VB .net?
>>
>>Private Sub Command1_Click()
>>Dim sTemp As String
>>Dim WordLen As Integer
>>Dim i As Integer
>>Dim result As Integer
>>
>>WordLen = Len(Text1.Text)
>>
>>If WordLen = 0 Then Exit Sub
>>
>>Open "C:\input.txt" For Input As 1
>> Do While Not EOF(1)
>> Line Input #1, sTemp
>> For i = 1 To Len(sTemp) - WordLen
>> If Mid(sTemp, i, WordLen) = Text1.Text Then
>> result = result + 1
>> End If
>> Next i
>> Loop
>>Close 1
>>
>>Text2.Text = result
>
>Have you tried running the upgrade wizard on it? For all its flaws, I've
>found that the upgrade wizard will often give you the equivalent .NET code.
>It's worked for some file access code I had.
>
>
-
Re: How do I convert this simple VB 6.0 into VB .NET
"Sarah" <sarahadams74@yahoo.com> wrote:
>
>Dave,
>
>Thanks so much for your response! Where do I get a wizard? And how would
>I convert the info (in text form) into a vb for conversion?
>
>
The wizard comes with Visual Studio.NET, most editions I think.
You would normally just attempt to open a VB6 project from VB.NET and you'll
be prompted for the rest.
To do smaller code segments, you can just create a new VB6 project with just
that code and then open the project in VB.NET, triggering the upgrade wizard
(that's how I got the code to reply to your question). Just be aware that
the upgrade wizard will usually produce code that needs some modifications
before it's compilable (it'll produce a task list, plus you'll find that
you'll have to do other modifications), although your example was upgraded
without the need for modifications.
Glad to help,
Dave
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks