• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

C## need to fix my string syntax concantenate

for (int j = 0; j < this.dataGridView1.ColumnCount - 1; j++)
                {
                    char mystring;
                    string mystring1;
                    mystring = (char)65;
                    mystring1 = mystring & j.ToString;
0
mphillip85
Asked:
mphillip85
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Fernando SotoRetiredCommented:
Concatenation in C# uses the + and not the & operator.
 
mystring1 = mystring + j.ToString;
0
 
Jaime OlivaresSoftware ArchitectCommented:
indeed it should be:
mystring1 = mystring + j.ToString();

but I prefer:
mystring1 = string.Format("{0}{1}", mystring, j);
0
 
mphillip85Author Commented:
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Richard LeeSoftware EnthusiastCommented:
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:
http://blogs.crsw.com/mark/archive/2004/08/06/449.aspx
0
 
Richard LeeSoftware EnthusiastCommented:
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
0
 
Richard LeeSoftware EnthusiastCommented:
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);

or

int result3 = c + int.Parse(s);
0
 
mphillip85Author Commented:
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;

0
 
Jaime OlivaresSoftware ArchitectCommented:
try with this:
mystring1 = mystring.ToString() + j.ToString();

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now