Войти

Показать полную графическую версию : VB.NET | Границы строки


mrcnn
23-03-2008, 11:38
VB.Net 2002

Надо currentposition < Len(s), НО НЕЛЬЗЯ. Почему?

Public Class Form1
Inherits System.Windows.Forms.Form
Dim s As String
Dim i As Integer
Dim currentposition As Integer = 0
Dim beginning As Integer
Dim tempstring As String

Dim Lb, Inf As New Label()
Dim Tx, MltTx As TextBox
Dim Bt As New Button()
Dim hzSize, constvAlign, consthAlign As Integer, _
vAlign As Integer = 5, _
hAlign As Integer = 5, _
vertSize As Integer = 20

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

constvAlign = vAlign
consthAlign = hAlign

Tx = New TextBox()
MltTx = New TextBox()

hzSize = 90
With Lb
.Size = New Size(hzSize, vertSize)
.Location = New Point(hAlign, vAlign)
.Text = "Длина"
.TextAlign = ContentAlignment.MiddleCenter
.ForeColor = Color.Red
End With

hAlign = hAlign + hzSize + 5
hzSize = 100
With Tx
.Size = New Size(hzSize, vertSize)
.Location = New Point(hAlign, vAlign)
.Text = "Введите число"
.ForeColor = Color.Blue
End With

hAlign = hAlign + hzSize + 5
hzSize = 80

With Bt
.Size = New Size(hzSize, vertSize)
.Location = New Point(hAlign, vAlign)
.Text = "Вычислить"
.ForeColor = Color.Red
End With

hAlign = 5
vAlign = vAlign + vertSize + 10
vertSize = 200
hzSize = 200

With MltTx
.Size = New Size(hzSize, vertSize)
.Location = New Point(hAlign, vAlign)
.Text = ""
.ForeColor = Color.Blue
.Multiline = True
End With


Controls.Add(Lb)
Controls.Add(Tx)
Controls.Add(Bt)
Controls.Add(MltTx)

AddHandler Bt.Click, AddressOf bt_click
AddHandler MltTx.TextChanged, AddressOf MltTx_TextChanged


End Sub




Private Sub bt_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim fg As Boolean = True
s = Trim(MltTx.Text)
tempstring = ""
beginning = currentposition

While fg = True
tempstring = ""
beginning = currentposition
'ПОЧЕМУ нельзя While s.Substring(currentposition, 1) <> ' (" " Or vbCrLf Or "." Or "," Or ":" Or ";") Then
'Надо currentposition < Len(s) НО НЕЛЬЗЯ. Почему?
While currentposition < Len(s) - 1 And _
String.Compare(s.Substring(currentposition, 1), " ") <> 0 And _
String.Compare(s.Substring(currentposition, 2), vbCrLf) <> 0 And _
String.Compare(s.Substring(currentposition, 1), ".") <> 0 And _
String.Compare(s.Substring(currentposition, 1), ",") <> 0 And _
String.Compare(s.Substring(currentposition, 1), ";") <> 0 And _
String.Compare(s.Substring(currentposition, 1), ":") <> 0
tempstring = tempstring & s.Substring(currentposition, 1)
currentposition = currentposition + 1
End While
'ТАК КАК НЕЛЬЗЯ currentposition < Len(s)
If currentposition = Len(s) - 1 Then
tempstring = tempstring & s.Substring(currentposition, 1)
fg = False
End If

If currentposition < Len(s) - 1 Then
If String.Compare(s.Substring(currentposition, 2), vbCrLf) = 0 Then
currentposition = currentposition + 2
Else
currentposition = currentposition + 1
End If
Else
fg = False
Bt.Enabled = False
End If
If Len(tempstring) > 0 And Len(tempstring) < Val(Tx.Text) Then fg = False
End While

If (Len(tempstring) < Val(Tx.Text)) Then
MltTx.SelectionStart = beginning
MltTx.SelectionLength = Len(tempstring)
MltTx.Select()
End If


End Sub

Private Sub MltTx_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Bt.Enabled = True
tempstring = ""
currentposition = 0
End Sub



End Class

mrcnn
23-03-2008, 12:02
Суть вот в чем

Предположим, есть строка "fgfgfg"
s="fgfgfg"
Элементы в строке считаются с 0.
В скобках номера элементов:
"f(0)g(1)f(2)g(3)f(4)g(5)"
Итак длина этой строки len(s)=6
Я могу обращаться к 0-5 элементам.
Показатель номера элемента в строке currentposition
Если currentposition<-1 или currentposition>=len(s) это ошибка выхода за пределы строки.

Почему компилятор выдает ошибку выхода за пределы, если я в условии цикла обращаюсь к последнему элементу, т.е currentposition = 5 при длине 6

В чем загвоздка?

mrcnn
23-03-2008, 12:25
Вопрос снят. Тему можно удалить.

String.Compare(s.Substring(currentposition,2), vbCrLf)




© OSzone.net 2001-2012