Results 1 to 18 of 18

Thread: code is reading ini as text not number

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    code is reading ini as text not number

    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


    frmsplash
    vb Code:
    1. Private Sub Command1_Click()
    2. Form1.Show
    3. frmSplash.Hide
    4. charname = "Gregor"
    5. race = ReadIniValue(App.Path & "\default.ini", "Default", "race")
    6. maxhp = ReadIniValue(App.Path & "\default.ini", "Default", "maxhp")
    7. hp = ReadIniValue(App.Path & "\default.ini", "Default", "hp")
    8. pmp = ReadIniValue(App.Path & "\default.ini", "Default", "pmp")
    9. maxpmp = ReadIniValue(App.Path & "\default.ini", "Default", "maxpmp")
    10. atk = ReadIniValue(App.Path & "\default.ini", "Default", "atk")
    11. pac = ReadIniValue(App.Path & "\default.ini", "Default", "pac")
    12. gold = ReadIniValue(App.Path & "\default.ini", "Default", "gold")
    13. plvl = ReadIniValue(App.Path & "\default.ini", "Default", "plvl")
    14. str = ReadIniValue(App.Path & "\default.ini", "Default", "str")
    15. dex = ReadIniValue(App.Path & "\default.ini", "Default", "dex")
    16. con = ReadIniValue(App.Path & "\default.ini", "Default", "con")
    17. intelect = ReadIniValue(App.Path & "\default.ini", "Default", "intelect")
    18. wis = ReadIniValue(App.Path & "\default.ini", "Default", "wis")
    19. cha = ReadIniValue(App.Path & "\default.ini", "Default", "cha")
    20. xp = ReadIniValue(App.Path & "\default.ini", "Default", "xp")
    21. End Sub
    22.  
    23. Private Sub Command2_Click()
    24. Form1.Show
    25. frmSplash.Hide
    26. charname = "Lidda"
    27. race = ReadIniValue(App.Path & "\default.ini", "Default", "race")
    28. maxhp = ReadIniValue(App.Path & "\default.ini", "Default", "maxhp")
    29. hp = ReadIniValue(App.Path & "\default.ini", "Default", "hp")
    30. pmp = ReadIniValue(App.Path & "\default.ini", "Default", "pmp")
    31. maxpmp = ReadIniValue(App.Path & "\default.ini", "Default", "maxpmp")
    32. atk = ReadIniValue(App.Path & "\default.ini", "Default", "atk")
    33. pac = ReadIniValue(App.Path & "\default.ini", "Default", "pac")
    34. gold = ReadIniValue(App.Path & "\default.ini", "Default", "gold")
    35. plvl = ReadIniValue(App.Path & "\default.ini", "Default", "plvl")
    36. str = ReadIniValue(App.Path & "\default.ini", "Default", "str")
    37. dex = ReadIniValue(App.Path & "\default.ini", "Default", "dex")
    38. con = ReadIniValue(App.Path & "\default.ini", "Default", "con")
    39. intelect = ReadIniValue(App.Path & "\default.ini", "Default", "intelect")
    40. wis = ReadIniValue(App.Path & "\default.ini", "Default", "wis")
    41. cha = ReadIniValue(App.Path & "\default.ini", "Default", "cha")
    42. xp = ReadIniValue(App.Path & "\default.ini", "Default", "xp")
    43. End Sub
    44.  
    45. Private Sub Form_Resize()
    46. If Form1.Height < 4335 Then
    47. Form1.Height = 4335
    48. End If
    49. If Form1.Width < 8490 Then
    50. Form1.Width = 8490
    51. End If
    52. If Form1.Height > 4335 Then
    53. Form1.Height = 4335
    54. End If
    55. If Form1.Width > 8490 Then
    56. Form1.Width = 8490
    57. End If
    58. End Sub
    that is all the code on frmsplash




    module1
    vb Code:
    1. Global gold As Integer
    2. Global str As Integer
    3. Global dex As Integer
    4. Global con As Integer
    5. Global intelect As Integer
    6. Global wis As Integer
    7. Global cha As Integer
    8. Global charname
    9. Global hp As Integer
    10. Global atk As Integer
    11. Global exp As Integer
    12. Global mobname
    13. Global mobhp As Integer
    14. Global mobatk As Integer
    15. Global maxhp As Integer
    16. Global mobgold As Integer
    17. Global pac As Integer
    18. Global mobac As Integer
    19. Global maxmobhp As Integer
    20. Global minmobhp As Integer
    21. Global plvl As Integer
    22. Global boat As Boolean
    23. Global race
    24. Global pmp As Integer
    please help me

    EDIT: i uploaded the ini i was referencing
    Attached Files Attached Files
    Last edited by bagstoper; Mar 28th, 2007 at 07:23 PM. Reason: wrong code

  2. #2

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: code is reading ini as text not number

    Have you considered wrapping ReadIniValue() in Val(), CInt(), Clng(), CDbl() etc functions?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: code is reading ini as text not number

    the ini defines them


    and no i dont know what those are

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: code is reading ini as text not number

    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).

  6. #6
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: code is reading ini as text not number

    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!

  7. #7
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: code is reading ini as text not number

    Quote Originally Posted by bagstoper
    the ini defines them
    I do not see any provision for the ini file to define a type.
    ex. from default.ini
    maxhp=30

    in the ini file maxhp simply is the character 3 followed by the character 0.
    vb does not know this to be an Integer, Long, ...


    pls post your ReadIniValue code.




    (From leinad31's suggestion)
    Val(), CInt(), Clng(), CDbl()

    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!

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: code is reading ini as text not number

    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

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: code is reading ini as text not number

    My two cents:

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: code is reading ini as text not number

    how would i use the two api calls martin?

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: code is reading ini as text not number

    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.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: code is reading ini as text not number

    why do i have to dim my variables if they are global? i will look at the save/get setting thing on sunday. no vb6 computer till then

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: code is reading ini as text not number

    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.

  14. #14
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: code is reading ini as text not number

    Quote Originally Posted by MartinLiss
    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:
    1. Option Explicit
    2. Public sLearningCurve As String

    Form1
    vb Code:
    1. Private Sub Form_Load()
    2.  sLearningCurve = "PUBLIC" 'set the PUBLIC sLearningCurve string to "PUBLIC"
    3. End Sub
    4.  
    5. Private Sub BangHeadOnWall()
    6. 'define a LOCAL-SCOPE variable of the same name
    7. 'as the PUBLIC variable sLearningCurve
    8. Dim sLearningCurve As String
    9. 'USE the LOCAL-SCOPE sLearningCurve
    10.  sLearningCurve = "LOCAL" 'set the LOCAL sLearningCurve string to "LOCAL"
    11.  Debug.Print sLearningCurve 'will show in ImmediateWindow as "LOCAL"
    12.  
    13. End Sub
    14.  
    15. Private Sub UsePublicVariable()
    16. 'USE the PUBLIC-SCOPE sLearningCurve
    17.  Debug.Print sLearningCurve 'will show in ImmediateWindow as "PUBLIC"
    18. 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!

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: code is reading ini as text not number

    Quote Originally Posted by MartinLiss
    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...


    Quote 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:
    1. Option Explicit
    2. Public sLearningCurve As String

    Form1
    vb Code:
    1. Private Sub Form_Load()
    2.  sLearningCurve = "PUBLIC" 'set the PUBLIC sLearningCurve string to "PUBLIC"
    3. End Sub
    4.  
    5. Private Sub BangHeadOnWall()
    6. 'define a LOCAL-SCOPE variable of the same name
    7. 'as the PUBLIC variable sLearningCurve
    8. Dim sLearningCurve As String
    9. 'USE the LOCAL-SCOPE sLearningCurve
    10.  sLearningCurve = "LOCAL" 'set the LOCAL sLearningCurve string to "LOCAL"
    11.  Debug.Print sLearningCurve 'will show in ImmediateWindow as "LOCAL"
    12.  
    13. End Sub
    14.  
    15. Private Sub UsePublicVariable()
    16. 'USE the PUBLIC-SCOPE sLearningCurve
    17.  Debug.Print sLearningCurve 'will show in ImmediateWindow as "PUBLIC"
    18. 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

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: code is reading ini as text not number

    In a Form

    vb Code:
    1. Option Explicit
    2.  
    3. Private MyVar As string ' This will be available to all procedures in the form

    In a code module

    vb Code:
    1. Option Explicit
    2.  
    3. Public MyOtherVar As String ' This will be available everyplace in the program

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: code is reading ini as text not number

    the module one was the one i was looking for. thanks martin

  18. #18
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: code is reading ini as text not number

    @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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width