Containers, Methods and Controls

My app uses several containers. At the moment I have a container array cc(10) as a property of the window and in the window open event I have cc(0)=new Container0. I use the controls in this container to open others. At the moment each container has its own methods and these are starting to duplicate.

What I would like to do is to move most of the methods from the containers to the main window. I find window.Name.methodName works OK howeverI seem to have lost the ability to refer to the container controls as I could before. In cc(3) for example I have a ListBox LB and I can refer to it in a container method as LB. Typing cc(3). in a Window method gives me a whole load of possibilities but none of them are the names of my container controls. Referring to cc(3).LB causes an error when run.

I’d be grateful for suggestions.

It sounds like your container array, cc(10), is typed as ContainerControl. So when you go “cc(3).” it can only show autocompletes for ContainerControl because that’s all the compiler knows it is.

If these containers are all the same type you can change cc(10) to that type and then those extra parts of that type will appear in autocomplete.

If cc(10) is holding a mix of types then it has to be typed as ContainerControl and/or something else is needed to get what you want.

You need to cast cc(3) and the compiler what kind of container control cc(3) is:


I don’t fully understand those responses. I have four containers designed at the moment, container0, container1, container2 and container3. They are all in the project

Perhaps what I need to do is to delete the container array as a window property and have c0 as container0, c1 as container1 etc. Then

container0, container1, container2 and container3 are subclasses of ContainerControl.

Dim cc As ContainerControl cc = container0 // you can store a subclass instance in variable dimensioned as ContainerControl cc.Close // this will work, as Close is defined in ContainerControl cc.YourMethod // this will NOT work, as YourMethod is defined in container0 for example container0(cc).YourMethod // will work (it is called casting) the compiler knows cc is of type container0

Same applies to your case with an array.

Oh, so you’ve created 4 different ContainerControls named container0/1/2/3 and then the property “cc() As ContainerControl” holds instances of all 4 types.

Why do you want an array? I mean, if having individual properties for each item works then what’s the purpose of an array?

Perhaps you could have 4 arrays, one for each type of ContainerControl…

cc0() As container0 cc1() As container1 //...

To clarify, the reason referring to cc(3).LB causes an error is because cc is most probably typed as ContainerControl (you haven’t actually said what cc’s type is though). When you code “cc(3)” it’s only known that cc is a ContainerControl so you can only access ContainerControl stuff. To access more stuff, like LB, you need cc(3) to be of the type with LB. You can do this either by casting as Eli pointed out or change the type of cc.

Well I still haven’t quite got my head round this. I have deleted cc(0) and replaced it with four window properties c0- c3 set to type container0 through to type container3. c0 is created in the window open event using

c0=new Container0

There are three button actually in the window itself which use similar code to open the other three containers. So I go to the window methods and try to create a methd that will work with the controls in c3, say.

Now my existing Container3 has a method including statements like

LB.celltag(n,0)=""//LB has been declared as a ListBox and n is a looping integer

But if I try to write a window method instead, such as


I get the error message “This method is protected. It can only be used from within its class”

Sorry, we overlapped there. What I’ve done seems to have improved things slightly but the new error message didn’t use to occur when the method was in the container

This could be because LB is private. Select LB in container3 and in it’s property pane change Scope to Public. You’ll need to make public everything the window needs to access. It could be something else though.

Thanks very much for your help guys, making the LB public prevents the final part of that problem.

I have to go out now, but I need to think about this some more. The point of putting the methods in the window was to avoide duplication and complexity. The problem I have now is that any method in the window is only going to work for one container. What I really want to do in window methods is to be able to refer to the ListBox and other controls so long as they have the same name whether they are in container 3 or container 23…

Do you think I could maybe make the window methods functions to which I could pass the container name?

But if I did that, is there some way for the window function to piece together c3 (the passed container name) with “LB.celltag(n,0)=”"

Actually, it’s starting to seem obvious to me now, since the ListBox is the most important object in nearly all of the containers, it would be much more sensible to put the methods in the listbox rather than the window…doh!