Question

Is the function System.String:Format is limited to 3 input?

When I do:

DISPLAY System.String:Format("~{0~} ~{1~} ~{2~}", 0, 1, 2).

Everything is fine.

But when I do:

DISPLAY System.String:Format("~{0~} ~{1~} ~{2~} ~{3~}", 0, 1, 2, 3).

I get a compile error:

Impossible to find a method 'Format' with a compatible signature  from class 'System.String'. (14457)

I found a workaround that looks like:

System.String:Format("~{0~} ~{1~} ~{2~}", 0, 1, System.String:Format("~{0~} ~{1~} ~{2~}", 2, 3, System.String:Format("~{0~} ~{1~} ~{2~}", 4, 5, 6))).

But I do not found it elegant.

Thank you! Sebastien

UPDATE:

Here an example where format is used to format the datetime, leading zero and round decimal.

/* constants */
DEFINE VARIABLE MSG_WELCOME AS CHARACTER NO-UNDO INITIAL "Hello user  ~{0~:d6}, your balance account is ~{1~:n2} in date of ~{2~:yyyy-MM-dd}.".

/* declaration */
DEFINE VARIABLE iUserId AS INTEGER NO-UNDO.
DEFINE VARIABLE dtTransaction AS DATETIME NO-UNDO.
DEFINE VARIABLE dBalance AS DECIMAL NO-UNDO.
DEFINE VARIABLE strMessage AS CHARACTER NO-UNDO.

/* initialization */
iUserId = 106.
dtTransaction = DATETIME(10, 31, 2014, 11, 22, 33).
dBalance = 1234.56789.
strMessage = System.String:Format(MSG_WELCOME, iUserId, dBalance, dtTransaction).

/* output */
MESSAGE strMessage VIEW-AS ALERT-BOX INFO BUTTONS OK.

OUTPUT: Hello user 000106, your balance account is 1234,57 in date of 2014-10-31.

Was it helpful?

Solution

By looking at the available overloads of System.String.Format this becomes less mysterious:

public static string Format(string format, object arg0);
public static string Format(string format, params object[] args);
public static string Format(IFormatProvider provider, string format, params object[] args);
public static string Format(string format, object arg0, object arg1);
public static string Format(string format, object arg0, object arg1, object arg2);

By adding the fact that OpenEdge does not support .Net vararg parameters (defined with the params keyword), it becomes clear. Your first example is using the last listed version with 3 object parameters, your second example does not work because there is no overload with 5 parameters.

But it's not impossible to use vararg parameters from OpenEdge. If we look at autocompletion from PDSOE, we see that all 5 signatures are there and that the params keyword is simply ignored. That means you could use the highlighted version with unlimited parameters, but you need to pass an array of type System.Object to it.

enter image description here

This is an example of how it could be done:

DEF VAR v-list AS System.Collections.ArrayList NO-UNDO.

v-list = new System.Collections.ArrayList().

v-list:Add(106).
v-list:Add(DATETIME(10, 31, 2014, 11, 22, 33)).
v-list:Add(1234.56789).
v-list:Add("moo").

MESSAGE System.String:Format("~{0~} ~{1~} ~{2~} ~{3~}", v-list:ToArray())
VIEW-AS ALERT-BOX.

OTHER TIPS

SUBSTITUTE() can be used to replace up to 9 variables with values. The function is somewhat similar to that of sprintf in C. The (perhaps bad) side effect is that the formatting isn't in the string itself but in the function call.

Example:

DEFINE VARIABLE MSG_WELCOME AS CHARACTER NO-UNDO INITIAL "Hello user  &1, your balance account is &2 in date of &3.".

DEFINE VARIABLE iUserId AS INTEGER NO-UNDO.
DEFINE VARIABLE dtTransaction AS DATETIME NO-UNDO.
DEFINE VARIABLE dBalance AS DECIMAL NO-UNDO.
DEFINE VARIABLE strMessage AS CHARACTER NO-UNDO.

iUserId = 106.
dtTransaction = DATETIME(10, 31, 2014, 11, 22, 33).
dBalance = 1234.56789.

strMessage = SUBSTITUTE( MSG_WELCOME, STRING(iUserId, "999999"), STRING(dBalance,">>>,>>>.99"), STRING(dtTransaction,"9999-99-99")).

MESSAGE strMessage VIEW-AS ALERT-BOX.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top