someone knows if it's possible create a fixed size ArrayList? Or I have to use necessarily an array?
I try with this
Dim array As ArrayList
array = New ArrayList(10)
and
array.Capacity = 10
But I can add more than 10 items, and it doesn 't show me any kind of error how I expected.
Thanks
Just use an Array this size will not change unless you explicitly code it to.
Dim myArray(9) As String 'or whatever object you need Integer, etc.
Note that specifying 9 will create 0-9 i.e. 10 items in your array
(ArrayLists are bad in many many ways so don't use them)
Capacity of ArrayList tells the maximum number of items ArrayList can currently hold. Capacity will be increased automatically at run time when more items are added in the ArrayList.
For fixed size, use Array as mentioned below:
Dim intArray(9) As Integer
If you are wanting to store different types in your collection you can use;
Dim myArray(5) As Object
If you wanted to read them back as the type you put them in as you would have to convert their type back what they were originally.
I do not recommend this as an approach. If you want to do this then I suggest you create a custom object such as a class or structures that will contain properties for each of the values you wish to set.
I am interested if it is possible to make variable name in PowerBuilder using a loop and a string. For example:
long ll_go
string lst_new
for ll_go = 1 to 8
lst_new = "text" + ll_go
lst_new.tag = 5500
next
So, it should give me variables text1, text2..,.,text8 and I would be able to assign values for them. Let me know if anybody succeeded, thanks in advance
Your description is lacking some term precision.
If you actually want to dynamically create new variables as "variable in a powerscript subroutine or function" this is simply not possible.
If instead you want to create dynamically some new controls statictext or textedit objects in a window or visual userobject this is possible:
use a local variable of the type of the new object you need to create, e.g. static text
make it a live object (instantiate) with create
set the object properties to whatever you need
"attach" the new object to its parent (either a window or a visual userobject - though any graphicobject is possible with using the win32api SetParent function) with the OpenUserObject() method. Note that you cannot simply add it directly to the parent's Control[] array.
you can also keep the object in your own array for later convenience access to the created objects instead of looping on the Control[] array
once the object is attached it its parent, you can reuse the local variable to create another one
Here is an example:
//put this in a button clicked() event on a window
//i_myedits is declared in instances variables as
//SingleLineEdit i_myedits[]
SingleLineEdit sle
int i
for i = 1 to 8
sle = create singlelineedit
sle.text = string(i)
sle.tag = "text_" + string(i)
sle.height = pixelstounits(20, ypixelstounits!)
sle.width = pixelstounits(100, xpixelstounits!)
parent.openuserobject(sle, pixelstounits(10, xpixelstounits!), pixelstounits(22 * i, ypixelstounits!))
i_myedits[i] = sle //keep our own reference
next
An exemple of values access:
//put that in another button clicked() event
SingleLineEdit sle
int i
string s_msg
for i = 1 to upperbound(i_myedits[])
sle = i_myedits[i]
if i > 1 then s_msg += "~r~n"
s_msg += "edit #" + string(i) + " (" + sle.tag + ") says '" + sle.text + "'"
next
messagebox("Edits values", s_msg)
As you can see, one practicality problem is that you cannot refer to these controls by constructing the control's name like "text"+2, instead you must access the my edits[] array or loop through the controls and test their .tag property if you set it to something specific.
I do not think that it is possible. Workaround could be an array maybe.
Br. Gábor
I'd see two ways to do this, but they aren't as easy as it seems that you were hoping:
1. Control Array
First method would be to go through the control arrays (on windows, tabs and user objects). I'd create a function that took the control name as a string, then another that overloaded the same function and took control name and an array of windowobject. The string-only method would just call the string/array method, passing the string through and adding the window.Control as the second parameter. The string/array method would go through the array, and for each element, get the ClassDefinition. Pull the name off of it, and parse it apart the way you want it to match the string parameter (e.g. for w_test`tab_first`tabpage_first`cb_here, do you want cb_here to match, or tab_first`tabpage_first`cb_here?). Deal with matches as appropriate. When you find a control of type tab or user object, call the string/array function again with the Control array from that object; deal with success/fail returns as appropriate.
2. DataWindow
What you're describing works extremely well with DataWindows, and their Describe() and Modify() functions. Since you pass these functions only a string, you can build not only the control names, but the values they're set to as you would build any string. In fact, you can build multiple Modify() strings together (delimited by a space) and make a single call to Modify(); this is not only faster, but reduces window flicker and visible activity.
Don't fall into the trap of thinking that, since your data isn't from a database, you can't use a DataWindow. Create an external DataWindow, and simply use it with one row inserted during the Constructor event.
As you might guess, I'd strongly favour the DataWindow approach. Not only is it going to perform better, but it's going to provide a lot more flexibility when you want to move on and tag more control types than just static text. (You'll have to do some type casting even with one control type, but if you want to get into multiples, you'll need to start a CHOOSE CASE to handle all your types.)
Good luck,
Terry
You can't create a variable name in a script because the variables have to be declared before you can use them. With PBNI it's possible to generate a name the way you describe and then get a reference to a variable of that name that already exists but I don't think that's what you want. If you want to keep track of additional properties for your controls, just inherit a new user object from whatever it is (sle, mle, etc.) and add the properties you want. Then you can place your user object on a window and use the properties. Another approach is to use the control's Tag property. It holds a string that you can put whatever you want in. PFC uses this technique. Terry's DataWindow solution is a good approach for storing arbitrary data.
Yes, and there are more than one way to skin a cat.
Sounds like you have several properties so I'd use an array of custom non visual user objects, or an array of structures. Otherwise you could probably use something from the .NET framework like a dictionary object or something like that, or a datawidnow using an external datasource, where you can refer to column names as col + ll_index.ToString().
SIMPLE Example:
Make custom NVO with following instance variables, plus getter/setter functions for each, name it n_single_field
// add the properties and recommend getter and setter functions
public string myTag
public string myText
public int myTabOrder
...
// To USE the NVO define an unbounded array
n_single_field fields[]
// to process the populated fields
integer li_x, li_max_fields
// loop through field 1 through max using array index for field number
li_max_fields = upperbound(fields)
for li_x = 1 to li_max_fields
fields[li_x].myTag = 'abc'
fields[li_x].myText = 'text for field number ' + li_x.ToString()
fields[li_x].myTabOrder = li_x * 10
next
Maybe I'm oversimplifying if so let me know, if there is a will there is always a way. ;)
I am making a basic GUI password creator that creates a random sequence of letters, numbers, and symbols in C++ (it's a Windows Forms Application). I am using a numericUpDown object to retrieve the user input for the length of the password being created. I am trying to define a char array using that number as the length, but I get an error that says "expected constant expression." I tried defining it as a constant variable but even that doesn't work. Is there any workaround?
The below code is part of what is executed when the "Randomize" button is pressed (the button that prompts the password to be created and then displayed.
const int length = System::Convert::ToInt16(numericUpDown1->Value);
And then later in the program:
char p[length];
You cannot declare arrays with a variable size in c++ (same goes for c++/cli).
It sounds like you want to generate a random array of characters and convert it to a System::String (System::String is immutable, so you need the array to manipulate the data). For this, you'll want to use a array<wchar_t> instead.
array<wchar_t>^ data = gcnew array<wchar_t>(System::Convert::ToInt32(numericUpDown1->Value));
// fill each character in data with a random character.
System::String^ password = gcnew System::String(data);
Edit: in case you're wondering why a wchar_t instead of char: in c++/cli, wchar_t is analogous to System::Char, which is the character type used by .NET. char would be analogous to System::SByte (a signed byte).
I have a 2d char array i.e. char myarray[5][5], I need to display the characters in a series of textboxes on the form application. There are two issues here:
The array is constructed by an external function, not a part of the public ref class form1 and i need to call the function of display which has to be defined inside form1 to access textboxes from the external function.
Even if i'm able to do that how do i display char in textboxes? It seems to accept only Sytem String^ type data.
I have reached the following conclusions (these might not be the ideal or good programming practices, but since no one has answered yet I'll tell how to get this through):-
Set a global variable. Store the processed data in it.
Access this data from Form1.
Convert data from string to System::String^ using gcnew
String^ myarray_str = gcnew String(myarray);
How do you save an array or an ArrayList in VB.NET using My.Settings? I cannot find the array type anywhere, even in the browse window.
I know I can convert the array to a string, but I do not know how to convert a string to an array. I know that if I were to break it at a delimiter then I could convert a string to an array, but my problem is that any text at all could be stored within the array as a single value, so I cannot pick a delimiter that is unlikely to be used.
I was also facing the same problem and I came up with a solution to this.
Here are the steps:
Open up the properties of your app and select settings
select the setting name and then where it says type click on the
arrow and select browse.
in the browse window type in system.collections.arraylist and hit enter!
there you have your array!
You can use array like this:
your_array_name(here_comes_the_item_no.) = whatever
What kind of array? I've had luck using StringCollection for strings. ArrayList works for most anything else (and that's about the only place I'd use arraylist).
I would either use the StringCollection type, and just convert your elements to/from strings when storing them in my.settings, or use XML Serialization to turn the array in to an xml string, and store that in my.settings.