Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arithmetic operation resulted in an overflow.

Status
Not open for further replies.

Elminster

Programmer
Jun 26, 2001
78
CA
Hi to you all, I got to read some value in a ini file ... and i always get a "Arithmetic operation resulted in an overflow."

I even found an example in the help files, that do the same. Here is the code, if somebody knows whats wrong, it would be of great help to me.

Thanks

The Code:

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Short, ByVal lpFileName As String) As Long

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim IniPath As String
Dim IniSection As String
Dim IniName As String
Dim LibName As String
Dim StrSize As Short
Dim RetVal As Integer
Dim Defaut As String

Try

IniPath = (MyPath & "\scfdata.ini")
IniSection = "Archive Settings"
IniName = "LongName"
LibName = Space(100)
StrSize = Len(LibName)
RetVal = GetPrivateProfileString(IniSection, IniName, Defaut, LibName, StrSize, IniPath)

Catch ex As Exception

Dim msg As String
msg = "HELP LINK: " & ex.HelpLink & Chr(13)
msg = msg & "SOURCE: " & ex.Source & Chr(13)
msg = msg & "MSG: " & ex.Message & Chr(13) & Chr(13)
msg = msg & ex.ToString
Me.TextBox1.Text = msg.ToString

Finally

End Try

End Sub
 
I forgot, when I trace the error, it come from that line :

RetVal = GetPrivateProfileString(IniSection, IniName, Defaut, LibName, StrSize, IniPath)
 
Just scanning your code, I suspect the problem is in Defaut.

1) You have lpDefault in the declaration, but properly have it spelled Defaut in the code.
2) You do not give a value to Defaut, so it is nul. Nul strings will tend to give overflows.
 
Thanks a lot TerishD, after a gave a default value to "defaut" the error msg vanished :)

txs
 
Elminster -

Do you have Option Explicit turned on at the top of your source file? If not, you need to.

Chip H.
 
I didn’t use it ... I know that it force me to declare my variable, is there any other use to it?
 
This is one of those things you should always have in your VB source code. With it, the compiler flags your typos. Without it, you spend a lot more time in the debugger trying to figure out why your code is acting weird.

Note that without it, any undeclared variables are dimmed as Object. This results in your program running slower as the values are boxed/unboxed as they are used.

Another option you should strongly think about having set is Option Strict. This prevents data loss when assigning values into variables of a smaller type. Like storing an Int into a Byte variable -- you'll suffer a loss of precision, and your program will probably do odd things.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top