[Gambas-user] Safety for collections
bsteers4 at gmail.com
Thu Aug 24 13:02:25 CEST 2023
On Tue, 22 Aug 2023 at 13:28, BB <adamnt42 at gmail.com> wrote:
> On 22/8/23 9:11 pm, Bruce Steers wrote:
> On Tue, 22 Aug 2023 at 12:19, BB <adamnt42 at gmail.com> wrote:
>> On 22/8/23 8:38 pm, BB wrote:
>> There is probably a simple way to do this, damned if I can see how.
>> I have a collection of things (datObjects) that have a property
>> (ObjectName::string) that in UML terms are Unique and ID. What I am trying
>> to do is prevent the *replacement* of an object in the collection if the
>> ObjectName is already there. Better still, I'd like to warn the user when
>> they enter the ObjectName that it already exists.
>> The input form has a TextBox for the ObjectName and a "save" button. The
>> latter updates the collection, whose key is the ObjectName, either adding
>> or amending the item in the collection. So I kind of need to do this:
>> 1) Are they really trying to add a new item or update an existing one?
>> If adding then prevent them from overwriting one with the same
>> ObjectName, otherwise if amending then only allow changes to the other data
>> and not the ObjectName.
>> 2) Then update the collection accordingly.
>> Is that clear? It isn't to me. Any input would really be appreciated.
>> p.s. I am trying to avoid having separate Add and Update buttons for
>> I may have oversimplified. The basic gen is that there is a form with a
>> list of some type on the left and an "editor" panel on the right. The user
>> clicks on an item in the list and the data for that item is loaded in the
>> editor. Now the quick way for them to add a new item is to click on a
>> similar one, give it a new name and change some of the other data as
>> required, then click the save button. BUT the new name they use may already
>> exist. ... ...
> I may not be getting the jist as i only have simple code to suggest..
> If datObjects.Exist(sName) Then
> Dim c As Integer = 2
> While dat.Objects.Exist(sName & Str(c))
> Inc c
> sName = sName & Str(c)
> That'll try Name2, Name3 etc till a free name is found.
> OK, you have given me a start. The real issue is that the structured
> naming convention was invented by a very clever person from a town in a
> European country that may-or-may-not end in "ermany" (or "eutschland" if
> you prefer). A name looks like this "ind_cl3_1x2_E0_03_09". Zounds egad!
> you may exclaim*. That "_03_" is an index (that indicates a color variant),
> so I can at least use your idea there. I think that I may have to denature
> that name into some sort of structure somehow, which is a damn problem as
> the so called structure seems to be an idea rather than a standard and
> there are many, lets say, "irregularities" in usage. 🙁
> * The "full" explanation of that name is "this is an industrial building
> in climate zone 3 that occupies 2 building blocks (1 wide by 2 long) that
> can or was only be built in or after "epoch" 0**, color variant 3 and an 09
> something (that is yet to be determined). You can see, I believe why users
> have some kind of difficulty in creating or copying new names.
> ** Australia is a wonderful place, there are only two epochs "before" and
> "after" a certain Captain Arthur Phillip
> <https://en.wikipedia.org/wiki/Arthur_Phillip> arrived. Actually there
> are really 3 but I wont go into that piece of political prehistory.
So i thought on these lines...
Created a class called Collection.class
' Gambas class file
Property NoOverwrite As Boolean Use $bNoOverwrite
Property StartValue As Integer Use $bStartValue = 1
Public Sub Add(Value As Variant, (Key) As String)
Dim iNumber As Integer = $bStartValue
Dim sKey As String = Key
If $bNoOverwrite And If Super.Exist(Key) Then
While Super.Exist(Key & Str(iNumber))
sKey = Key & Str(iNumber)
then i tried this...
Dim c As Collection = New Collection
c.NoOverwrite = True
c["ka"] = "ooo"
c["kb"] = "ooo"
c["kc"] = "ooo"
For Each s As String In c
Print c.Key, s
So there i can use c[sKey] and the value is overwritten if the key exists.
but if i use c.Add(vValue, sKey) and the key already exists then a number
is added to the end of the keyname
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the User