EachIn troubles

BlitzMax Forums/BlitzMax Beginners Area/EachIn troubles

AndyBoy_UK(Posted 2005) [#1]
Ignore me. I fixed it, I was talking rubbish, the following code works fine.

Strict

Type TSetting
   Field name:String
   Field val:String
End Type

Type TSettingsFile
   Field settings:TList

   Method New()
       settings = New TList
   End Method

   Method addSetting(sName:String, sVal:String)
      Local newSetting:TSetting = New TSetting
      newSetting.name = sName
      newSetting.val = sVal
      ListAddLast self.settings, newSetting
   End Method

   Method SaveToFile(sFileName:String)

       Local s:TSetting
       For s = EachIn self.settings
           Print s.Name + " = " + s.Val
       Next
   End Method
End Type


Local file:TSettingsFile = New TSettingsFile

file.addSetting("swers","kk132")
file.addSetting("sdsfs","kdsfk2")
file.addSetting("ssdfewrs","kfdk")

file.SaveToFile("")



Dreamora(Posted 2005) [#2]
your problem must be somewhere else.
The following code works with BM 1.06 (win beta) without any problems

Strict

Type TSetting
   Field name:String
   Field val:String
End Type

Type TSettingsFile
   Field settings:TList

   Method New()
       settings = New TList
   End Method


   Method SaveToFile(sFileName:String)

       
       For Local s:TSetting = EachIn settings
           Print s.Name + " = " + s.Val
       Next
   End Method
End Type

Local a:TSetting = New TSetting
a.name = "bla"
a.val = "trala"

Local file:TSettingsFile = New TSettingsFile
file.settings.addlast(a)

file.savetofile ("a")
Delay 2000
End



AndyBoy_UK(Posted 2005) [#3]
Thanks Dremora, I have similar code that doenst work, but as what I just typed in does, I must be being stupid in my other project. Will post again if I have further issues.

Cheers,a


AndyBoy_UK(Posted 2005) [#4]
Ok Here is the faulty code

Type tFRSetting
	Field id:String
	Field value:String
End Type

Type tFRFile
	Field id:String
	Field fileName:String
	
	Field settings:TList
		
	' // Constructor //
	Method New()
		Self.settings = CreateList()
	End Method
	
	
	Function getSetting(name:String)
	End Function
	
	Function setSetting(name:String, newValue:Object)
	End Function
	
	Function loadRaw(sFileName:String)
		Local sLine:String
		Local sSettingName:String
		Local sSettingValue:String
	
		Local iEquals:Int
	
		Local strmFile:TStream = ReadFile(sFileName)

		If Not strmFile Then
			Print "ERROR: FileReader  - No Stream for file " + sFileName
			Return
		End If 
		
		While Not Eof(strmFile)
			sLine = ReadLine(strmFile)
			sLine = Trim(sLine)
			
			If Left(sLine,1) <> ";" Then
				iEquals = Instr(sLine, "=", 1)

				sSettingName = Left(sLine, iEquals -1)
				sSettingValue = Mid(sLine, iEquals +1)

				' // Get rid of any trailing semi-colons //
				If Right(sSettingValue, 1) = ";" Then
					sSettingValue = Left(sSettingValue, Len(sSettingValue) -1)
				End If
				
				' // Create setting object //
				Local newSetting:tFRSetting = New tFRSetting
				newSetting.id = sSettingName
				newSetting.value = sSettingValue
				
				' // Add this new setting object to the file class //
				ListAddLast Self.settings, newSetting
			End If
		Wend
		
		CloseStream strmFile
	End Function
	
	Function saveFile(fileName:String)
		
		Local strmFile:TStream
		Local s:tFRSetting
		
		strmFile = WriteFile(fileName)
		
		For s = EachIn self.Settings
			WriteLine strmFile, s.id + "=" + s.value + ";"
		Next
		
		CloseStream strmFile
		
	End Function
End Type


Function FRLoadFile:tFRFile(fileName:String)
	Local newFile:tFRFile = New tFRFile
	newFile.fileName = fileName
	newFile.loadRaw(fileName)
	
	Return newFile
End Function

Print "  Module: FileReader Loaded"



Whats going on there.


AndyBoy_UK(Posted 2005) [#5]
Ok Found it, Sorry for wasting thread space, its because Im using Functions and not Methods inside the Types when Im not returning anything, schoolboy error! My bad.

Although, shouldnt I get a "no return value found" when I declare them?

Cheers,
A


Dreamora(Posted 2005) [#6]
The return isn't a problem

Problem is that Function can not access Fields defined as it works on "type scope" and not "instance scope" so the only thing it knows of within a type is global and const.

for working on instance scope you have to use method as you already realized :)

return is no problem. wrong return types etc would give you an error as many other things would too (especially in strict)


AndyBoy_UK(Posted 2005) [#7]
Ah Of course, that makes perfect sense and I should bash my head against something until it hurts.

Thanks Dreamora for the explanation, I really should have realised that one myself.

Ta

A