Hi
In below code i want to pass value dynamic instead of hard coded value - 321483788811
C#
var body = @"{" + "\n" +
@" ""awbs"": [" + "\n" +
@" ""321483788811""," + "\n" +
@" ""NMBC0001789312""" + "\n" +
@" ]" + "\n" +
@"}";Thanks

Amit MohantyRank #112 · 20K reputationPosted Jan 11, 2023, 4:44 AM EST
Try this
private static string GetBody(string valueOne, string valueTwo) { var body = @"{" + "\n" + @" ""awbs"": [" + "\n" + @" ""{val1}""," + "\n" + @" ""{val2}""" + "\n" + @" ]" + "\n" + @"}"; var postBody = new StringBuilder(body); postBody.Replace("{val1}", valueOne); postBody.Replace("{val2}", valueTwo); return postBody.ToString(); }How to call
string body = GetBody("321483788811", "NMBC0001789312");Uday DodiyaRank #239 · 10.3K reputationPosted Jan 11, 2023, 5:30 AM EST
Try Following
string val1 = "321483788811"; string val2 = "NMBC0001789312"; var body = $@"{{ ""awbs"": [ ""{val1}"", ""{val2}"" ] }}";Or Create function for body like following
private static string GetBodyString(string val1, string val2) { var body = $@"{{ ""awbs"": [ ""{val1}"", ""{val2}"" ] }}"; return body; } //Call that function. Here, I have called that function two times to show you how you can set the dynamic value in the body. string body = GetBodyString("321483788811", "NMBC0001789312"); string body1 = GetBodyString("521583788811", "NKXC0001789312");Jignesh KumarRank #91 · 22.9K reputationPosted Jan 11, 2023, 11:21 PM EST
Hi Ramco,
You can use string interploation as $ and do as below to pass dynamic parameter,
var param = "321483788811"; var param1 = "NMBC0001789312"; var body = $@"{{ ""awbs"": [ ""{ param} "",""{ param1 }""]}}";Munib ButtRank #156 · 14.9K reputationPosted Jan 12, 2023, 3:37 PM EST
Simply use the $"string" with placeholders option as below:
var value1 = "Test";
var val = $"This is a test value = {value1}";
Console.WriteLine(val);
Using C#11