Ga naar inhoud

[VB6] Vreemd gedrag variabelen


Aanbevolen berichten

Ik heb een globale string genaamd s en ik start een sub met als argument een in de subheader gedefinieerde string genaamd What. Vanwege een windows api wil ik de string s een vaste waarde geven van 128 spaties door middel van s=Space(128) Het vreemde is dat de inhoud van What ook verandert in 128 spaties. [code:1:e1932f9acb] Private s As String Private Sub GetEntries(What as String) s = space(128) getprivateprofilestring What, "iets", "EMPTY", s, 128, "bestand.dat" End Sub [/code:1:e1932f9acb] Sla ik eerst de inhoud van What op in een tijdelijke variabele dan is dat geen probleem, maar als ik nadat ik s heb bewerkt de inhoud van de tijdelijke variabele weer terug in What zet dan wordt s ook wat What wordt. [code:1:e1932f9acb] Private s As String Private Sub GetEntries(What as String) Dim t as String t = What s = space(128) What = t getprivateprofilestring What, "iets", "EMPTY", s, 128, "bestand.dat" End Sub [/code:1:e1932f9acb] Het werkt wel als ik t als globale variabele declareer, net als s, en dan t gebruik in de api aanroep. [code:1:e1932f9acb] Private s As String Private t as String Private Sub GetEntries(What as String) t = What s = space(128) getprivateprofilestring t, "iets", "EMPTY", s, 128, "bestand.dat" End Sub [/code:1:e1932f9acb] Mijn vraag is: is dit vreemde gedrag onder windows XP normaal? Is het normaal dat variabelen automatisch aangepast worden aan andere variabelen wanneer je er mee werkt?
Link naar reactie
Dit hoort zeer zeker niet. In princiepe zouden de twee variabelen niets met elkaar te maken moeten hebb. Maar ja, het blijft Microsoft. Ik heb het even in windows ME geprobeerd: [code:1:6dd21671ff]Private s As String Private Sub test(what As String) s = Space(128) MsgBox what MsgBox s End Sub[/code:1:6dd21671ff] Dit komt er uit: (what maak ik "abc" van) what = "abc" s = " " (heleboel spaties, kan niet precies zien hoeveel) Conclusie: in winME werkt het wel. Wat doet ' getprivateprofilestring What, "iets", "EMPTY", s, 128, "bestand.dat"' overigens?
Link naar reactie
Dat weet ik en daarom werk ik liever met lokale variabelen, maar hier moet het wel. Een .ini bestand ziet er zo uit: [games] NumOfGames=2 1=Unreal Tournament 2=Quake 3 [apps] NumOfApps=0 Ik wil de waarden van Games in een listbox plaatsen en ik lees eerst hoeveel spellen er in games zitten, dan maak ik een do while loop die alle games leest en in de listbox plaatst Dit is de huidige code [code:1:acdcdaa2eb]Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long 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 Private Sub GetEntries(What As String) r = LCase(What) l = GetPrivateProfileInt(r, "Number", 0, MenuFile) If l > 0 Then Do While l > 0 s = Space(128) GetPrivateProfileString r, CStr(l), "EMPTY", s, 128, MenuFile List1.AddItem s l = l - 1 Loop End If End Sub[/code:1:acdcdaa2eb] l, r en s zijn globaal gedeclareerd, evenals MenuFile. De twee declaraties zijn public omdat ze in een ander bestand (Declarations.bas) zitten.
Link naar reactie
[quote:022fb17dd2="DarkScribe"] Een .ini bestand ziet er zo uit: [games] NumOfGames=2 1=Unreal Tournament 2=Quake 3 [apps] NumOfApps=0 [/quote:022fb17dd2] Dit is tegenstrijdig met onderstaande code. Dit lijkt er meer op (mits je alleen lijstjes wilt makens zoals bovenstaand)... [code:1:022fb17dd2] [games] Number=2 1=Unreal Tournament 2=Quake 3 [apps] Number=0 [/code:1:022fb17dd2] [quote:022fb17dd2="DarkScribe"] Ik wil de waarden van Games in een listbox plaatsen en ik lees eerst hoeveel spellen er in games zitten, dan maak ik een do while loop die alle games leest en in de listbox plaatst Dit is de huidige code [code:1:022fb17dd2] - [untested] - Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long 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 - [/untested] - Private Sub GetEntries(What As String) [/code:1:022fb17dd2] Altijd aangeven of What een Byval of ByRef variabele is! Grote kans dat hiermee het probleem achterhaald kan worden. Om de code beter onderhoudbaar te maken raad ik je aan om ook de listbox als parameter mee te geven. [code:1:022fb17dd2] r = LCase(What) l = GetPrivateProfileInt(r, "Number", 0, MenuFile) If l > 0 Then Do While l > 0 s = Space(128) GetPrivateProfileString r, CStr(l), "EMPTY", s, 128, MenuFile List1.AddItem s l = l - 1 Loop End If End Sub[/code:1:022fb17dd2] [/quote:022fb17dd2] [code:1:022fb17dd2] Private Function GetEntries(ByVal SectionName As String, ByRef myListBox As ListBox, ByVal IniBestand As String) As Boolean GetEntries = False On Error Goto ErrHandler Dim lSectionName As String Dim KeyValue As String Dim KeyCount As Integer lSectionName = LCase(SectionName) KeyCount = GetPrivateProfileInt(lSectionName,"Number",0,IniBestand) If (KeyCount > 0) Then Do While (KeyCount > 0) KeyValue = Space(128) If (GetPrivateProfileString (lSectionName,CStr(KeyCount),"EMPTY",KeyValue,128,IniBestand) > 0) then Call myListBox.AddItem(KeyValue) EndIf KeyCount = KeyCount - 1 Loop End If GetEntries = True End Function ErrHandler: Call MsgBox("Fout in GetEntries(): " & Err.Description) End Function ' Aanroepen met het volgende... GetEntries("games",List1,MenuFile) [/code:1:022fb17dd2] code niet getest. Maar dit moet je in de goede richting helpen. [quote:022fb17dd2="DarkScribe"] l, r en s zijn globaal gedeclareerd, evenals MenuFile. De twee declaraties zijn public omdat ze in een ander bestand (Declarations.bas) zitten.[/quote:022fb17dd2] Lekker duidelijke variabele namen. Not!
Link naar reactie

Om een reactie te plaatsen, moet je eerst inloggen

Gast
Reageer op dit topic

×   Geplakt als verrijkte tekst.   Herstel opmaak

  Er zijn maximaal 75 emoji toegestaan.

×   Je link werd automatisch ingevoegd.   Tonen als normale link

×   Je vorige inhoud werd hersteld.   Leeg de tekstverwerker

×   Je kunt afbeeldingen niet direct plakken. Upload of voeg afbeeldingen vanaf een URL in

  • Populaire leden

    Er is nog niemand die deze week reputatie heeft ontvangen.

  • Leden

    Geen leden om te tonen

×
×
  • Nieuwe aanmaken...