Skip to main content
The 2024 Developer Survey results are live! See the results
clarified no duplicate
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question and it is not a duplicate of any other questions on the site. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

I am trying to find a clear answer to my question and it is not a duplicate of any other questions on the site. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

just corrected minor typos
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56

I was converting a VBA macro to C#. And in VBA chr(7) is simply can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questionquestions on this on SO and several other sites. For example this one thatwhich is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is thethis: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

I was converting a VBA macro to C#. And in VBA chr(7) is simply can be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related question on this on SO and several other sites. For example this one that is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is the do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on or even examples would do. Thanks in advance.

I was converting a VBA macro to C#. And in VBA chr(7) can simply be concatenated to a string as if chr() would yield a string. Why can't this be done in C#?

I am trying to find a clear answer to my question. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?

And unfortunately the answer is not clear and many times they state that this is a correct use:

string mystring=(char)7;

Yet it gives me a compiler error as it does not evaluate as a string.

I had to use this to make it work:

string mystring=((char)7).ToString();

This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.

My question is this: do I always need to cast the char over to string explicitly or there are some cases where it converts over implicitly?

UPDATE:

Per @Dirk's answer, this also works:

string mystring = "" + (char)7;

This does not lessen my mystery. If concatenation works why there is no implicit cast??

I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.

edited the beginning of the question to make it clearer
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading
edited tags
Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading
Notice removed Draw attention by ib11
Bounty Ended with Hans Passant's answer chosen by ib11
update
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading
updated with more data
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading
added 107 characters in body
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading
Notice added Draw attention by ib11
Bounty Started worth 50 reputation by ib11
Source Link
ib11
  • 2.6k
  • 3
  • 23
  • 56
Loading