0

In C# I can do this:

public string[] MyStrings;
...
...
if(MyStrings.Contains("bob")) ...

In ObjectScript how is this done?

With %ArrayOfObjects type I don't see exactly what I am looking for here

What I have tried:

#Dim MyStrings As %ArrayOfDataTypes
do MyStrings.SetAt("User","User")   
do MyStrings.SetAt("Users","Users")
do MyStrings.SetAt("Group","Group")
do MyStrings.SetAt("Groups","Groups")

// if MyStrings contains Groups
if MyStrings.GetAt("Groups") '= ""
{
}
O.O
  • 11,077
  • 18
  • 94
  • 182

3 Answers3

2

It sounds like you want the .IsDefined() method. For example:

#Dim MyStrings As %ArrayOfDataTypes
do MyStrings.SetAt("User","User")   
do MyStrings.SetAt("Users","Users")
do MyStrings.SetAt("Group","Group")
do MyStrings.SetAt("Groups","Groups")

// if MyStrings contains Groups
if MyStrings.IsDefined("Groups")
{
    // code to execute if MyStrings contained "Groups"
}
Derek
  • 952
  • 3
  • 13
  • 34
1

I am not familiar with that language, but could you try something like:

//declare myString to hold a string
set myString = MyStrings.GetNext("")
While myString '= ""
{
    if MyStrings.GetAt(myString) '= ""  //Or should it be something like myString.value?  Is there a way to check the type of an object to see if it is a string?
    {
        //Do something here.  Exit loop if you are trying to find just a match.
    }
}

I am assuming '= is equivalent to != in C#.

Trisped
  • 5,705
  • 2
  • 45
  • 58
  • Yes '= is not equal and there is no null, only "". Nice try, but I think there might be an easier way. – O.O Nov 02 '12 at 20:24
  • In this case you don't want to iterate the whole array (which is really more like a dictionary) because GetAt actually gets the value stored by that key. Since in this case the key and value are the same, just doing GetAt(myString) is all you need. – psr Nov 02 '12 at 20:40
  • @psr the OP did not state that the key and value would be the same. It also did not guarantee that the values would be strings. – Trisped Nov 02 '12 at 20:51
  • @Trisped - I am assuming the key and value are the same. If not, we need to know if he is testing the key or the value. Your code tests the value correctly though it isn't quite a complete example in the Cache Object Script language. That language has only one data type, so he didn't need to specify it. – psr Nov 02 '12 at 20:54
1

You've got it right. I would suggest making a subclass of %Library.ArrayOfDataTypes with your own methods on it, such as "contains".

It's probably a little safer to use your own classes that you do control than library classes that you don't anyway (though in a pinch you could always use %Dictionary package methods to switch all references to a library class to a new class of your design, so it's not really that big a deal).

psr
  • 2,870
  • 18
  • 22
  • Good idea. Why is it safer to use my own classes? – O.O Nov 02 '12 at 20:24
  • If you use the class in 500 places and then for whatever reason want to change the class, you can't do it. (Well, you *can*, but your change would get overridden every time you upgraded). If it's your own class, you can. (But, as I said, you could programmatically change all your references from an Intersystem's class to your own class anyway, so it's not actually that big a deal). – psr Nov 02 '12 at 20:31
  • Oh, I understand about reuse. But it seems like you are saying Intersystems may change their code and not have it be backwards compatible. Yikes! Thanks. – O.O Nov 02 '12 at 20:40
  • No, I'm not really saying that. It's more that if need to refactor your code, then you can, but you can't refactor their code. As far as breaking changes go, it's safer to encapsulate *any* outside code, in any environment. But it's a risk/reward thing and I highly doubt %ArrayOfDataTypes will have breaking changes down the road. – psr Nov 02 '12 at 20:45