If you are having the need to use DotNet in an older client such as Dynamics NAV 2009 R2 you will problably run into troubles which they have fixed for later versions of Dynamics NAV. I had the problem that I tried to downgrade code using the DotNet type System.String. I didn’t get very far until the compiler started to complain…
I can’t assign any values to my variable. Instead I get “Assignment is not allowed for this variable“. Hmmm.. But then I tried to use another methods to add values to my System.String variable.
I looked at the System.String on msdn and found the method Insert. So how about Variable.Insert(0, <MyString>) ?
Unfortunately that didn’t work either. The error I received was “The DotNet variable has not been instantiated“.
Okay, how about using the constructors? Well that didn’t work either. The solution became to use StringBuilder instead (System.Text.StringBuilder). I could use that as a string container and it also had the method that I was looking for in the first place, Replace.
So I replaced my Original code:
DotNetSystemString := DotNetSystemIOFile.ReadAllText(FileName);
DotNetSystemString := DotNetSystemString.Replace('OldText','NewText');
DotNetSystemIOFile.WriteAllText(FileName,DotNetSystemString.ToString);
With:
DotNetSystemTextStringBuilder := DotNetSystemTextStringBuilder.StringBuilder(DotNetSystemIOFile.ReadAllText(FileName));
DotNetSystemTextStringBuilder.Replace('OldText','NewText');
DotNetSystemIOFile.WriteAllText(FileName,DotNetSystemTextStringBuilder.ToString);
I hope that helps you.