String Replace

S

shapper

Hello,

I need to replace some text on the values IDictionary<String, String>
_scripts:

foreach (KeyValuePair<String, String> s in _scripts) {
s.Value.Replace("$Database", Database);
s.Value.Replace("$Login", Login);
}

However, the values are not replaced. I think I need to use the
following:

s.Value = s.Value.Replace("$Database", Database);

But the problem is that I can't do this in a IDictionary value.

How can I solve this problem?

Thank you,

Miguel
 
A

Arne Vajhøj

I need to replace some text on the values IDictionary<String, String>
_scripts:

foreach (KeyValuePair<String, String> s in _scripts) {
s.Value.Replace("$Database", Database);
s.Value.Replace("$Login", Login);
}

However, the values are not replaced. I think I need to use the
following:

s.Value = s.Value.Replace("$Database", Database);

But the problem is that I can't do this in a IDictionary value.

How can I solve this problem?

The easy solution is to do the replace just before you add the
value to the IDictionary<String, String> in the first place.

If you insist on later then try:

foreach (KeyValuePair<String, String> s in _scripts) {
_scripts[s.Key] = s.Value.Replace("$Database",
Database).Replace("$Login", Login);
}

Arne
 
A

Arne Vajhøj

I need to replace some text on the values IDictionary<String, String>
_scripts:

foreach (KeyValuePair<String, String> s in _scripts) {
s.Value.Replace("$Database", Database);
s.Value.Replace("$Login", Login);
}

However, the values are not replaced. I think I need to use the
following:

s.Value = s.Value.Replace("$Database", Database);

But the problem is that I can't do this in a IDictionary value.

How can I solve this problem?

The easy solution is to do the replace just before you add the
value to the IDictionary<String, String> in the first place.

If you insist on later then try:

foreach (KeyValuePair<String, String> s in _scripts) {
_scripts[s.Key] = s.Value.Replace("$Database",
Database).Replace("$Login", Login);
}

Which does not work, because the collection is considered
modified so the enumerator throws an exception.

We may have to copy the keys then:

foreach (string k in _scripts.Keys.ToArray()) {
_scripts[k] = _scripts[k].Replace("$Database",
Database).Replace("$Login", Login);
}

Arne
 
S

shapper

Correct.  The Replace() method returns a new string, rather than
modifying the original.  Strings are immutable.  Always.



KeyValuePair<TKey, TValue> is a value type.  Changing the Value property
of the instance in the loop will have no effect at all on your
dictionary.  It has nothing to do with using an interface versus the
original type.

One good approach is to do what Arne suggested.

Alternatively, you could just create a whole new dictionary instance,
initializing it as you go through the original dictionary and then once
done, replacing the original reference with the new one.  It will use
more memory, but might be easier to read.

Pete

Thank you once again to you both.

Is is working fine now.

Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top