&, | , &&, || what…?

A friend of mine was wondering the advantage over the various operators.  I used the wording short circuits and thus does not eval the portion of the op to the right.

I sent her to this documentation site

&& and || Operators

  • To avoid exceptions and increase performance by skipping unnecessary comparisons, use && instead of & and || instead of | when you perform comparisons, as shown in the following example.
                Console.Write("Enter a dividend: ");
                var dividend = Convert.ToInt32(Console.ReadLine());
    
                Console.Write("Enter a divisor: ");
                var divisor = Convert.ToInt32(Console.ReadLine());
    
                // If the divisor is 0, the second clause in the following condition
                // causes a run-time error. The && operator short circuits when the
                // first expression is false. That is, it does not evaluate the
                // second expression. The & operator evaluates both, and causes 
                // a run-time error when divisor is 0.
                if ((divisor != 0) && (dividend / divisor > 0))
                {
                    Console.WriteLine("Quotient: {0}", dividend / divisor);
                }
                else
                {
                    Console.WriteLine("Attempted division by 0 ends up here.");
                }
    

MVC-API with Windows CE Embedded 6

WinCEMVC

Previously I posted about using a Web-API to import data from another vendor’s products into our database. One of my goals for our team has been to move away from using direct calls into our database be it by either .NET ADO, Linq or SQL commands and instead expose a means for all products to get their data needs via a service. When we built a mobile application; which is a Single Page Application, I rolled out a MVC-API using Microsoft’s framework for this. Our first big pay off on adaptability and cross platform use proved out when we needed to support a RFID Device made by Motorola. This device uses a Windows CE Embedded operating system. Below I will quickly cover how I coded to connect to the API from this device.

First things first…

I had tried to store the API url’s on the device in a XML file, but watch out because xml does not like the & symbol in any value. You can use things like & or %26 inline, but Windows CE in-application-browser does not seem to like this very much… My work around was to store the values on the device’s SQL Lite instance.

Now moving on, as you know an API will have an end point you send your query to.
For example:

https://www.mysite.org/api/schedule/{0}

The above api, requires a school id so you can pull the correct schedule for that school/site.

In my example, I will show how I pulled a set of inventory tags down from the API site.

I have a basic method called GetTags

The api is called, several helper methods help pull the data and parse it and eventually inflate a List with POCO objects called TagItem which is a simple class with Getters and Setters.

public static List<TagItem> GetTags(string ApiURL,int SiteUId)

{

string uri = String.Format(“{0}/api/Inventory/GetTags?siteUId={1}”, ApiURL, SiteUId);

return JsonConvert.DeserializeObject<List<TagItem>>(GET(uri));

}

As you can see we pass in the GENERIC API URL and pre-pend it to the API Signature.

The real meat here is the GET method which in turn uses the GetBaseRequest and other helper methods. I will post the code below since it should be fairly obvious what it is doing…

private static string GET(string pUri, int pTimeOut)

{

HttpWebRequest request = null;

string retValue = “”;

try

{

request = GetBaseRequest(pUri, “GET”, “application/json; charset=utf-8”, pTimeOut);

retValue = ReadRequestResponse(request);

}

catch (Exception)

{

throw;

}

return retValue;

}

private static HttpWebRequest GetBaseRequest(string pUri, string pMethod, string pContentType, int pTimeout)

{

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pUri);

request.Headers.Add(“Authorization”, Token);

request.Method = pMethod;

request.Proxy = null;

request.KeepAlive = false;

request.Timeout = pTimeout;

request.ContentType = pContentType;

request.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”;

return request;

}

private static string ReadRequestResponse(HttpWebRequest pRequest)

{

string retval;

HttpWebResponse response = (HttpWebResponse)pRequest.GetResponse();

using (Stream responseStream2 = response.GetResponseStream())

using (StreamReader reader = new StreamReader(responseStream2))

retval = reader.ReadToEnd();

response.Close();

return retval;

}

That is the meat of it, hope that helps someone else. If you have questions please let me know.