This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
IndexOutOfRangeException in VB.NET
(2 answers)
What does IndexOutofRangeException mean?
(3 answers)
“IndexOutOfRangeException was unhandled” [duplicate]
(2 answers)
Closed 4 years ago.
I have this code:
Module Module11
Sub Main()
Dim mess As String
Dim out As String
Dim num, p As UInteger
Dim q As Integer = -1
Console.Write("Enter a message: ")
mess = Console.ReadLine()
While p < 9
q += 1
num = Asc(mess(p + q)) + 5
out = Chr(num)
Console.Write(out)
End While
Console.ReadKey()
End Sub
End Module
But on Line num = Asc(mess(p + q)) + 5, it is showing 'index out of the bounds of array'.
I was actually trying to create code that could change every character (even blank space) to the next sixth character (with reference to ASCII codes) of whatever character we input.
It shows error even after giving the correct output (in the black console).
Please help.
I think I have the answer (or at least AN answer). I'm not familiar with visual basic, but I am familiar with Python and this seems to be extremely similar. After doing some quick researching, I learned that the "UInteger" data type cannot be negative. I would recommend changing the while loop to read "While p > 0 And p < 9" and then the rest of the code. It looks to me like your answer is coming out negative. This would make sense considering what your error message says. However, I'm not sure how the output would still come out correctly, but it's worth a shot. I would try that, and let me know how it goes!
Related
I'm getting a "Run-time error code '6': Overflow" message on something that is trivial (imo).
Strange problem I'm experiencing here with the code below:
Sub StageSetup()
Dim rLastDataRow As Range
Dim lCount As Long: lCount = 0
Dim lDelta As Long: lDelta = 0
1 With Worksheets("Analyzer")
2 Set rLastDataRow = .Range("A2").End(xlDown)
3 lCount = (rLastDataRow.Row - 2) / 10
4 lDelta = (rLastDataRow.Row - 2)
5 lCount = lDelta / 10
6 End With
End Sub
In debug mode, the problem is occurring on line 5 above which is odd in that it is a simple long division.
Note:
I added line 3 above after the fact just to see what happens when it is done in a single line. That works, but I'd like it to work in the form shown in line 5 as the future mods to this code will involve addition accumulations made to lCount before the division occurs.
rLastDataRow.Row when this happened was 2510 in value which so happen to be an evenly divisible value by 10 (stating the obvious).*
I could use a loop to simulate the division, but what's the point in doing that when a simple divide should suffice.
As can be seen, both lCount and lDelta are of Long type. So, this should have been trivial as there shouldn't be any form of type conversion going on here.
I've tried casting the variable and literal utilizing CLng and also the & (as I've seen in some of the threads but thought that was only meant for non-decimal base representations) in various combinations to no avail. I've also did some casting on line 4 with no effect.
Does anyone have any clue what may be going on here?
This question already has answers here:
Break out of a While...Wend loop
(4 answers)
Closed 5 years ago.
I can't get the While/Wend loop to work at all. Even the tiniest possible case, such as below:
Sub TestingWhile()
Dim i As Integer
i = 0
Do While i < 10
i = i + 1
Wend
End Sub
The result of that code is an error message window saying "Compile error: Wend without While".
I found people having this error, but only because they always had some wrong if/else statements. This obviously is not the case. What's going on?
By the way, I'm using Microsoft Visual Basic for Applications 7.1 while in Excel 2013.
EDIT - Question solved. Syntax, beginner mistake: Do While is closed by Loop, and While is closed by Wend
Do While is closed by Loop and While is closed by Wend :
Sub TestingWhile()
Dim i As Integer
i = 0
Do While i < 10
i = i + 1
Loop
End Sub
I'm working on this project, in Visual Basic:
Sub Main()
Dim i As Integer, j As Double
i = 1
Dim fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\text.txt")
For i = 1 To 3
Dim stringReader = fileReader.ReadLine()
j = j + CDbl(stringReader)
Next
Debug.Print(j)
End Sub
Which is supposed to read the first three lines of a text file containing:
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
Now when these numbers are added up, and the result is printed, it gives me scientific notation, so here is my question:
Is there a way I can make the program write the whole number without scientific notation, AND all the significant figures, I seem to get a problem where past 10 ~ 12 digits it's just all 0.
I know similar questions have been asked but I can't seem to find an answer to apply to my case, so I'm sorry if it is a duplicate.
You can use something like this. This will avoid E exponential format of numbers.
YourNumber.ToString(".################") 'No of digits = No of hashes #
This question already has answers here:
Escaping quotes in a string in VB6
(7 answers)
Closed 8 years ago.
I am trying to make one of my variables equal to the following:
Dim Quote as string
Quote = "Letter Size 11 x 17""
I need the single quote/inch symbol in the text but i do not know how to make VBA recognize it without giving me and error saying i have to end quote it.
Try one of these. It seems to do what you want (depending on where you want the ":
Sub test()
Dim Quote As String
Quote = "Letter Size 11 x 17"""
Debug.Print Quote
Quote = "Letter Size 11"" x 17"""
Debug.Print Quote
End Sub
during some code conversion from another persons VB.net project to C#, i have come across the following code:
Public Sub New(ByVal lbytModuleAddress As Byte, ByVal lbytRelayStateMask As Byte)
Dim lintCounter As Integer
mbytModuleAddress = lbytModuleAddress
For lintCounter = 0 To 7
If lbytRelayStateMask And (2 ^ lintCounter) Then
mblnRelayState(lintCounter) = True
Else
mblnRelayState(lintCounter) = False
End If
Next
End Sub
Now m trying to convert this to C#, but im a little confused as to exactly the meaning of this line:
If lbytRelayStateMask And (2 ^ lintCounter) Then
Could someone please enlighten me? It appears there something going on behind the scenes (that ill have to examine further) however before i do i would just like to clarify the result of this if statement.
Am i correct in saying if either one of the sub-expressions equals zero then its false, otherwise its true? Sorry, im not too up to speed on VB.net.
lbytRelayStateMask And (2 ^ lintCounter) results in BIT number lintCounter, which is on or off. a bit is a boolean value. AND is a bit operation, not the AND in an IF
th author might have written too:
mblnRelayState(lintCounter) = (lbytRelayStateMask And (2 ^ lintCounter))
Yes, I believe you are correct in your analysis.