RSS

Dynamics NAV 2009 R2: DotNet System.String

21 Sep

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.

 
Leave a comment

Posted by on 2015-09-21 in Okategoriserade

 

Tags: , , , ,

Leave a comment

 
Kauffmann @ Dynamics 365 Business Central

A good programmer makes all the right mistakes.

The BC Docs Librarian

Tips and news for AL programmers

Stefan Maroń

Dynamics 365 Business Central

James Pearson

Thoughts about Dynamics 365 Business Central, Git, Azure DevOps, PowerShell, Automated Testing, Visual Studio Code...and maybe some other stuff

Roberto Stefanetti BLOG

MVP & MCT Microsoft Dynamics 365 Business Central, Powershell, Azure, SQL Server

JoeBrown

DevOps starts with culture!

Comments for DevOps ABCs Blog

Reflections from the ERP field

DevOps ABCs Blog

Reflections from the ERP field

Dynamics 365 Business Central for Partners

Reflections from the ERP field

Freddys blog

Learn something new... - then Teach someone!

Stefano Demiliani Technical Blog

Reflections from the ERP field

David Worthington's NAV Performance Blog

Micosoft Dynamics NAV Performance Testing & Optimization

Freddys Blog

Reflections from the ERP field

Marije...

Storytelling and content creation

Vjeko.com

Ideas in the cloud

Goprowe AB

Reflections from the ERP field

ArcherPoint

Reflections from the ERP field

The NAV Viking´s Blog

- Sharing my experience in the Dynamics NAV world

Comments for

Reflections from the ERP field