Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Closed Unassigned: Issue concatenating java string and c sharp string [55]

$
0
0
This is in the basic java engine (not v8)

I have some basic code here (in javascript):
```
var accountstr = host.newVar(CLRString);
if (!downloadInfo.TryGetValue("Account", accountstr.out))
{
accountstr = "No Key found";
}
var output = "_" + accountstr;

return output;
```

This should be returning "_908561", but is instead giving me "_undefined". When I remove the underscore concatenation, I get the proper result.

Is there some sort of trick when concatenating a c sharp string and a javascript string?


EDIT:
I managed to find a solution to my problem. For some reason, I have to call .ToString on my System.String. Very weird I had to do this, but it worked.
Comments: Hello! The short answer is that this is working as intended :) Here's the long answer: JavaScript's `+` operator, when its first operand is a string, attempts to convert the second operand to a string by calling its standard `toString()` method. In this case the second operand is a host object. In JScript, host objects are "special" and don't have standard `toString()` methods. Therefore you must explicitly convert the host object to a string. An easy way to do that is to call the host object's _managed_ `ToString()` method, as you discovered. Another possibility, since the host object is a string variable, is to retrieve its value explicitly: ``` JavaScript var output = "_" + accountstr.value; ``` By the way, your error path discards the string variable and replaces it with a native JavaScript string, which has neither a `ToString()` method nor a `value` property. To make the whole thing work, try this: ``` JavaScript var accountstr = host.newVar(CLRString); if (!downloadInfo.TryGetValue("Account", accountstr.out)) { accountstr.value = "No Key found"; } var output = "_" + accountstr.value; return output; ``` Good luck!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>