Structures

I doubt I have a good use for Structures and my guess is that for now I shouldn’t use them but
knowing that curiosity killed the cat I’m curious.

  1. On a New Desktop Project I insert a Structure.

  2. I name it a

  3. 2 field 2 declarations
    a) myfield1 as string * 24
    b) myfied2 as string * 24

  4. add button to window

  5. button pressed.
    var myexp as a

myexp.myfield1 = “question”
myexp.myfield2 = “answer”

  1. Compile error
    Window1.Button1.Pressed, line 1
    Can’t find a type with this name
    var myexp as a

  2. What am I missing?

You are correct, you should not bother using structures. Use classes instead.

But your steps seem to work fine for me.

StructureTest project

Edit: Download link fixed

2 Likes

Does your Structure have broad enough scope for the button event to see it?

Can I view or download Structure Test Project? If so how?
Once I declare the structure it appears in the Navigator. Is there a further
step? If I declare a structure, SomeStructure and then do a var structurevariable as SomeStructure then I get a ‘can’t find a type with this name’ error.

When I click your link I get this:

Finally!
Obviously, I’m supposed to do something else after

  1. insert structure
  2. declare structure
    But instead of doing it that way
  3. add to window1 structure
  4. declare somestructure in structure editor
  5. I can then declare somevariable as somestructure
    This compiles and runs.

Paul Lefebre put that in his reply to me. I can’t figure what I’m supposed to do with it either. But the problem is solved. I just needed to insert the structure declaration in a project component. Details are in a prior reply but will repeat if needed.

But seriously, if you’re not using Declares, don’t use structures.

2 Likes

Why, beside the fact that they originally were only intended fro declares?

Because I can’t define classes within classes i sometime use structures for groups of related data within a class. That is because I can define structures in a class and that helps reducing the issue with getting lost in the navigator or having long property lists in a class. I actually makes maintaining the code easier and classes more self documenting in a readable fashion IMO and helps tame autocomplete lists.
BTW many years ago I put in a feature request to be able to group properties within a class that never got any traction.

-Karen

1 Like

In certain situations, structures are much faster than objects and can make a big difference in terms of speed.

1 Like

I see two people liked Greg’s post saying not to use Structures for other things besides declares, but know one has answered my question as to why.

I posted why I sometime use them for other things…

Besides less clutter in the navigator, being able to useful data structures in the class the apply to (which can not be done with class) , organize group go related that make autocomplete more user more usable, there is the fact that you also don’t have the overhead of addition object installation - which can be important when you need a lot instances.

So what is the issue with using them for other things besides declares?

Is it a philosophical objection, or are there practical reasons not to use them?

Of course way back when before I found REALBasic I used the Pascal Record data type that way.

  • Karen

Boy did I have a lot of typos in that!

It should have read:

Besides less clutter in the navigator, being able to define useful data structures in the class they apply to (which can not be done within a class) , organizing groups of related properties that make autocomplete more more usable, there is the fact that you also don’t have the overhead of additional object instantiation - which can be important when you need a lot instances.

-Karen

So philosophical?

-Karen

From your answer and the lack of other explanations, it seems that there are no negative consequences to using structures outside of in conjunction with declares.

While I have used them for declares, my most common use is for custom datatypes.

  • Karen

I’d say for 98.34% (yes, I made that up) of use cases, you want a class over a structure.

There are certainly uses for structures, which some have noted here: Declares, specific performance issues, or code simplification. But for most people, most of the time, classes are the way to go.

3 Likes
  1. A reasonable use for structures (per the documentation) is for writing to and reading from text files. I’m going to try and upload my experimental project whose final output is to place 2 buttons in Window1 after the launching of the program. Three methods are involved.
    A custom button is implemented as a class. A structure is inserted with 4 fields (left, top, width and height)
    Method 1 Read the previous written text file where the fields were delimited by commas… Go through the file line by line, splitstring each line and place the fields in the structure fields. Place the structure in a structure array and write the contents to a new file.
    Method 2 Read the new file line by line place each string in a structure and place the structure in a structure array. Then call method 3
    Method 3 Place 2 button in Window1 using the info in the structure array (structure field strings are converted to integers.

I’m still puzzled about the littleendian boolean variable parameter in the function stringvalue. True or false doesn’t seem to make any difference. When is this variable used?
Can’t upload the project.
Method I
var b as buttonspec
var c as string
var s() as string
var x as integer
var a() as string
var q as buttonspec
var r() as buttonspec
var myfile as FolderItem
var d as openfiledialog
var t as TextInputStream
d = new openfiledialog
myfile = d.showmodal
if myfile <> nil then
t = textinputstream.open(myfile)
while t.endoffile = false
c = t.ReadLine
s() = c.split(“,”)
q.left = s(0)
q.top = s(1)
q.width = s(2)
q.height = s(3)
r.add(q)
wend

t.close
end if

// ------------------------------------------------------------
Method II
var b as buttonspec
var c as string
var s() as string
var x as integer
var a() as string
var q as buttonspec
var r() as buttonspec
var myfile as FolderItem
var d as openfiledialog
var t as TextInputStream
d = new openfiledialog
myfile = d.showmodal
if myfile <> nil then
t = textinputstream.open(myfile)
while t.endoffile = false
c = t.ReadLine
b.stringvalue(true) = c

r.add(b)

wend

t.close
end if

makenewbutton(r)


Method III
Var p as new custombutton

p.left = integer.fromstring ( r(22).left )
p.top = integer.fromstring ( r(22).top )
p.width = integer.fromstring ( r(22).width )
p.height = integer.fromstring ( r(22).height )
addcontrol(p)

Var d6 as new custombutton

d6.left = integer.fromstring ( r(15).left )
d6.top = integer.fromstring ( r(15).top )
d6.width = integer.fromstring ( r(15).width )
d6.height = integer.fromstring ( r(15).height )
addcontrol(d6)