Variant arguments and string/text

In my thread class, where I have to update the UI I pass messages through to a timer event handler which modifies the UI. For flexibility, the messages passed contain variants. This gives a problem when I pass a fixed string through the variant, thus:

t.UIwriter ("Some stringy data")

where UIwriter is defined as:

[quote]UIwriter (arg1 as variant)

The problem is that this fixed data enters the variant as a String rather than as a Text type, which gives a problem at the receiving end which assumes it is being given a Text item. So I have to do:

dim msg as text msg = "Some stringy data" t.UIwriter (msg)

and all is OK. But this is clumsy. If I happen to do as follows:

[quote]t.UIwriter ("Some string data: " + str(27).ToText)

then the string passed through is Text and again, all is OK.

How can I force fixed strings, as in the first example above, always to be Text and not string?

FYI, you can do this:

t = Integer( 27 ).ToText

But I’m not sure what the problem is here. Why not just have UIwriter do the conversion for you?

if arg1.Type = Variant.TypeText then
  dim s as string = arg1.TextValue
  arg1 = s
end if

Or convert String to Text, if that’s what you prefer.

Presumably it would actually be:

if arg1.Type = Variant.TypeString then dim s as text = arg1.StringValue.ToText arg1 = s end if

And are you saying I can do:

t.UIwriter ("Some string data".ToText)

Sure, you could do that, but be careful. If the String’s encoding is nil, that will generate an exception. If you’re sure your Strings will always be encoded, it’s no problem. Text → String always works.

Not that, no. But it would be an odd construct anyway. If you want to force a string literal to Text, use CType, as in v = CType( "some string", Text ) or assign it to a Text constant like const kMyText as Text = "some string". This is generally not needed as encoded Strings can always be converted to Text, and Text will convert to UTF8-encoded Strings.

However, if you write the method as discussed, it won’t be necessary either way. The method will handle the conversion for you so it won’t matter if you pass in a String or Text.

[quote=288426:@Kem Tekinay]FYI, you can do this:

t = Integer( 27 ).ToText [/quote]

Kem, I had never seen this use of Integer. I can not find it in Language Reference.
Curiously, If I write:

Dim t As Double = Double( 27.2 ) 

I get this error:

Which, of course, can not be understood. At least by me.

Yes, I see. I’d say one of these is a bug. Either you shouldn’t be allowed to do it with an integer, or double should work too.

I discovered this just by experimenting, and I experimented because of this thread. :slight_smile:


It seems to me that there should be a compiler switch saying: “Treat bare text strings as type Text”. Ticked means Text, unticked means String.