Genie problem: storing strings in classes

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
ProgRob
Posts: 67
Joined: Wed 13 Feb 2013, 12:39

Genie problem: storing strings in classes

#1 Post by ProgRob »

Hi all,

I am tearing my hair out on this. I am working on a project that, amongst other things, is reading in .desktop files and storing some of the information it finds in them. I'd like to use classes to store the info but I am getting some behaviour which I only half understand.

I've created a simple test program to demonstrate:

Code: Select all

class Thing
	prop nameline : string = "** name line not found **"
	prop genericnameline : string = " generic name line not found **"

init
	line : string
	lngth : size_t
	var t = new Thing()
	var dis = new DataInputStream(File.new_for_path("/usr/share/applications/Geany-text-editor.desktop").read())
	while ((line = dis.read_line (out lngth, null)) != null)
		if line.has_prefix("Name=")
			t.nameline = line
		else if line.has_prefix("GenericName=")
			t.genericnameline = line
	print "We found\n  Name: %s\n  Generic name: %s", t.nameline, t.genericnameline
When I run this I get:

Code: Select all

We found
  Name:
  Generic Name: XricName=Geany^ [[?1;2c
Not at all what I was expecting! However, when I run the following program which does not use classes:

Code: Select all

init
	line : string
	lngth : size_t
	nameline : string = "** name line not found **"
	genericnameline : string = "** generic name line not found **"
	var dis = new DataInputStream(File.new_for_path("/usr/share/applications/Geany-text-editor.desktop").read())
	while ((line = dis.read_line (out lngth, null)) != null)
		if line.has_prefix("Name=")
			nameline = line
		else if line.has_prefix("GenericName=")
			genericnameline = line
	print "We found\n  Name: %s\n  Generic name: %s", nameline, genericnameline
I get the result I expect:

Code: Select all

We found
  Name: Name=Geany text editor
  GenericName: GenericName=Geany
I think this is something to to with weak references but I cannot fathom out how to get the class to own the strings.

Can anyone advise?

Thanks,
Rob

ProgRob
Posts: 67
Joined: Wed 13 Feb 2013, 12:39

#2 Post by ProgRob »

OK, so I have found a solution that works.

In the class definition I simply drop the 'prop' and turn the properties into fields. My question is therefore now a simpler one: what is the different between a field and property in a Genie (and Vala) class?

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#3 Post by PaulR »

The first dozen or so lines of this page should make things clearer (at least for Vala) with regard to properties:

https://wiki.gnome.org/Projects/Vala/PropertiesSample

My understanding is that 'fields' are just class variables.

Paul.

Post Reply