How to ignore if var is null C #

I join the loading of strings to create a super string, but I need to ignore the parameter if it is zero. Currently, I cannot think of how to do this, except that they cover all the parameters in separate if statements. Help pls:

Here is the code

public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype, string includeresults, string includemap, string username, string password) { sharedkey = "&" + sharedkey; service = "&" + service; period = "&" + period; bulletintype = "&" + bulletintype; includeresults = "&" + includeresults; includemap = "&" + includemap; username= "&" + username; password = "&" + password; string completeLink = sharedkey + service + period + bulletintype + includeresults + includemap + username + password; 

I don’t know how to solve this.

+6
source share
5 answers

I would really reorganize it like this:

 public void LinkBuilder(params string[] links) { string completeLink = String.Join("&", links.Where(x=>!String.IsNullOrEmpty(x))); } 
+22
source

You can perform string checking with the ?: in operator.

 public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype, string includeresults, string includemap, string username, string password) { sharedkey = checkValue(sharedkey); service = checkValue(service ); period = checkValue(period ); bulletintype = checkValue(bulletintype ); includeresults = checkValue(includeresults ); includemap = checkValue(includemap ); username= checkValue(username ); password = checkValue(password ); string completeLink = sharedkey + service + period + bulletintype + includeresults + includemap + username + password; } private String checkValue(String str) { return str != null ? "&" + str : ""; } 
+1
source

Make an enumerable collection of your strings, use the linq bit to filter out the zeros, then reconnect everything together using String.Join :

 var elements = new[]{baselink, sharedkey, service, period, bulletintype, includeresults, includemap, username, password}; var nonNullElements = elements.Where(e => e != null); var outputString = String.Join("&", nonNullElements); 

In the event that you are actually trying to assemble a request, there are better ways.

For example, you can use HttpUtility and reflection using the following method to parse an anonymous object into a query string:

 public static class ObjEx { public static string ToQueryString(this object data) { var collection = data.GetType() .GetProperties() .Aggregate( HttpUtility.ParseQueryString(string.Empty), (prev,curr) => { var val = curr.GetValue(data); var propName = curr.Name; prev.Add(propName,val.ToString()); return prev; }); return collection.ToString(); } } 

then

 var data = new{foo = "bar", num = 1, cat = "bad", dog = "good", needsEscaping = "Γ©\"&"}; Console.WriteLine(data.ToQueryString()); 

will provide you with:

  foo = bar & num = 1 & cat = bad & dog = good & needsEscaping =% u00e9% 22% 26 
+1
source

If the goal is to avoid wrapping each parameter in an if statement, you can add them to the list, then use String.Join and Linq.Select

  public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype, string includeresults, string includemap, string username, string password) { var allParams = new List<string> { baselink, sharedkey, service, period, bulletintype, includeresults, includemap, username, password }; var completeLink = "?" + String.Join("&", allParams.Select(p => p != null)); } 
+1
source

Below will be adopted Collection , which, in my opinion, may be better supported. I redid a little.

 public string LinkBuilder(Dictionary<string, string> parameters) { var url = String.Empty; foreach(var parameter in parameters) if(!string.IsNullOrEmpty(parameter.Value)) url += String.Format("&{0}={1}", parameter.Key, parameter.Value); return url; } 

This way you will pass the collection to create your url, then a giant url will return for you. You have a massive choice, I personally like Maxim.

+1
source

All Articles