Solved

C## need to fix my string syntax concantenate

Posted on 2008-06-23
8
274 Views
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;
0
Comment
Question by:mphillip85
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 62

Expert Comment

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

Expert Comment

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

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

Author Comment

by:mphillip85
Comment Utility
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
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
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
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
Comment Utility
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
 

Author Comment

by:mphillip85
Comment Utility
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
 
LVL 55

Expert Comment

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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now