Weather Underground Web API using C#,

[Origin Source: http://samueltoepke.com/?p=218]
In the off hours, I’ve been teaching myself C#…a simple enough object oriented language, with the benefit of using the .NET framework. If you’re targeting Windows, it’s incredibly easy…features like file IO and serial port communications work effortlessly.

I have a list of tasks that I complete before I refer to myself as ‘capable’ in a new language:

  • Helloworld
  • Web API Access
  • File IO
  • XML Parsing
  • GUI Creation
  • Serial Communication
  • System Call
  • Directory Management
  • Database Connectivity
  • 3rd Party Library Integration
  • Error Catching/Mitigation
  • Logging

I complete some of the above by using C# to access the Weather Underground’s API, and pull down current weather information for places I’ve lived. If you’re looking to use this code, you’ll have to go to the API website and request a key.

Helloworld
Using Visual Studio Express 2012 create a new console application. I added some lines to frame the start and end of the program.

0

Parse XML
I create a method called ‘parse’ that performs the following:

  1. Takes a Wunderground query URL (e.g. “http://api.wunderground.com/api/YOUR_KEY/conditions/q/VA/Springfield.xml”) as input
  2. Requests the data from Wunderground.
  3. Parses the XML file for pertinent weather data.
  4. Prints the data out to the console.

1

Output
Below is a screenshot of weather data for five different places I’ve lived…

results

Full Code
Full project file for Visual Studio can be found here: Wunderground_API_Test.

Parameters in C#

We’ve all seen them, those functions & methods that seem to go on forever:
public myMethod(int age, string name, string surname, int yearBorn, int weight, float xlocation, float ylocation ……)

Why is This Bad?
Well we’ve already seen one reason why coding like this is bad; readability.
what about optional parameters? You know the ones where you can add a default value.
public myMethod(int age = 20 ……)

How do You Solve This?

  1. public class myMethodParams
  2. {
  3. public int age { get; set;}
  4. public string name { get; set;}
  5. public string surname { get; set;}
  6. public int yearBorn { get; set;}
  7. public int weight { get; set;}
  8. public float xlocation { get; set;}
  9. public float ylocation { get; set;}
  10. }
  11. public myMethodParams()
  12. {
  13.     age = 20;
  14. }
  15. public void myFunction(myMethodParams theParams)
  16. {
  17. }

How to detect ScrollBar is visible on ScrollViewer

Most of time we use <ScrollViewer HorizontalScrollBarVisibility=”Auto”>. Since, it is Auto, we need to tap in some event and do some checking to know scroll bar is appeared.

For this you need to 

<ScrollViewer HorizontalScrollBarVisibility=”Auto” ScrollChanged=”ScrollViewer_ScrollChanged”>
<StackPanel x:Name=”Canvas1″ Width=”800″ Height=”400″>
<Button Click=”ButtonBigger_Click” Width=”100″ HorizontalAlignment=”Left”>Bigger</Button>
<Button Click=”ButtonSmaller_Click” Width=”100″ HorizontalAlignment=”Left”>Smaller</Button>
</StackPanel>
</ScrollViewer>

 

private void ButtonBigger_Click(object sender, RoutedEventArgs e)
{
Canvas1.Width = 888;
Canvas1.Height = 444;
}

private void ButtonSmaller_Click(object sender, RoutedEventArgs e)
{
Canvas1.Width = 333;
Canvas1.Height = 222;
}

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
bool scrollBarVisible = true;
if (e.ExtentHeight> e.ViewportHeight || e.ExtentWidth>e.ViewportWidth)
{
scrollBarVisible = true;
}
else
{
scrollBarVisible = false;
}
}

Basic How to Use the Ping Command

Start > Programs > Accessories > Command Prompt. This will give you a command window.

Example: ping www.google.com


PING www.google.akadns.net (216.239.41.104): 56 data bytes
64 bytes from 216.239.41.104: icmp_seq=0 ttl=248 time=18.952 ms
64 bytes from 216.239.41.104: icmp_seq=1 ttl=248 time=16.094 ms
64 bytes from 216.239.41.104: icmp_seq=2 ttl=248 time=15.321 ms
64 bytes from 216.239.41.104: icmp_seq=3 ttl=248 time=15.190 ms
64 bytes from 216.239.41.104: icmp_seq=4 ttl=248 time=28.476 ms
64 bytes from 216.239.41.104: icmp_seq=5 ttl=248 time=14.374 ms


With above simple command, few things you are achieved.

Usage1: Check www.google.com is alive or not

Usage 2: Check your DNS server is able resolve www.google.com into IP Address.

Usage 3: Check that you are on internet or not.

Usage 4: Check your network speed.

Convert Generics List to HashSet to utilize SetEquals and Except

Use Case

You have List<string> registered_name and List<Member> members. You want to know
a) are all members registered?
b) who are not registered?

Step1: Convert List<Member> into List<string> List

 
            List<string> memberName = (from n in Members
                                        select n.Name).ToList();

Step2: Check is all members registered?

 
            HashSet<string> registeredNameSet = new HashSet<string>(registered_name);
            HashSet<string> entireNameSet = new HashSet<string>(memberName);
            if (sequenceToCheckSet.SetEquals(runHistorySet))

Step3: Check who are not registered?

 
            var sequenceMissing = sequenceToCheckSet.Except(runHistorySet);