Link to home
Start Free TrialLog in
Avatar of Pic
Pic

asked on

PropBag

How to store the content of an array to and fore design-time and run-time. ProBag?
I need to use an array I created in design-time during run-time. And after that, when switch back to
design-time, I still need that array. How do I preserve the array during the transition.  
Avatar of Kobe_Lenjou
Kobe_Lenjou
Flag of Belgium image

What are you exacly trying to do????
Can't you hardcode the array, put it in a data file, a database?

Avatar of Anita030598
Anita030598

How big is your array? If you have a form in your application, you can  add a listbox to it and store your array elements as list items in it. Use the List property. You can hide the list box as well. During run time, you can use the list box as it is to get the array item (list items) or put the items in an array or collection object.

BTW, what's ProBag? New terminology for me.
Anita, check out PropertyBag object in VB help.  Using the propertybag, you can do this without adding a listbox control.
mrmick, thanks for that note. I will see that right away!
Avatar of Pic

ASKER

mrmick, I think you are in a better position to help me.
How do I use the PropBag on an Array for the Q I mentioned?.
I don't think that you can preserve the array at design time. Using the ReadProperty and WriteProperty method, you can save the info into the 'Cookie'

Cheers
vspeter, please don't lock questions without providing a solution.  This question has nothing to do with cookies - cookies are something a webserver passes to a connected client for identification purposes.  Cookies have nothing to do with arrays or saving data.

pic, the propertybag object isn't what you want.  This is designed to be used when creating an ActiveX control.  When an ActiveX control is placed on a form it is instantiated and is then "running"; thus, properties become available and you can make additions/changes.  These changes are actually read/written in the UserControl_ReadProperties and  UserControl_WriteProperties event procedures.  This wouldn't work in an app because there's no "running code" to cause the data to be saved until you actually run the app.

Probably your best bet is to dimension an array in the declarations section of the form/module (module using public if you want global scope) and populate it in the Form_Load event procedure.  For example, if you had an array titled Colors, you could populate it in the Form_Load event procedure as follows...

Colors(0) = "Red"
Colors(1) = "Blue"
etc...

If you don’t like this suggestion, you could write your own UserControl (aka ActiveX control) just for this purpose; however, if this is the path you choose, I recommend you take Anita’s suggested solution since the listbox would serve this purpose.

Whatever you do, I hope you reject vspeter's proposed answer.
Avatar of Pic

ASKER

mrmick, sorry that I didn't mention this - I am builting an ActiveX control!
During design time in the initproperties event, my control will loop through all
controls on the form, any editbox found will be stored into an array. The control also
has a property page that allow one of the editbox in the array to be selected. The selected editbox will be used during run-time for this control to "talk" to.
All the above is already written and tested.
Now, when switched back to design time, as you know, the array is no longer there.
What happen if the user using my control want to reselect another editbox?  
This is where my Q comes in. I need to preserve that array somehow.
I think PropBag may do, but I don't know how to create the property for the array and how to use the propbag in read and write properties event.
Or how do I use the listbox? please explan. you did say that I don't need it.

The contents of arrays are effectively discarded once a program has ended.  Files are used to store data in a more permanent form on your storage device.

One solution is to write the array contents to disk using a fuction and reading it back from the file when you re-run the program. example

private sub WriteArray(ByRef MyArray())

'Function to read values back from a temporary file. You will call this function at design 'time to store your array values to a text file.
'You need to send the

dim i as integer
open "c:\temp\array.txt" for output as #1
for i = LBound(MyArray) to UBound(MyArray)
  write #1,MyArray(i)
next
close #1,

end sub

private sub ReadArray(ByRef MyArray())

'Function to store array values in a temporary file
'In your finished application you will be able to restore the values to the array.

dim i as integer
open "c:\temp\array.txt" for output as #1
for i = LBound(MyArray) to UBound(iArrayUpper)
  input #1,MyArray(i)
next
close #1,

end sub

 
Avatar of Pic

ASKER

deighton, sorry, I would prefer not to use a file for storage of array content.
My idea is to use the propbag, if it is possible. mrmick did say it can be done.
It's quite possible. But what you mean 'I need to use an array I created in design-time'?
Avatar of Pic

ASKER

marti, just assume that I store some data into the array at design-time which I need to use later when I switch from design-time to run-time, then back to design-time.
I thought I explained it clearly when I talked about the ActiveX control that I am building.

Well. Create a property say 'Array'. Whenever you change your array at design time create a string which has this format:
1. Number of rows & separator
2. Number of cols & separator
3. List all the members of the array delimited with separator
Set 'Array' property equal to this string.
When you switch to run time this string will be saved at WriteProperties event.
So at run time when ReadProperties fires you have to populate that string back to the array.
And that's it. Hope this is what you are looking for.
Regards.
Avatar of Pic

ASKER

marti, your answer is actually what I have done in the past.
Right now, I am looking for a way to preserve the array without
bothering about constructing it into a string and then separate it back later.
Does antone know how I can do this using the PropBag?




The other way is to create property for every element of the array. Do you like it better?
Avatar of Pic

ASKER

marti, my array is a dynamic array. It is not fixed-sized array.

So in this case I don't think you have option other then assembling your dynamic array into a sting. Read Write PropBag events are invoked only per properties.
Avatar of Pic

ASKER

marti, if that is the case, so be it. You can lock on, I will give you the point.
ASKER CERTIFIED SOLUTION
Avatar of marti
marti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial