C# Static cast char to string
up vote
2
down vote
favorite
I am trying to declare a constant char to hold a key and a constant string to hold a message telling the user to press the key:
...
private const KEY = 'r';
private const string MSG = "Press " + KEY + " to restart.";
...
I need to explicitly cast the key char to a string since the implicit cast is done during runtime. However, I can't figure out the way to cast a character to a string at compile time. I've seen ToString () on the internet, but it's performed at runtime and therefore doesn't work. I have the key char as a separate variable because it is used several times in the program. Does anybody know how to statically cast a char to a string?
c# string type-conversion character string-concatenation
add a comment |
up vote
2
down vote
favorite
I am trying to declare a constant char to hold a key and a constant string to hold a message telling the user to press the key:
...
private const KEY = 'r';
private const string MSG = "Press " + KEY + " to restart.";
...
I need to explicitly cast the key char to a string since the implicit cast is done during runtime. However, I can't figure out the way to cast a character to a string at compile time. I've seen ToString () on the internet, but it's performed at runtime and therefore doesn't work. I have the key char as a separate variable because it is used several times in the program. Does anybody know how to statically cast a char to a string?
c# string type-conversion character string-concatenation
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to declare a constant char to hold a key and a constant string to hold a message telling the user to press the key:
...
private const KEY = 'r';
private const string MSG = "Press " + KEY + " to restart.";
...
I need to explicitly cast the key char to a string since the implicit cast is done during runtime. However, I can't figure out the way to cast a character to a string at compile time. I've seen ToString () on the internet, but it's performed at runtime and therefore doesn't work. I have the key char as a separate variable because it is used several times in the program. Does anybody know how to statically cast a char to a string?
c# string type-conversion character string-concatenation
I am trying to declare a constant char to hold a key and a constant string to hold a message telling the user to press the key:
...
private const KEY = 'r';
private const string MSG = "Press " + KEY + " to restart.";
...
I need to explicitly cast the key char to a string since the implicit cast is done during runtime. However, I can't figure out the way to cast a character to a string at compile time. I've seen ToString () on the internet, but it's performed at runtime and therefore doesn't work. I have the key char as a separate variable because it is used several times in the program. Does anybody know how to statically cast a char to a string?
c# string type-conversion character string-concatenation
c# string type-conversion character string-concatenation
asked Nov 11 at 2:30
Andrew Pratt
254
254
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The short answer is that you can't compose a string
with anything other than string
fragments.
You have a few options though. You can make both constants string
types:
private const string KEY = "r";
private const string MSG = "Press " + KEY + " to restart.";
Another option would be to compose the string
at runtime:
private const char KEY = 'r';
private static string MSG => "Press " + KEY + " to restart.";
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
add a comment |
up vote
1
down vote
You can't const string
with other values which is const
.
You can try to use readonly
.
readonly
can only modify values in the class constructor method, it will set that value at runtime.
private const char KEY = 'r';
private readonly string MSG = "Press " + KEY + " to restart.";
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The short answer is that you can't compose a string
with anything other than string
fragments.
You have a few options though. You can make both constants string
types:
private const string KEY = "r";
private const string MSG = "Press " + KEY + " to restart.";
Another option would be to compose the string
at runtime:
private const char KEY = 'r';
private static string MSG => "Press " + KEY + " to restart.";
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
add a comment |
up vote
1
down vote
accepted
The short answer is that you can't compose a string
with anything other than string
fragments.
You have a few options though. You can make both constants string
types:
private const string KEY = "r";
private const string MSG = "Press " + KEY + " to restart.";
Another option would be to compose the string
at runtime:
private const char KEY = 'r';
private static string MSG => "Press " + KEY + " to restart.";
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The short answer is that you can't compose a string
with anything other than string
fragments.
You have a few options though. You can make both constants string
types:
private const string KEY = "r";
private const string MSG = "Press " + KEY + " to restart.";
Another option would be to compose the string
at runtime:
private const char KEY = 'r';
private static string MSG => "Press " + KEY + " to restart.";
The short answer is that you can't compose a string
with anything other than string
fragments.
You have a few options though. You can make both constants string
types:
private const string KEY = "r";
private const string MSG = "Press " + KEY + " to restart.";
Another option would be to compose the string
at runtime:
private const char KEY = 'r';
private static string MSG => "Press " + KEY + " to restart.";
answered Nov 11 at 2:43
DavidG
67.1k9106122
67.1k9106122
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
add a comment |
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
Thanks, I wanted to avoid this initially, but I ended up needing it converted to a string every time I needed it, so it makes sense to store it as a string
– Andrew Pratt
Nov 11 at 3:39
add a comment |
up vote
1
down vote
You can't const string
with other values which is const
.
You can try to use readonly
.
readonly
can only modify values in the class constructor method, it will set that value at runtime.
private const char KEY = 'r';
private readonly string MSG = "Press " + KEY + " to restart.";
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
add a comment |
up vote
1
down vote
You can't const string
with other values which is const
.
You can try to use readonly
.
readonly
can only modify values in the class constructor method, it will set that value at runtime.
private const char KEY = 'r';
private readonly string MSG = "Press " + KEY + " to restart.";
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
add a comment |
up vote
1
down vote
up vote
1
down vote
You can't const string
with other values which is const
.
You can try to use readonly
.
readonly
can only modify values in the class constructor method, it will set that value at runtime.
private const char KEY = 'r';
private readonly string MSG = "Press " + KEY + " to restart.";
You can't const string
with other values which is const
.
You can try to use readonly
.
readonly
can only modify values in the class constructor method, it will set that value at runtime.
private const char KEY = 'r';
private readonly string MSG = "Press " + KEY + " to restart.";
edited Nov 11 at 3:09
answered Nov 11 at 3:00
D-Shih
24.4k61431
24.4k61431
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
add a comment |
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
Thank you, that would work, but I'm going with davidg's suggestion to change the char to be a string because I ended up only ever needing it as a string. I'll be sure to remember both ways in the future though
– Andrew Pratt
Nov 11 at 3:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245343%2fc-sharp-static-cast-char-to-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown