jcnconnect

I have ran my code and the debugger found a problem and i can't seem to fix this problem.

Can anyone tell me what is wrong with my code.

Thanks.

Imports System.IO

Imports System

Public Class Form1

Dim DocName As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

DocName = TextBox2.Text

Dim fs As New FileStream(DocName, FileMode.Open, FileAccess.Read)

'declaring a FileStream to open the file named file.doc with access mode of reading

Dim d As New StreamReader(fs)

'creating a new StreamReader and passing the filestream object fs as argument

d.BaseStream.Seek(0, SeekOrigin.Begin)

'Seek method is used to move the cursor to different positions in a file, in this code, to

'the beginning

While d.Peek() > -1

'peek method of StreamReader object tells how much more data is left in the file

parse(d.ReadLine())

End While

d.Close()

End Sub

Private Sub parse(ByVal message As String)

Dim mystring, ch, word As String, N, nch As Integer

'Using the ASCII values starting with 65 for uppercase "A"

word = ""

mystring = message

N = mystring.Length

For x As Integer = 0 To N - 1 Step 2

ch = mystring.Substring(x, 2)

nch = Convert.ToInt32(ch)

ch = Chr(nch)

ch = ch.ToLower

word &= ch

Next

RichTextBox1.Text &= word

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

End Sub

End Class



Re: Visual Basic Express Edition what is wrong with my code

Karl Davies-Barrett

THe richtextbox has native methods to do what i think you are trying to do.

Code Snippet
RichTextBox1.LoadFile(TextBox2.Text)

same goes for saving if you need it

Code Snippet
RichTextBox1.SaveFile(TextBox2.Text)

So you don't need the parse method and you can replace all the code in the button click to

Code Snippet

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

RichTextBox1.LoadFile(TextBox2.Text)

End Sub

Good Luck






Re: Visual Basic Express Edition what is wrong with my code

Adamus Turner

ch = Chr(nch)

ch = ch.ToLower

There's no such thing as a lowercase special character

Adamus






Re: Visual Basic Express Edition what is wrong with my code

jcnconnect

I am trying to make a file with ACSII code to readable charecters. I need the parse method to convert the ACSII code to readable charecters.



Re: Visual Basic Express Edition what is wrong with my code

jcnconnect

I removed the ToLower. My debugger says the problem is

ch = mystring.Substring(x, 2)

In the parse method.

Thanks.





Re: Visual Basic Express Edition what is wrong with my code

spotty

Your code doesnt have any error checking it - so this may be an issue relating to the data you provide to it in the form of a file.

If the line is 1 byte long then this may be a problem as your code assumes that there are at least two characters in the line to get the substring - this may not be the case.

Perhaps - the question being asked is what are you trying to achieve with your code. If you tell us what your expected behaviour is and what you are providing as an input file, perhaps we will be better able to assist you. From the looks of your code you are trying to load the text from a file as lower case.

Is this the case as there may be a much simple way of doing this.





Re: Visual Basic Express Edition what is wrong with my code

jcnconnect

Here is my code for making the file.

Imports System.IO

Imports System

Public Class Form1

Dim LogFileName As String = "C:\ENC3.txt"

Dim LSW As New StreamWriter(LogFileName, True)

Private Sub Log(ByVal Message As String)

Dim logMsg As String = Message

LSW.WriteLine(logMsg)

End Sub

Private Sub parse(ByVal charecter As String)

Dim charecter2 As String = Now.ToString & " : " & charecter

Dim ch, word As String, N, ach As Integer

If charecter2 <> "" Then

word = ""

N = charecter2.Length

charecter2 = charecter2.ToLower

For x As Integer = 0 To N - 1

ch = charecter2.Chars(x)

ach = Asc(ch)

If ach < 10 Then ch = "0" & ch

If ach < 100 Then ch = "0" & ch

ch = ach.ToString

word &= ch

Next

Log(word)

MessageBox.Show(word)

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

parse("Button1 Clicked")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

parse("Button2 Clicked")

LSW.Close()

End

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

parse("form loaded")

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

End Class

Here is my input file.

5347514847504848553257585148585156321121093258321021111141093210811197100101100
5347514847504848553257585148585251321121093258329811711611611111049329910810599107101100
5347514847504848553257585148585254321121093258329811711611611111050329910810599107101100

Here is my decoder.

Imports System.IO

Imports System

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim fs As New FileStream("C:\ENC3.txt", FileMode.Open, FileAccess.Read)

'declaring a FileStream to open the file named file.doc with access mode of reading

Dim d As New StreamReader(fs)

'creating a new StreamReader and passing the filestream object fs as argument

d.BaseStream.Seek(0, SeekOrigin.Begin)

'Seek method is used to move the cursor to different positions in a file, in this code, to

'the beginning

While d.Peek() > -1

'peek method of StreamReader object tells how much more data is left in the file

parse(d.ReadLine())

End While

d.Close()

End Sub

Private Sub parse(ByVal message As String)

Dim mystring, ch, word As String, N, nch As Integer

'Using the ASCII values starting with 65 for uppercase "A"

word = ""

mystring = message

N = mystring.Length

For x As Integer = 0 To N - 1 Step 2

ch = mystring.Substring(x, 2)

nch = Convert.ToInt32(ch)

ch = Chr(nch)

word &= ch

Next

RichTextBox1.Text &= word

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

End Sub

End Class

Hope this helps.





Re: Visual Basic Express Edition what is wrong with my code

spotty

Sure

the line

For x As Integer = 0 To N - 1 Step 2

ch = mystring.Substring(x, 2)

The value of N is 79, indicating the length is 79 characters long. meaning character positions 0 - 78

However the substring which will retrieve the characters you are trying to retrieve something from position 78 that is 2 characters long.

This is longer than the string and therefore this is you failure. You need to ensure that there are sufficient characters to retrieve.

Perhaps with some checking such as


Code Snippet

For x As Integer = 0 To N - 1 Step 2

If (x + 2) <= N - 1 Then

ch = mystring.Substring(x, 2)

nch = Convert.ToInt32(ch)

ch = Chr(nch)

word &= ch

End If

Next


Or adjust the iteration in the for loop so it is going round N-3 instead of N-1 assuming 2 characters each time.


Not too sure what you intention is but this is you problem.