Array property definition

I want an array of classes as a protected property m_PhraseExceptionList() with a public read-only property PhraseExceptionList() out in front of it. This array would be initially empty; it will be loaded with some unknown number of elements at runtime.

The class that the array holds is just a container for 2 string properties. It is called PhraseRule.

I have the protected property defined as follows:

Protected Property m_PhraseExceptionList() As PhraseRule

This produces a compile-time syntax error.

If I change it to:

Protected Property m_PhraseExceptionList(0) As PhraseRule

… the property compiles, but attempts to work with it yield “Type mismatch error. Expected class PhraseRule, but got class PhraseRule”.

So ignoring the incoherent error message … what am I doing wrong?

Type mismatch error.  Expected class PhraseRule, but got class PhraseRule
Return m_PhraseExceptionList

Also these 2:

Type mismatch error.  Expected String, but got Int32
paddedString = paddedString.Replace(paddedString, pr.Phrase, pr.Correction)

There is more than one method with this name but this does not match any of the available signatures.
paddedString = paddedString.Replace(paddedString, pr.Phrase, pr.Correction)

Those last 2 I have seen when the compiler is just confused or fails to actually define a type, and then runs home to Int32, and hilarity results. So I think the actual problem is that while the property definition is compiling, it’s still not making it clear to the compiler what I actually want.

For thoroughness, here’s the PhraseRule class definition:

Protected Class PhraseRule

		Sub Constructor(p As String, c As String)
		  Phrase = p
		  Correction = c
		End Sub

		Correction As String
		Phrase As String

End Class

If PhraseRule is on a Module / Namespace you need to include that in the definition.

Private Property m_PhraseExceptionList() As MyNamespace.PhraseRule

No, it’s just defined in the app, no modules.

I can’t reproduce your syntax error: array_prop.xojo_xml_project

I was, and in fact when I tried to take the parens off, the IDE insisted on putting them back on. To change from () to (0) and get rid of the initial error I had to rename the property and then name it back to what I wanted it to be.

Now I just changed (0) to () and got no compilation error. So we can chalk that up to the IDE getting its knickers in a twist somehow.

Either way I’m left with the downstream errors. In the getter for the public PhraseExceptionList property, I can’t return an array of PhraseRules as it seems to be expecting an individual PhraseRule (I assume that’s it’s malfunction anyway). In another method I have a for loop:

For Each pr As PhraseRule In m_PhraseExceptionList

… and it doesn’t seem to have the slightest idea what type pr is (or has the idea it’s an integer).

Computed Properties haven’t allowed arrays for as long as I’ve known the framework.

2 Likes

It’s not a computed property, it’s just a private property. It’s not even complaining about the public version.

Oh, you’re calling that a computed property … sorry, .NET terminology clash there.

OK fine … I’ll have to do something crufty around that …

You had said “In the getter for the public PhraseExceptionList property” so I assumed you had meant you’d created a Computed Property and were using the Setter / Getter of that. Arrays are not supported for this feature.

You can store arrays as properties. I’ve posted an example project illustrating the syntax for this earlier. You have to create your own functions to return arrays of things, not the Computed Property feature.

Really there is no difference in this case between a computed property and returning the array in a function. The latter won’t show up in the debugger, but you’ll have access to the shadow property anyway.

HOWEVER, keep in mind that if you return the original array, the caller can manipulate it. That may or may not be acceptable. If it’s not, return a copy of the array on each function call.

I’ve updated the demo project to show how to use a for each loop with an array property. Short of seeing an example where it’s not working, I’m not sure how else to help you.

array_prop.xojo_xml_project

I worked around this by pretending this was Java and wrote a get_PhraseExceptionList method.

Also I had brainfarted the syntax of string.replace. It wasn’t working for unrelated reasons when I was thrashing earlier and I misread the syntax help and added an extra argument that didn’t belong.

Where I come from, “m_Whatever” or “_whatever” would be a private class field and “public Whatever” would be a public class property. Hence my terminology. Either one returns a type so I don’t see why one would support array types and one would not, so that hadn’t occurred to me. It’s a crufty limitation but this is just a crufty little CRUD app really so I don’t care all that much. If this were code I had to revisit often, the inconsistency would irritate me, but this one is largely fire-and-forget.

This was my first cross platform deployment with Xojo (MacOS to Windows and Postgres to Sql Server). I anticipated reworking the SQL but apart from that, even though there were no declares or fancy design elements or even any graphics, porting this puppy from MacOS to Windows involved a couple of joyless days of head scratching because DB access behaves differently in substantive ways and the Xojo-supplied drivers for Sql Server and Odbc felt like abandonware so I had to resort to MBS, which itself was behaving differently from the mostly excellent Xojo Postgres driver – but Christian provided excellent support and diagnoses, which got me past the differences.

Visually … control sizes are off a few pixels and some textboxes graze each other, but this is for internal use so acceptable if not impressive. I think the client will want to reproduce this functionality in an administrative web app eventually anyway or else I’ll rewrite and extend it using .NET Maui at some point. But this will tide me over okay.

Thanks for the help, Tim, I appreciate it. :slight_smile: