Bpm strings

Do I require the string.Format in the following syntax:

string engBody = string.Empty;
engBody = 'JobNum: ' + string.Format(System.Convert.ToString(ttLaborDtl_xRow.JobNum));

or can I just use:

engBody = 'JobNum: ' + System.Convert.ToString(ttLaborDtl_xRow.JobNum);

I don’t think you are doing any formatting so I would say the string.Format is not needed.
MS help for string.Format String.Format Method (System) | Microsoft Learn

Side note 1, I thought in c# strings had to be surrounded with " as opposed to ’ ?

Side note 2, I have used Object.ToString() many times before but not System.Convert.ToString(Object) so I looked up the docs and one difference is System.Convert.ToString(Object) will return String.Empty if Object is null while Object.ToString() would throw an exception. I learned something new today - thanks!

Brett

Thanks Brett.

C# strings require " not ’ - typo on the post :grinning:

You can use simplified string formatting with embedding text… put a dollar sign in front, and then the variables inside curly brackets… like this (and do it all in one line). There is no need to define the string and then assign on the next line:

string engBod = $"JobNum: {ttLaborDtl_xRow.JobNum}";

If you want multiple variables, you can do that also:

string morestuff = $"JobNum: {ttLaborDtl_xRow.JobNum}, PartNum: {ttLaborDtl_xRow.PartNum}";

For more information on String “Interpolation” vs String.Format vs String conversion see:

and

I really like the interpolation method of doing things because it takes the guesswork out, AND it makes for shorter lines of code.
I just realized that your original question may not have been answered… you actually dont need to “Convert” the JobNum field because it is already a string. One other solution is simply:

string EngBody = "JobNum: "+ttLaborDtl-xRow.JobNum;

That said, i would probably still use the interpolation way because I like it better (although according to dotnetperls, simple concatenation is slightly faster (83 milliseconds faster for 1,000,000 cycles)).

1 Like

Thanks Tim - good information.
Thanks for the reply.
:slight_smile: