Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i have use the follwing code
str=str.Replace("a",dictionary[key])
but is in note replacing

actually Dictionary[key] contains a list of value.

it is replace by this

System.Collections.Generic.List`1[System.Object]
not with the exact value.
Posted
Comments
Maarten Kools 21-Jan-14 7:45am    
Your dictionary returns a List<Object>, and by passing this to the Replace method, the list's ToString method is called. Maybe if you post some more code we can properly help you out.
rahuls1 21-Jan-14 7:47am    
foreach (string key in dictionary.Keys)
{
if (key == "Precondition")
{
List<object> values = dictionary[key];
strContent = strContent.Replace("init_step_here", dictionary[key].ToString());
//strContent = strContent.Replace("stimulation_step_here", dictionary[key].ToString());
//strContent = strContent.Replace("reset_step_here", dictionary[key].ToString());
//strContent = strContent.Replace("eval_step_here", dictionary[key].ToString());

}

}
rahuls1 21-Jan-14 7:47am    
abve is the code..
BillWoodruff 21-Jan-14 9:58am    
A generic Dictionary requires that Keys be unique: so, for me, your code doesn't make sense: there can be only one Key with the value "Precondition" !

"List<object> values = dictionary[key];" also makes no sense to me.

What is your goal in this application ? If you describe your goal clearly, I am sure folks here will assist you.

better to write a helper function to convert List<object>'s to a meaningful string or select an object from the list, then convert it to a string. it depends on your aim.

One helper might be:

C#
string MyListStringRepresentation(List<object> l) {
 string s = string.Empty;
 l.ForEach(l1=>s+=l1.ToMyString());
 return s;
}

string ObjectStringRepresentationAt(List<object> l, int idx) {
 return l[idx].ToMyString();
}</object></object>


then you can use above functions when replacing a text in your code. there are really many options here. it depends on what you want to do.

besides that, I advice you not to use List<object> which is very ambiguous. use type-safe declarations like List<string>, List<SomeClass>, List<SomeInterface>, List<SomeAbstaction>. you have .net framework, use its power. even my helper functions is not good enough in terms of my suggestion. I can use extension methods, overrides etc.
 
Share this answer
 
Using ToString on a collection of any sort does not return the content - it returns the name of the collection type as a string instead, because none of the standard collection objects implement a ToString overside - so you get the stardard Object implementation, which gives the highest possible type name in the inheritance try for that object. Hence the replacement with "System.Collections.Generic.List`1[System.Object]".

If you want to replace a single string with a collection of strings, then you could do this:
C#
str=str.Replace("a",string.Join("?", dictionary[key]));
Where "?" is the value you want between each of the strings in the values collection.
 
Share this answer
 

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