Convert C# to VB.Net

September 2, 2007

It can be frustrating when a code sample that seems to do exactly what you want is written in C#, and your application is in VB.Net. http://www.kamalpatel.net/ConvertCSharp2VB.aspx solves this problem by converting the C# code directly into its VB.Net equivalent – all online and for free for code up to 6.15KB in size, and for larger amounts of code a tool can be downloaded for free.

It does the majority of the conversion well but I ran into a few minor problems where line breaks were not taken into account.

This tool will be even more useful when the ESRI samples, the majority of which are only available in VB6, are updated to .NET…

Advertisement

Lorem Ipsum

August 17, 2007

I recently added some Lorem Ipsum text to fill in some space on a prototype ASP.NET website I made recently, which lead to a few questions from the client (a person not a machine) such as “what language is all this text in?!” A handy generator and a history of Lorem Ipsum can be found here.

However far more amusingly is the generator at http://www.malevole.com/mv/misc/text/. There are some real classics in there…

Mutley

“Mutley, you snickering, floppy eared hound. When courage is needed, you’re never around. Those medals you wear on your moth-eaten chest should be there for bungling at which you are best. So, stop that pigeon, stop that pigeon, stop that pigeon, stop that pigeon, stop that pigeon, stop that pigeon, stop that pigeon. Howwww! Nab him, jab him, tab him, grab him, stop that pigeon now.”


Podcasts and Python

August 13, 2007

I’ve been listening recently to “.NET Rocks!” twice weekly .NET development podcasts. It is very easy to listen to, mainly due to the personalities of the presenters, and it gives a good overview of .NET develpment and related technologies. Software development as a whole is often discussed – test driven development, agile programming, management, spaghetti code…

A full archive of previous episodes is available for download (for free), and I found an epsiode that gives a good introduction to Python – it can be downloaded here. I’m still not convinced of the use of Python and ArcObjects as explaining how to run scripts with parameters to GIS users, or creating custom toolboxes, takes as much time as creating a nice UI in Visual Studio. In the show one of the presenters asks “but don’t you miss the Visual Studio environment and intellisense?”, which I couldn’t agree more with. However there is also mention of IronPython, which I knew had something to do with .NET, but it can also apparently be used within the Visual Studio environment which could make life a whole lot easier.

I’m off to download IronPython now…

Python


Another Visual Studio Macro

April 4, 2007

My start up program when working with ArcObjects is nearly always set to ArcMap.exe As my ArcMap installation had recently moved from an F: drive to a C: drive I had to update this for over a dozen projects. There had to be an easier way (!) so I spent some time trying to find out how to accomplish this with a Visual Studio macro. This can easily be altered to set the start up program for a set of projects in a solution, or to another .exe.

    1 Imports System

    2 Imports EnvDTE

    3 Imports VSLangProj

    4 Imports VSLangProj2

    5 Imports VSLangProj80

    6 

    7 Public Module ArcGISMacros

    8 

    9     Sub SetStartProgram()

   10 

   11         Dim proj As Project

   12         Dim config As Configuration

   13         Dim configProps As Properties

   14         Dim prop As [Property]

   15 

   16         For Each proj In DTE.Solution.Projects

   17             If TypeOf proj.Object Is VSLangProj.VSProject Then ‘loop through projects

   18                 config = proj.ConfigurationManager.ActiveConfiguration

   19                 configProps = config.Properties

   20                 prop = configProps.Item(“StartProgram”)

   21                 If prop.Value.ToString() Like “*ArcMap*” Then

   22                     prop.Value = “C:\Program Files\ArcGIS\Bin\ArcMap.exe”

   23                 End If

   24             End If

   25         Next

   26     End Sub

   27 End Module


Migrating .NET Projects from ArcGIS 9.1 to 9.2

April 4, 2007

My Windows XP recently decided to fall apart, so after a few days reinstalling everything I decided it would be a good time to switch from ArcGIS 9.1 to 9.2. The installation went smoothly enough, and after a few more hours I had Visual Studio 2005 up and running as well. With some trepidation I opened up my largest ArcGIS VB solution…1030 errors, let alone warnings! The solution had been developed for ArcGIS 9.1 so I had expected some issues..

After some investigation it became apparent the cause of most errors were:

1. None of the ESRI 9.1 DLLs that my projects referenced were present on my machine. These had all been updated to 9.2

2. The ESRI.ArcGIS.Utility library has been deprecated, and its existing functionality moved to the ESRI.ArcGIS.ADF library.

With regards to the first issue all the libraries still had the same name, but were different versions. Changing the “Specific Name” property of the reference allowed VS to find the library and removed the error. I must have several 100 of these references for several projects so I decided to try out the VS macros to automate this task. To edit and create macros manually go to Tools >> Macros > Macros IDE in Visual Studio (2005). All the subs in this post were created in the same module, and require the following references listed below. Some of these had to be added manually to the “MyMacros” project via the References in the Macros IDE Project Explorer. The VSLangProj80 and VSLangProj2 libraries are in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\

    1 Option Strict On

    2 Option Explicit On

    3 

    4 Imports EnvDTE

    5 Imports VSLangProj

    6 Imports VSLangProj2

    7 Imports VSLangProj80

The following procedure loops through every project in the solution, and then through every reference in the project. If the reference starts with the text ESRI then the “Specific Version” property is set to false.

    2 

    3         http://msdn2.microsoft.com/en-us/library/vslangproj80.reference3.specificversion(VS.80).aspx

    4 

    5         Dim projectItem As ProjectItem

    6         Dim myCodeProject As VSLangProj.VSProject

    7         Dim proj As Project

    8         Dim ref As Reference3

    9 

   10         For Each proj In DTE.Solution.Projects

   11             If  TypeOf  proj.Object Is VSLangProj.VSProject  Then ‘loop through projects

   12                 myCodeProject = CType(proj.Object, VSProject)

   13                 For Each ref In myCodeProject.References

   14                     If ref.Name Like “ESRI.*” Then

   15                         ref.SpecificVersion = False

   16                     End If

   17                 Next

   18             End If

   19         Next

   20 

   21     End Sub

This greatly reduced the number of errors, and was so satisfying I continued playing around with macros and the macro recorder available in VS. OK it may have taken longer than doing all this manually..but where is the fun in that..

The following macros work as follows:

UpdateReferences – runs all the sub macros, passing parameters where appropriate.

ReplaceReference – removes a reference tat is no longer needed, and automatically adds in the new reference. In this example I use it to replace all references (sic) to the deprecated ESRI.ArcGIS.Utility reference with the new ESRI.ArcGIS.ADF reference.

ReplaceNameSpaces – this opens the find / replace dialog with parameters already filled. I commented out the automated execution of this replacement as I sometimes received dialogs while replacing that cause the macro to crash. In this example I replaced all Import declarations with the new ESRI.ArcGIS.ADF reference.

   35     Sub UpdateReferences()

   36 

   37         ChangeSpecificVersions()

   38         ReplaceNameSpaces(“ESRI.ArcGIS.Utility”, “ESRI.ArcGIS.ADF”)

   39         ReplaceReference(“ESRI.ArcGIS.Utility”, “C:\Program Files\ArcGIS\DotNet\ESRI.ArcGIS.ADF.dll”)

   40 

   41     End Sub

   42     Sub ReplaceReference(ByVal strOrigRefName As String, ByVal strNewRefPath As String)

   43 

   44 

   45         Dim projectItem As ProjectItem

   46         Dim myCodeProject As VSLangProj.VSProject

   47         Dim proj As Project

   48         Dim ref As Reference3

   49 

   50         For Each proj In DTE.Solution.Projects

   51             If TypeOf proj.Object Is VSLangProj.VSProject Then ‘loop through projects

   52                 myCodeProject = CType(proj.Object, VSProject)

   53                 For Each ref In myCodeProject.References

   54                     If ref.Name = strOrigRefName Then

   55                         ref.Remove()

   56                         myCodeProject.References.Add(strNewRefPath)

   57                     End If

   58                 Next

   59             End If

   60         Next

   61 

   62     End Sub

   63 

   64     Sub ReplaceNameSpaces(ByVal strOldNameSpace As String, ByVal strNewNameSpace As String)

   65 

   66         DTE.ExecuteCommand(“Edit.Find”)

   67         DTE.ExecuteCommand(“Edit.SwitchtoReplaceInFiles”)

   68         DTE.Find.Target = vsFindTarget.vsFindTargetFiles

   69         DTE.Find.FindWhat = strOldNameSpace

   70         DTE.Find.ReplaceWith = strNewNameSpace

   71         DTE.Find.MatchCase = False

   72         DTE.Find.MatchWholeWord = True

   73         DTE.Find.MatchInHiddenText = False

   74         DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral

   75         DTE.Find.SearchPath = “Entire Solution”

   76         DTE.Find.SearchSubfolders = True

   77         DTE.Find.KeepModifiedDocumentsOpen = False

   78         DTE.Find.FilesOfType = “*.vb”

   79         DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1

   80         DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

   81         ‘If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then

   82         ‘Throw New System.Exception(“vsFindResultNotFound”)

   83         ‘End If

   84         ‘DTE.Windows.Item(“{CF2DDC32-8CAD-11D2-9302-005345000000}”).Close()

   85 

   86     End Sub


Some Notes on Web Standards and XHTML

March 19, 2007

I am starting to develop more and more online GIS and mapping applications, and to stop all the wiggly lines in Visual Studio I am trying to implement appropriate web standards. A good introductory article can be found here:  

http://msdn2.microsoft.com/en-us/library/aa479043.aspx

My summary…

 There are several versions of XHTML:

  • XHTML 1.0 Transitional – most similar to HTML
  • XHTML 1.0 Strict – most similar to XML
  • XHTML 1.0 Frameset – to allow the use of frames
  • XHTML 1.1 – allows additional elements in languages such as SVG
  • XHTML 2.0 – is apparently on the way
  • Depending on which language is used the page should contain the correct DOCTYPE e.g.

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”&gt;

    In ASP.NET 2.0 every control renders valid XHTML 1.0 transitional output by default (not strict XHTML..ASP 1.1 controls do not even meet the transitional criteria) . As code is used to generate XHTML then pages cannot be validated at design time. However a URL can be validated at the following site http://validator.w3.org/ – this checks the actual XHTML output seen by the browser.

    When using JavaScript in a web page, the script shold be enclosed in a CDATA (character data)  section. This stops the browser interpreting < or > as XML tags.  The CDATA section start and end should be enclosed in JavaScript comment tags to avoid errors:

    /* <![CDATA[ */

    {}
    /* ]]> */
     


    Microsoft Access Queries

    March 2, 2007

    With the arrival of MS Access as the “personal” (i.e. restricted…) geodatabase of choice, I along with many others have had to get familiar with using SQL and Access. The graphical query generator (I am currently using MS Access 2002) seems to like to make the statements as verbose as possible, even thoigh relatively simple SQL still runs. Anyway below are some of my often used queries.

    • To copy data from one table to another (where both fields in both tables match). This is useful when two identical datasets cover two different geographical areas but need to be combined into a larger datasets.

    INSERT INTO Table1( Field1, Field2)
    SELECT Field1, Field2
    FROM Table2;

    • To create a new field in a table using a SQL statement (useful if you have to update many tables).

    ALTER TABLE Table1 ADD COLUMN MyStringField STRING;

    • To combine the results of two queries with the same fields (the top 15 values from one subquery, and a specific record from another).

    SELECT TOP 15 Field1, Field2
    FROM Subquery1
    UNION
    SELECT Field1, Field2
    FROM Subquery2
    WHERE Field1 = 272;

    More to follow (they get harder to explain!)…