Can ANYBODY find what's wrong with this simple code

S

SamSpade

Public Property TxtTabSpacing() As Integer
Get
Dim lTabPos(31) As Integer 'Max allowed is 32 tabs
lTabPos = RichTextBox1.SelectionTabs()
TxtTabSpacing = lTabPos(0)
End Get
Set(ByVal TabLength As Integer)
Try
'Set a tab every TabLength
Dim lTabPos(31) As Integer
Dim lLpCnt As Integer
For lLpCnt = 0 To lTabPos.Length - 1
lTabPos(lLpCnt) = TabLength * (lLpCnt + 1)
Next
RichTextBox1.SelectionTabs = lTabPos
Catch
End Try
End Set
End Property

Compiler says:
Code generation for property 'TxtTabSpacing' failed. Error was: 'Index was
outside the bounds of the array.'

Thanks for any help

PS
I think the help for
RichTextBox.SelectionTabs Property [Visual Basic]
is wrong. It says for Exception Type:
The specified value is less than 0 or greater than 32.
I think it means the array lenght must not be greater that 32
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
Public Property TxtTabSpacing() As Integer
Get
Dim lTabPos(31) As Integer 'Max allowed is 32 tabs
lTabPos = RichTextBox1.SelectionTabs()
TxtTabSpacing = lTabPos(0)

This code doesn't make sense. The 2nd line will assign the
richtextbox's 'SelectionTabs' to the array, which may not have any
elements if no tabs are specified.
I think the help for
RichTextBox.SelectionTabs Property [Visual Basic]
is wrong. It says for Exception Type:
The specified value is less than 0 or greater than 32.
I think it means the array lenght must not be greater that 32

The number of items cannot be less than 0.
 
L

Les Smith

Sam
I assume this error is a run time exception even tho you said Compiler
says...

If that is true, it appears that lTabPos array is not being set. You could
step through the debugger to see the value of lTabPos(0) is before you
reference it. I suspect it is Nothing.

This line of code might be better:

Dim lTabPos(31) As Integer = RichTextBox1.SelectionTabs()

To set the contents of a whole array from a multi-item object normally has
to be done as the array is declared (in mho)

HTH
Les
http://www.knowdotnet.com
 
S

SamSpade

Herfried K. Wagner said:
This code doesn't make sense. The 2nd line will assign the
richtextbox's 'SelectionTabs' to the array, which may not have any
elements if no tabs are specified.

If (0) exists why not just
TxtTabSpacing = RichTextBox1.SelectionTabs(0)
I think the help for
RichTextBox.SelectionTabs Property [Visual Basic]
is wrong. It says for Exception Type:
The specified value is less than 0 or greater than 32.
I think it means the array lenght must not be greater that 32

The number of items cannot be less than 0.
sure but it say things about the values, probably meant indexes
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
If (0) exists why not just
TxtTabSpacing = RichTextBox1.SelectionTabs(0)

ACK, but I recommend to check if the element exists before accessing it.
 
S

SamSpade

Les Smith said:
Sam
I assume this error is a run time exception even tho you said Compiler
says...


I don't know. It sometimes appears in one of the IDE boxes (bummer, I forgot
which one) .
I can't make it happen.
But the "Code generation failed" made me think compiler (could be JIT?)
Anyway, if SelectionTabs(0) exits why not simply?
Get

TxtTabSpacing = RichTextBox1.SelectionTabs(0)

End Get


I'm converting from VB6 and can't run this (no menu item yet) to see if it
is working.
If that is true, it appears that lTabPos array is not being set. You could
step through the debugger to see the value of lTabPos(0) is before you
reference it. I suspect it is Nothing.

This line of code might be better:

Dim lTabPos(31) As Integer = RichTextBox1.SelectionTabs()

To set the contents of a whole array from a multi-item object normally has
to be done as the array is declared (in mho)

HTH
Les
http://www.knowdotnet.com


SamSpade said:
Public Property TxtTabSpacing() As Integer
Get
Dim lTabPos(31) As Integer 'Max allowed is 32 tabs
lTabPos = RichTextBox1.SelectionTabs()
TxtTabSpacing = lTabPos(0)
End Get
Set(ByVal TabLength As Integer)
Try
'Set a tab every TabLength
Dim lTabPos(31) As Integer
Dim lLpCnt As Integer
For lLpCnt = 0 To lTabPos.Length - 1
lTabPos(lLpCnt) = TabLength * (lLpCnt + 1)
Next
RichTextBox1.SelectionTabs = lTabPos
Catch
End Try
End Set
End Property

Compiler says:
Code generation for property 'TxtTabSpacing' failed. Error was: 'Index was
outside the bounds of the array.'

Thanks for any help

PS
I think the help for
RichTextBox.SelectionTabs Property [Visual Basic]
is wrong. It says for Exception Type:
The specified value is less than 0 or greater than 32.
I think it means the array lenght must not be greater that 32
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top