Solved

C## need to fix my string syntax concantenate

Posted on 2008-06-23
8
276 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
ID: 21847236
Concatenation in C# uses the + and not the & operator.
 
mystring1 = mystring + j.ToString;
0
 
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);
0
 

Author Comment

by:mphillip85
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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:
http://blogs.crsw.com/mark/archive/2004/08/06/449.aspx
0
 
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
0
 
LVL 18

Accepted Solution

by:
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);

or

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

Author Comment

by:mphillip85
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;

0
 
LVL 55

Expert Comment

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

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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