C## need to fix my string syntax concantenate

Posted on 2008-06-23
Last Modified: 2010-05-18
for (int j = 0; j < this.dataGridView1.ColumnCount - 1; j++)
                    char mystring;
                    string mystring1;
                    mystring = (char)65;
                    mystring1 = mystring & j.ToString;
Question by:mphillip85
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 63

Expert Comment

by:Fernando Soto
ID: 21847236
Concatenation in C# uses the + and not the & operator.
mystring1 = mystring + j.ToString;
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21847349
indeed it should be:
mystring1 = mystring + j.ToString();

but I prefer:
mystring1 = string.Format("{0}{1}", mystring, j);

Author Comment

ID: 21847417
it gives me an error to add a char to a string.  I am converting a integer to a letter and appending a number to the end for cycling to a spreadsheet.
Such as char(65) = A + number.tostring = 1, then change to char(66) = B + number.tostring = 2, etc.

I hope this is more clear as my intentions.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 18

Expert Comment

by:Richard Lee
ID: 21847443
All of the following will work, but the latter two address the problem of string being immutable in .NET.

mystring1 = mystring + j.ToString();
mystring1 += j.ToString();
mystring1 = String.Format("{0}{1}", mystring, j);
mystring1 = String.Concat(mystring, j);

See reference for immutable:
LVL 18

Expert Comment

by:Richard Lee
ID: 21847479
If you wish to add a number to a char simple to the following:

char c = (char)65;
c++; // This increment to 66 / B

// You can also do this:
c += 5; // This should give you F
LVL 18

Accepted Solution

Richard Lee earned 500 total points
ID: 21847534
I forgot to mention this option in the previous comment:

char c = (char)65;
string s = "1";

// Use on of the following
int result2 = c + Convert.ToInt32(s);


int result3 = c + int.Parse(s);

Author Comment

ID: 21847679
1.  need to convert nuber to char;
2.  need to convert nuber to string;
3.  need to add both together to get "A1" , "A2", "B1", "B2", etc;

LVL 55

Expert Comment

by:Jaime Olivares
ID: 21847959
try with this:
mystring1 = mystring.ToString() + j.ToString();


Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question