Click here to Skip to main content
16,012,082 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
infomessage.Text += item.Tostring();
infoMessage.Text += pair.Key;


These two codes are in a two different methods ( functions). these codes display the messages together as a big string on the web.I can't even do like the following together because they are in the different functions.

C#
infoMessage.Text = string.Format("{0}\r\n{1}", pair.Key, item.ToString());


so I tried the following by keeping them in their method.

C#
infoMessage.Text = string.Format("{0}",item.Tostring());// a string list
infoMessage.Text = string.Format("{1}",pair.Key          // a dictionary list


this time i only get the item.Tostring() displaed but the next one dissapeard.

My objective is to show them both by having a space in between. Ho do I do it.
Posted
Updated 10-Nov-11 3:39am
v2
Comments
Sergey Alexandrovich Kryukov 10-Nov-11 11:45am    
Please don't post any fragments of code which will not compile (unless compilation is your problem). It won't compile due to wrong casing. Always make you post based on paste from actual code.
--SA

Not clear what you mean by "these codes are in two different methods:" in your example: both 'item' and 'pair' (whatever they are) appear as if they are in the same scope in some class.

The answer by KairuByte above does point out what you need to be aware of in using string concatenation.

If you need to call two different methods to get the data you need to join together to produce the desired string result, you might consider writing a function something like
public string BuildInfoMessage()
{
  return Class1Instance.SomeObject.Item.ToString() +  
  Class2Instance.SomeObject.Pair.ToString();
}
The above assumes you have access from where this function is called to valid instances of Class1, and Class2, and that the Objects in those classes in which you access 'Item' and 'Pair' are valid.
 
Share this answer
 
Without looking at the rest of your code, best suggestion would be to do:

C#
infoMessage.Text = string.Format("{0}",item.Tostring());// a string list
infoMessage.Text += string.Format("{0}",pair.Key);               // a dictionary list


Note the += on the second string. It has to be after the initial one or you are just overwriting the current value in infoMessage.Text, while += appends.

And make sure to not forget the semicolon on the second one.

As well, you need to make sure to use the proper parameter identifier. The second time it is a different call to String.Format, which resets the parameter identifiers.
 
Share this answer
 
v5
Comments
BillWoodruff 10-Nov-11 9:53am    
+5 good answer, in spite of the OP providing very little information about other issues implied in his question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900