Multiple values on 1:st index in array

105 views Asked by At

I'm trying to add lines in a menu. The object I try to change is an Core.Array. And it's named groupSizes.

I'm trying to do like this:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 1 put: #(4 1 9 4)

But that just puts a new array in side the array (logical). And "add:" don't work. I think I wish to achive something like this:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) at: groupSizes put: #(4 1 9 4)

But that don't work either. I can inspect the object and manually change the array from (18) to my desired (4 1 9 4). To confim that the change will achive what I wish.

Oh, this will be a change that is applied to the image by fileIn from an external .st-file. Thats why I can't change how it's created, (that and the fact that I barely know what I'm doing.) and wish to manipulate the values inside the array.

I'm 100% green on smalltalk. So I attached a picture that might say more then my words. enter image description here

Any idéas of how to change the value of that array?

On closer examination, this is more likely what i wish to do:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 1 put: 4.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 2 put: 1.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 3 put: 9.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 4 put: 4.

But the array don't have index 2-4, how can I increase the number of index:es?

2

There are 2 answers

0
Guinness On

In ArrayedCollection i found "growToAtLeast:" that could increase the capacity of groupSizes.

...groupSizes growToAtLeast: 2.
...groupSizes at: 1 put: 4.
...groupSizes at: 2 put: 1.
..etc..

I'm not sure this is the correct way, and I also dont know why I increased the size to 2, to get 4 index. But it works. Pleace tell me if this is the wrong way.

1
Karsten On

You shouldn't be so rude to your Objects. What you try to achieve is to modify a menu, so what you should be doing is kindly ask the menu to perform the changes or create a new menu instead. What you shouldn't be doing is modify the data that the menu operates on.


But first things first: You ask how you can add to an Array and the answer is simple: You can't! Arrays are meant to be created at a fixed size and not change their size ever. If you create an Array via: Array new: 4 you can access the slots 1 to 4 and put anything in it that you like. But if you try to do anArray add: 1 you'll get an Error via: self shouldNotImplement. Now the comment of the method #add: is along the lines of "yeah, you really shouldn't", which is absolutely not helpful. Even the class-comment doesn't say a word about "why". Among other things, Arrays are used for passing data to/from the VirtualMachine, where it's easier to keep things simple instead of having complicated objects (dealing with growth is pretty complicated stuff).

If you need a collection that can grow and behave a lot like Arrays, use OrderedCollection. They can grow, they can shrink, you can add to them or remove from them. OrderedCollection is basically the go-to Collection for when you need a list of things.

For more information on Collections, you should have a look at the BasicLibraries.pdf in your VisualWorks' installation's doc folder. Chapter 1 explains the various Collection subclasses. The most common classes that you may be using are: OrderedCollection, Dictionary and Set. Set you'll use almost never, Dictionary a bit more and OrderedCollectiona lot more. Most of the other Collections are rarely needed. It's good to have an overview of what's there, but you really don't need most of them.SortedCollectionused to be used more frequently back in the days. Nowadays it's easier to just sort any collection via#sorted:` and not bother with SortedCollection at all.

So to answer your Question:

Any idéas of how to change the value of that array?

The answer is simply: Don't even try.


To answer your actual question:

I'm trying to add lines in a menu.

There're other things you can do:

  1. Menus are resources, so there's probably a method that describes the menu somewhere on class-side of an ApplicationModel. You can edit the menu with the menu editor.

  2. Menus are hierarchies, so a Menu object is both the root of the menu, but also the object that's stored in a MenuItem's submenu. If you want to replace a menu item's submenu, just tell it to: aMenuItem submenu: myNewMenu. So you don't need to actually modify the sub-sub-sub menu that you try to modify. You can also create a new Menu object and just replace the sub-menu. When you create a Menu you can use the following code:

    myNewMenu := Menu new addItem: firstItem; addItem: secondItem; addLine; addItem: thirdItem; yourself. mySubmenu menu: myNewMenu.

So with #addItem: you can add your existing items to a new menu. And with #addLine you can tell the menu to create a new group and create a separator line.

If you need in-depth information on how to deal with Menus, i'd suggest you look at the GUIDevGuide.pdf in your VisualWorks' installation's doc folder. Chapter 7 is all about Menus.