• Welcome to Valhalla Legends Archive.
 

[VB] Problems reading INI file. (Solved)

Started by Don Cullen, February 12, 2007, 01:50 AM

Previous topic - Next topic

Don Cullen

I'm using the below I found by searching on the forums. I had originally written my own using solely GetPrivateProfileString and WritePrivateProfileString, but I liked this one as it was considerably easier to use. It was written by I believe Stealth.

It writes fine to the INI, however when I try to pull up the value, it shows up as blank. I think there's an error somewhere in reading, but I'm unable to locate the error. Perhaps you guys will have better luck. Thanks in advance for any assistance rendered.

Public 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
Public 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

Public Const sINIFile As String = "Sanity.ini"

'Read/WriteIni code thanks to ickis
Public Sub WriteINI(ByVal wiSection$, ByVal wiKey$, ByVal wiValue$, ByVal wiFile$)
    wiFile$ = (App.Path & "\" & wiFile$)
    WritePrivateProfileString wiSection, wiKey, wiValue, wiFile
End Sub

Public Function ReadINI(ByVal riSection$, ByVal riKey$, ByVal riFile$) As String
    Dim sRiBuffer$
    Dim sRiValue$
    Dim sRiLong$
   
    riFile$ = (App.Path & "\" & riFile$)
    If Dir(riFile$) <> "" Then
        sRiBuffer = String(255, vbNull)
        sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, riFile)
        If Left(sRiBuffer, 1) <> Chr(1) Then
            sRiValue = Left(sRiBuffer, sRiLong)
            ReadINI = sRiValue
        End If
    Else
        ReadINI = ""
    End If
End Function

Public Sub SaveOptions(sVariable As String, ByVal sValue)
    WriteINI "Options", sVariable, sValue, sFile
End Sub

Public Function GetOptions(sVariable)
    GetOptions = ReadINI("Options", sVariable, sINIFile)
End Function


And the code I'm using for the command button click is:


Private Sub Command2_Click()
    SaveOptions "Test", "TestValue"
    tempvar = GetOptions("Test")
End Sub


I checked the INI file, and it shows this:

Quote[Options]
Test=TestValue

What do you think?
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Barabajagal


Public Sub SaveOptions(sVariable As String, ByVal sValue)
    WriteINI "Options", sVariable, sValue, sFile
End Sub

sFile doesn't seem to be defined. You seem to be pretty new at VB with all these mistakes... enable Option Explicit and it won't happen.

Don Cullen

D'oh. Yeah, I never used Option Explicit mainly because it was annoying (I tend to not bother to properly define my variables). But considering how I just wasted your time with those silly newbish mistakes (very embarassing when I've been coding in VB for a year now), I think I'll take your advice and just use Option Explicit. At least it'll ensure I make less of those newbish mistakes.

Anyway, it works now. Thanks!
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Warrior

..did you just say you don't like predefining your variables? ..oh god. What has VB done to people..
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

UserLoser

Quote from: Warrior on February 12, 2007, 03:14 PM
..did you just say you don't like predefining your variables? ..oh god. What has VB done to people..

VB code is a measurement of intelligence.

Don Cullen

Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Grok

Quote from: Kyro on February 12, 2007, 05:00 AM
D'oh. Yeah, I never used Option Explicit mainly because it was annoying (I tend to not bother to properly define my variables). But considering how I just wasted your time with those silly newbish mistakes (very embarassing when I've been coding in VB for a year now), I think I'll take your advice and just use Option Explicit. At least it'll ensure I make less of those newbish mistakes.

Anyway, it works now. Thanks!

Good!  Remember this -- regardless of the language you use, the compiler probably includes helpful restrictions which protect you from ... yourself!  Option Explicit is one of those protections.  I wouldn't write professional code without this restriction.  When you're focused on other things, such as overall architecture, algorithms, data validation, exception handling, you're often writing code by the function, rather than line-by-line.  Having the compiler catch when you forget to declare and type a variable is a tremendous help.