DAVIDEVANS.COM
 PHOTO GALLERIES FOR THE EVANS FAMILY ON THE WEB SINCE 1995
Sunday 05
September 2010
Sign In
Blog .NET Software Development Blog Entries
Thursday 05 August 2010   
.NET Software Development
Team Foundation Server (TFS)
Attention all Visual Studio developers!

I must be a creature of habit.  I have spent the last 5+ years living every work day inside Visual Source Safe.  Although I have upgraded every version of Visual Studio at the first possible opportunity I have avoided changing my source control behaviour.  Simply because the one time I tried Ankh/Tortoise and that open source garbage I came unglued.

Well thank you Microsoft.  Finally I have upgraded to TFS - Team Foundation Server.  I cannot explain it - but everything is fast, smooth, and I am flying through edits like never before.  VS 2010 is responsive and all my previous gripes about it have vanished. 

I never realized that VSS was the source (forgive the pun) of so many problems.

It's a whole new day with TFS.  If anyone reads this and cares... go for it. And if you come unglued with TFS, email me and I'll try and help.  I believe in it that much.

Cheers, David

Wednesday 17 February 2010   
.NET Software Development
Using Reflection to DeDupe object arrays

Hi,

Here's a handy bit of code that I developed, with some help from my friend Bill O'Toole, to dedupe various object arrays using Reflection.

Cheers, D

        public class DupeChecks

        {

                public class DupeChecksStrings

                {

                        private List<string> _dupeChecks;

                        private bool _ignoreCase;

                        public bool IgnoreCase

                        {

                                get { return _ignoreCase; }

                                set { _ignoreCase = value; }

                        }

                        public DupeChecksStrings()

                        {

                                _dupeChecks = new List<string>();

                                _ignoreCase = true;

                        }

                        public bool Has(string str)

                        {

                                bool rv = false;

                                foreach (string _dupeCheck in _dupeChecks)

                                {

                                        if (_ignoreCase)

                                        {

                                                if (_dupeCheck.ToLower() == str.ToLower())

                                                {

                                                        rv = true;

                                                }

                                        }

                                        else

                                        {

                                                if (_dupeCheck == str)

                                                {

                                                        rv = true;

                                                }

                                        }

                                }

 

                                if (!rv)

                                {

                                        _dupeChecks.Add(str);

                                }

 

                                return rv;

                        }

                }

                public class DupeChecksInts

                {

                        private List<int> _dupeChecks;

                        public DupeChecksInts()

                        {

                                _dupeChecks = new List<int>();

                        }

                        public bool Has(int i)

                        {

                                bool rv = false;

                                foreach (int _dupeCheck in _dupeChecks)

                                {

                                        if (_dupeCheck == i)

                                        {

                                                rv = true;

                                        }

                                }

 

                                if (!rv)

                                {

                                        _dupeChecks.Add(i);

                                }

 

                                return rv;

                        }

                }

                public class ByObject

                {

                        private List<object> store;

                        public ByObject()

                        {

                                store = new List<object>();

                        }

                        public List<object> DeDupe(List<object> source, string propertyName)

                        {

                                if (source.Count > 0)

                                {

                                        Type type = source[0].GetType();

                                        PropertyInfo[] sourceProperties = type.GetProperties();

                                        bool foundProperty = false;

                                        foreach (PropertyInfo sourceProperty in sourceProperties)

                                        {

                                                if (sourceProperty.Name == propertyName)

                                                {

                                                        foundProperty = true;

                                                }

                                        }

                                        if (foundProperty)

                                        {

                                                foreach (object obj in source)

                                                {

                                                        if (!StoreHas(obj, propertyName))

                                                        {

                                                                store.Add(obj);

                                                        }

                                                }

                                        }

                                }

                                return store;

                        }

                        private bool StoreHas(object item, string propertyName)

                        {

                                bool rv = false;

 

                                Type itemType = item.GetType();

                                PropertyInfo[] itemPIs = itemType.GetProperties();

 

                                foreach (object obj in store)

                                {

                                        Type storeType = obj.GetType();

                                        foreach (PropertyInfo storePI in storeType.GetProperties())

                                        {

                                                if (storePI.Name == propertyName)

                                                {

                                                        foreach (PropertyInfo itemPI in itemPIs)

                                                        {

                                                                if (itemPI.Name == propertyName)

                                                                {

                                                                        if (itemPI.GetValue(item, null).ToString() == storePI.GetValue(obj, null).ToString())

                                                                        {

                                                                                rv = true;

                                                                        }

                                                                }

                                                        }

                                                }

                                        }

                                }

 

                                return rv;

                        }

                }

        }


Sunday 31 January 2010   
.NET Software Development
C# .NET Google Maps Latitude Longitude


Hi,

This is a handy little bit of C# that will help you convert an address into a Latitude and Longitude using hte Google Maps API.

Cheers, D     

public class Address

      {

            public static string googleMapsAPIKey = "";

            public string Street = "";

            public string City = "";

            public string State = "";

            public string Zip = "";

            public string Country = "";

            public float Latitude = 0.0F;

            public float Longitude = 0.0F;

            public string Status = "";

            public int StatusCode = 0;

 

            public Address()

            {

                  googleMapsAPIKey = ConfigurationManager.AppSettings["MyGoogleMapsAPIKey"];

                  Street = "";

                  City = "";

                  State = "";

                  Zip = "";

                  Country = "";

                  Latitude = 0.0F;

                  Longitude = 0.0F;

                  Status = "";

                  StatusCode = 0;

            }

 

            override public string ToString()

            {

                  string rv = Street + " " + City + " " + State + " " + Zip + " " + Country;

 

                  return rv;

            }

            public static Address EncodeLatLong(string location)

            {

                  googleMapsAPIKey = ConfigurationManager.AppSettings["MyGoogleMapsAPIKey "];

                  Address oAddress = new Address();

 

                  string urlStart = "http://maps.google.com/maps/geo?q=";

                  string urlMiddle = Uri.EscapeDataString(location);

                  string urlEnd = "&output=csv&key=" + googleMapsAPIKey;

                  string url = urlStart + urlMiddle + urlEnd;

 

                  try

                  {

                        HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);

                        HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

                        Stream stream = HttpWResp.GetResponseStream();

                        StreamReader sr = new StreamReader(stream, Encoding.UTF8);

                        string response = sr.ReadToEnd();

                        string[] pieces = response.Split(',');

 

                        if (pieces.Length == 4)

                        {

                              try { oAddress.StatusCode = int.Parse(pieces[0]); }

                              catch { }

                              try { oAddress.Latitude = float.Parse(pieces[2]); }

                              catch { }

                              try { oAddress.Longitude = float.Parse(pieces[3]); }

                              catch { }

                        }

                  }

                  catch { }

 

                  return oAddress;

            }

      }


Friday 15 January 2010   
.NET Software Development
Image Resize in C# .NET
Here's a handy way to resize an image - perhaps one uploaded through a web site. It's good for making thumbnails as well. Make sure you use Server.MapPath to get the fully resolved fullFileNames. Make sure your web site has permission to read/write to the path.

protected void ResizeImageOverwrite(string fullFileNameSource, 
    string fullFileNameDest, int newHeightMax, int newWidthMax)

      {

            try

            {

                  FileStream fs = new FileStream(fullFileNameSource, 
                    FileMode
.Open, FileAccess.Read, FileShare.Read);

                  System.Drawing.Image 
                    newImage = System.Drawing.Image.FromStream(fs);

                  int fileWidth = newImage.Width;

                  int fileHeight = newImage.Height;

                  fs.Close();

                  fs = null;

 

                  float ratio = 1.0F;

                  try

                  {

                        ratio = (float)fileWidth / (float)fileHeight;

 

                        int newHeight = newHeightMax;

                        int newWidth = 
                            Convert.ToInt32((double)newHeightMax * ratio);

                        if (newWidth > newWidthMax)

                        {

                              newWidth = newWidthMax;

                              newHeight = 
                                   Convert.ToInt32((double)newWidthMax / ratio);

                        }

 

                        Bitmap bitmap = 
                            new Bitmap(newImage, newWidth, newHeight);

                        bitmap.Save(fullFileNameDest);

                  }

                  catch { }

            }

            catch { }

      }