API 2.0 Conversion: Compiler suggests replacement that doesn't work

Hi,

i have the following function:

If I replace Right with String.Right I get a compiler error.

What do these three lines of API 2.0 code look like?

Of course I could rebuild the code that way, but then the function will be longer. Couldn’t this be shorter?

[code]Var hexColor() As String

If hasPrefix Then

hexColor.AddRow("#")

End If

Var r As String = “0” + Hex(c.Red)
Var g As String = “0” + Hex(c.Green)
Var b As String = “0” + Hex(c.Blue)

hexColor.AddRow(r.Right(2))
hexColor.AddRow(g.Right(2))
hexColor.AddRow(b.Right(2))

Return String.FromArray(hexColor, “”)[/code]

Perhaps you need to add:

[code]Var tmp as String
tmp = “0” + Hex (c.Red)

hexColor.AddRow (tmp.Right(2))[/code]

etc.

Right. That makes my short code longer. :frowning:

hexColor.AddRow(CType("0" + hex(c.Red), String).Right(2)) hexColor.AddRow(CType("0" + hex(c.Green), String).Right(2)) hexColor.AddRow(CType("0" + hex(c.Blue), String).Right(2))

Not very intuitive I know, but we work with what we’re given. Even the docs for Hex don’t show this and show the API 1 route.

What do you mean with

?

[quote=490890:@Martin Trippensee]What do you mean with

?[/quote]

See the examples at the bottom of http://documentation.xojo.com/api/text/hex.html ?

They are still using API 1 Right() and not string.right()

In using such as .Right(2), is one supposed to be able to do such as:

("0" + hex(c.Red)).Right(2)

which is what the complier seems to be saying to the OP in his posted code. Trouble is, if I try what I’ve put in the post, all I get is “Syntax Error”. Unless one can do this, either the OP lives with his Warning, or he uses a tmp string as I said upthread.

If you just want the hex value for the color, would this work for you?

Var hexValue As String = Str(c).Right(6)

[quote=490893:@Paul Lefebvre]If you just want the hex value for the color, would this work for you?

Var hexValue As String = Str(c).Right(6)

Excellent Paul, thank you very much.

[quote=490889:@]hexColor.AddRow(CType("0" + hex(c.Red), String).Right(2)) hexColor.AddRow(CType("0" + hex(c.Green), String).Right(2)) hexColor.AddRow(CType("0" + hex(c.Blue), String).Right(2))

Not very intuitive I know, but we work with what we’re given. Even the docs for Hex don’t show this and show the API 1 route.[/quote]

A Text literal, or string literal, isnt a string hence why you need that ugly dance
.Right is an extension on String - which doesnt work for a string literal
Would be nice if it did as the API 1 syntax is simpler for some things because of this
<https://xojo.com/issue/56629>

You could also use the tohex function of integer http://documentation.xojo.com/api/data_types/integer.html#integer-tohex which has an optional parameter for the minimum number of digits so

hexColor.AddRow(c.Red.ToHex(2)) hexColor.AddRow(c.Green.ToHex(2)) hexColor.AddRow(c.Blue.ToHex(2))

should do it for you.

norman is back, YAY!

Thanks Wayne - Integer.ToHex returns Text instead of String. So it’s not my first choice.

This would no longer be Right(StringVariable,5) - but:
StringVariable.Right(5)

[quote=490951:@Detlef Kahner]This would no longer be Right(StringVariable,5) - but:
StringVariable.Right(5)[/quote]

Why can’t I say:

StringExpression.Right (5)

I’ve not seen an answer to that yet.

Compiler limit / choice. I also regret this because it makes life more complicated or for inefficient code (CType)
Xojo is no Python in this regard.

[quote=490954:@Tim Streater]Why can’t I say:

StringExpression.Right (5)

I’ve not seen an answer to that yet.[/quote]
Because thats not how the compiler works ?
I dont think there’s anything more sophisticated than that.
It sure would be handy though esp with the API 2 changes

When I want to check that all characters in a TextField are number, I loop on each character doing “0123456789”.IndexOf(ThisChar) but I can’t, I have to define a string (constant or variable).
I wax able to do it with Instr(“0123456789”, ThisChar)
(yes I know there is TextField.Mask, but it can be a value I read in a text file or etc. ).

That is one of the use cases mentioned on the report I referred to earlier

You have the following choices as far as I can tell

  1. create a variable for this (but then you dont have the same immutability as you do with a literal)
  2. create a constant like
        const numbers as string = "0123456789"
        var index as integer = numbers.instr( valueToCheck )    

and you MUST specify a type if you use a locally defined constant like above

  1. Or you use the old API 1 syntax