the code i have reads ini information as text and not a number like i want it to. the problem ocurres on frmsplash. the varriables are defined in module1. let me know if you need any more inforemation. any help would be nice
You should always use Option Explicit in your programs. The downside (it's really not a downside but you may look at it that way) of that is that it forces you to Dim your variables. The upside is that once you Dim hp As Integer, hp will be a number (if in fact the ini contains a numeric vale for that variable).
you might find problem using str (line 2 in Module1) and exp (line 11 in Module1) as variable names.
I do not get errors from VB when declaring or using these names, but the original functions don't work.
Exp Function
Returns a Double specifying e (the base of natural logarithms) raised to a power.
Syntax
Exp(number)
The required number argument is a Double or any valid numeric expression.
Remarks
If the value of number exceeds 709.782712893, an error occurs. The constant e is approximately 2.718282.
Note The Exp function complements the action of the Log function and is sometimes referred to as the antilogarithm.
Str Function
Returns a Variant (String) representation of a number.
Syntax
Str(number)
The required number argument is a Long containing any valid numeric expression.
Remarks
When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied.
Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number.
Note The Str function recognizes only the period (.) as a valid decimal separator. When different decimal separators may be used (for example, in international applications), use CStr to convert a number to a string.
Do not use if shrinkwrap is broken or missing!
I'm learning how to fish, too!
Val()
Returns the numbers contained in a string as a numeric value of appropriate type.
Syntax
Val(string)
The required string argument is any valid string expression.
Remarks
The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.
The following returns the value 1615198:
Val(" 1615 198th Street N.E.")
CInt converts variable to Integer - if possible
CLng converts variable to Long - if possible
CDbl converts vairable to Double - (you guessed it) if possible
CDbl Double -1.79769313486231E308 to
-4.94065645841247E-324 for negative values;
4.94065645841247E-324 to 1.79769313486232E308 for positive values.
CInt Integer -32,768 to 32,767; fractions are rounded.
CLng Long -2,147,483,648 to 2,147,483,647; fractions are rounded.
I use Write/GetPrivateProfileString... and convert when Writing/Getting so I can sanity-check the values in BOTH directions. Yes, I know that I am 'STRING'ing the values, but no extra datatype info needs to be stored.
Code:
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
....Hope this makes sense. I must be off.
Do not use if shrinkwrap is broken or missing!
I'm learning how to fish, too!
Always read your values into a string variable, whatever the type is (for scalar variables only, of course). Then convert the read variable to the type needed into the real variable. IOW
Code:
Dim s As String
s = ReadIniValue(App.Path & "\default.ini", "Default", "race")
'assuming race is an Integer (you defined it as a Variant)
race = CInt(s)
That way if what you think is a number is somneting else in the .ini file, your program won't blow up. You'll get 0 for the number 'abc', for example. Reading 'abc' directly into an Integer variable will throw an error.
This is just good practice for .ini files - files don't usually get corrupted. It's almost mandatory for user input, where the input can be the wrong type.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana
1) Don't use ReadIniValue and WriteIniValue but instead use GetPrivateProfileString and WritePrivateProfileString. IMO the former two are unnecessarily compilcated substitutes for the latter two APIs.
2) Don't "Always read your values into a string variable... It's almost mandatory for user input, where the input can be the wrong type." I say that firstly because if your program is written correctly and validates user input you can't get "the wrong type". In the second place if the user has been able to enter invalid data and you blithly convert abc to 0 you will never know about the error. To give a (contrived) example let's say the ini file contains 0 or 1 to represent male or female, and the female user enters "female" rather than 1. From that point on you would treat her as male and never know the difference. IMHO I'd rather have a program fail then process invalid data.
Click the link to the How to use SaveSetting and GetSetting example in my signature. Please note however that your result will be the same unless you Dim your variables in your program. Sooner or later, one way or another, you will run into problems if you don't.
Are they declared as Public in a code module or as Private in a form? If not then they are not global but rather local to the procedures in which they are used.
Are they declared as Public in a code module or as Private in a form? If not then they are not global but rather local to the procedures in which they are used.
Marty, do you work in the 'Microsoft Department Of Obfuscation Disambiguation Department?'
I think what your meaning is:
if you declare Public sLearningCurve as String in a module,
and also declare Dim sLearningCurve as String in a form procedure Private Sub BangHeadOnWall () then....
in the frmBangHead.BangHeadOnWall SUB sLearningCurve will be *LOCAL* to the BangHeadOnWall sub and
will not equate to the variable of the same name as declared in the module.
example:
Module1
vb Code:
Option Explicit
Public sLearningCurve As String
Form1
vb Code:
Private Sub Form_Load()
sLearningCurve = "PUBLIC" 'set the PUBLIC sLearningCurve string to "PUBLIC"
End Sub
Private Sub BangHeadOnWall()
'define a LOCAL-SCOPE variable of the same name
'as the PUBLIC variable sLearningCurve
Dim sLearningCurve As String
'USE the LOCAL-SCOPE sLearningCurve
sLearningCurve = "LOCAL" 'set the LOCAL sLearningCurve string to "LOCAL"
Debug.Print sLearningCurve 'will show in ImmediateWindow as "LOCAL"
End Sub
Private Sub UsePublicVariable()
'USE the PUBLIC-SCOPE sLearningCurve
Debug.Print sLearningCurve 'will show in ImmediateWindow as "PUBLIC"
End Sub
I'll stop here. If I go on, it might not help any more.
Do not use if shrinkwrap is broken or missing!
I'm learning how to fish, too!
Are they declared as Public in a code module or as Private in a form? If not then they are not global but rather local to the procedures in which they are used.
how would i make it public? like this?
public hp as integer
if that is wrong how do i make it public?
and...
Originally Posted by kewakl
Marty, do you work in the 'Microsoft Department Of Obfuscation Disambiguation Department?'
I think what your meaning is:
if you declare Public sLearningCurve as String in a module,
and also declare Dim sLearningCurve as String in a form procedure Private Sub BangHeadOnWall () then....
in the frmBangHead.BangHeadOnWall SUB sLearningCurve will be *LOCAL* to the BangHeadOnWall sub and
will not equate to the variable of the same name as declared in the module.
example:
Module1
vb Code:
Option Explicit
Public sLearningCurve As String
Form1
vb Code:
Private Sub Form_Load()
sLearningCurve = "PUBLIC" 'set the PUBLIC sLearningCurve string to "PUBLIC"
End Sub
Private Sub BangHeadOnWall()
'define a LOCAL-SCOPE variable of the same name
'as the PUBLIC variable sLearningCurve
Dim sLearningCurve As String
'USE the LOCAL-SCOPE sLearningCurve
sLearningCurve = "LOCAL" 'set the LOCAL sLearningCurve string to "LOCAL"
Debug.Print sLearningCurve 'will show in ImmediateWindow as "LOCAL"
End Sub
Private Sub UsePublicVariable()
'USE the PUBLIC-SCOPE sLearningCurve
Debug.Print sLearningCurve 'will show in ImmediateWindow as "PUBLIC"
End Sub
I'll stop here. If I go on, it might not help any more.
i realy dont understand what you are saying here. i am new to vb6
@bagstoper:
Refer to the code ...
if you were to put this code in a project, you could see it.
if you run the sub UsePublicVariable(), the immediate window would display the word "PUBLIC",
showing that you are accessing the variable sLearningCurve that was
declared public in a module (although the value was assigned in the Form_Load().)
if you run the sub BangHeadOnWall(), you will see that the immdediate window would display the word "LOCAL"
, showing that you were NOT accessing the variable sLearningCurve that was
declared in the module, BUT were really accessing a local variable of the same name--with a different value.
Do not use if shrinkwrap is broken or missing!
I'm learning how to fish, too!