ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Mauricio Rojas Blog

December 2006 - Posts

  • PowerScript to VB.NET

    If you are migrating a Powerbuilder Application to .NET the more recommend language you can use is VB.NET. This because this language has a lot of common elements with PowerScript. Lets see some simple comparisons.

    Variable definition

    A variable is defined writing the Data Type just before the name.

    For example:

    Integer amount

    String name

     

    In VB.NET this will be

    Dim amount as Integer

    Dim name as String

     

    Constants

     

    constant integer MAX = 100

     

    In VB.NET this will be

    Const MAX As Integer = 100

     

    Control Flow: If Statement

     

          If monto_cuota=13 Then

                    nombre= 'Ramiro'

          ElseIf monto_cuota=15 Then

                 nombre= 'Roberto'

          Else

              nombre= 'Francisco'

          End If

    This code is almost exactly the same in VB.NET

     

            If monto_cuota = 13 Then

                nombre = "Ramiro"

            ElseIf monto_cuota = 15 Then

                nombre = "Roberto"

            Else

                nombre = "Francisco"

            End If

     

    Control Flow: Choose Statement

     

          Choose case monto_cuota

              Case Is< 13: nombre='Ramiro'

              Case 16 To 48:nombre= 'Roberto'

          Else

              nombre='Francisco'

          End Choose

    This code is slightly different:

      Dim monto_cuota As Integer

            Dim nombre As String

            Select Case monto_cuota

                Case Is < 13

                    nombre = "Ramiro"

                Case 16 To 48

                    nombre = "Roberto"

                Case Else

                    nombre = "Francisco"

            End Select

     

     

     

     

     

    Control Flow: For statement

     

          For n = 5 to 25 step 5

     

          a = a+10

     

          Next

     

    In VB.NET

     

    Dim n, a As Integer

          For n = 5 To 25 Step 5

                a = a + 10

          Next

     

     

    Control Flow: Do Until, Do While

     

    integer A = 1, B = 1

    //Make a beep until variable is greater than 15 variable

    DO UNTIL A > 15 

        Beep(A)

        A = (A + 1) * B

    LOOP

     

     

    integer A = 1, B = 1

    //Makes a beep while variable is less that 15

    DO WHILE A <= 15

    Beep(A)

    A = (A + 1) * B

    LOOP

     

    In VB.NET

     

            Dim A As Integer = 1, B As Integer = 1

            'Make a beep until variable is greater than 15 variable

            Do Until a > 15

                Beep(a)

                a = (a + 1) * B

            Loop

            'Makes a beep while variable is less that 15

            Do While A <= 15

                Beep(a)

                a = (a + 1) * B

            Loop

  • ASP to ASP.NET: File exists


    This is the migration of a snippet of ASP classic to ASP.Net

     

    Checking that a File Exists ASP Classic

    <%
    Dim strExists  
    Dim strNotExists 
    Dim objFileSystemObjectVar
    ``
    strExists    = "exists.txt"
    strNotExists = "error.txt"
     
    Set objFileSystemObjectVar = Server.CreateObject("Scripting.FileSystemObject")
     
    ' The FileExists method expects a fully qualified path and
    ' use Server.MapPath 
    %>
    <p>
    &quot;<%= strExists %>&quot; exists:
    <b><%=objFileSystemObjectVar.FileExists(Server.MapPath(strExists)) %></b>
    </p>
    <p>
    &quot;<%= strNotExists %>&quot; exists:
    <b><%= objFileSystemObjectVar.FileExists(Server.MapPath(strNotExists)) %></b>
    </p>

     

    Checking that a file exists ASP.NET

     

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.IO" %>
    <script language="VB" runat="server">
            Dim strExists As String = "exists.txt"
            Dim strNotExists As String = "error.txt"
    </script>
    <p>
    &quot;<%= strExists %>&quot; exists:
    <b><%=File.Exists(Server.MapPath(strExists))%></b>
    </p>
    <p>
    &quot;<%= strNotExists %>&quot; exists:
    <b><%= File.Exists(Server.MapPath(strNotExists))%></b>
    </p>
     

     

     

  • LINQ Project

    These project extends the VB and C# languages with query, set and transforms operations. It adds a native syntax for those operations.

     

    The idea of the LINQ project is to make data manipulation part of the language constructs. Lets see these VB examples for LINQ:

     

    The following examples associates the Customer class to the Customer table. Just adding the Column Tag before a field, maps it to a table column.

     

        <Table(Name:="Customers")> _

        Public Class Customer

            <Column(Id:=True)> _

            Public CustomerID As String

           

            <Column()> _

            Public City As String

           

        End Class

    To access the database you do something like:

    ' DataContext takes a connection string

    Dim db As DataContext = _
            New DataContext("c:\...\northwnd.mdf")

          'Get a typed table to run queries

          Dim Customers As Table(Of Customer) = db.GetTable(Of Customer)()

          'Query for customers from London

            Dim q = _

              From c In Customers _

              Where c.City = "London" _

              Select c

     

          For Each cust In q

              Console.WriteLine("id=" & Customer.CustomerID & _

                  ", City=" & Customer.City)

          Next

     

    You just create a DataContext and create typed object that will relate dot the relational tables. I think this is awesome!!

     

    It is even nicer if you create a strongly typed DataContext

     

        Partial Public Class Northwind

            Inherits DataContext

            Public Customers As Table(Of Customer)

            Public Orders as Table(Of Order)

            Public Sub New(connection As String)

                MyBase.New(connection)

     

    Your code gets cleaner like the following:

        Dim db As New Northwind("c:\...\northwnd.mdf")

     

        Dim q = _

            From c In db.Customers _

           Where c.City = "London" _

            Select c

         

          For Each cust In q

              Console.WriteLine("id=" & Customer.CustomerID & _

                  ", City=" & Customer.City)

          Next

     

     

    These project will start a lot of exciting posibilities. I recommed you have a look at’: http://msdn.microsoft.com/data/ref/linq/

     

     

     

  • Thin Clients or Rich Clients

    This is an old topic but I always like to give some thoughts on this idea.  I really think that in the future everything will run from the internet. Internet is becoming another basic need just as electricity, water, gas and telephone. The appearance of new technologies makes it easy to have Internet even in remote places like beaches and mountains.

    Rich Clients have been defended because it was said that not a lot of interactivity could be produced by thin clients. It has also been said that powerful interfaces (with complex gadgets, etc) could be produced in a web interface. I think that Flash, and AJAX have shown that despite all believes it is possible. There are still more technologies that will come. But the easier deployment and the fact that you can use your web applications everywhere even from a cell phone in a taxi cub.

     

    I also love phases like “If you ask an engineer the time, he'll tell you how to build a clock.”. Web interfaces are simple and easy to learn look at blogs like Jon Galloway http://weblogs.asp.net/jgalloway/archive/2003/09/27/29446.aspx and Microsoft's Inductive User Interface (IUI) initiative it seams like more people is starting to think in this way.

  • ASP to ASP.NET

    Migrating ASP to ASP.NET

     

    Surpinsingly for me. I found that some friends were working on migrating an ASP classic site to ASP.NET. I was impressed to see that there are still sites in ASP classic at ALL!!!!

    ASP.NET 2.0 provides so much improvements, you cannot even debug in ASP. ASP.NET 2.0 has better performance and easier to deploy. There is even Intellisense! These days is hard for me not assuming that all IDEs provide the developer aids like that.

     

    Also migrating simple ASP classic code to ASP.NET is not that hard.

    Let’s see a simple ASP classic page like:

     

    <%

    Dim objConn

    Dim objRS

    Set objConn = Server.CreateObject(“ADODB.Connection”)

    Set objRS = Server.CreateObject(“ADODB.Recordset”)

    objConn.ConnectionString = “DRIVER=(SQL Server);server=…”

    objConn.Open()

    objRS.Open “SELECT * from Items”, objConn

    Do While Not objRS.EOF

         Response.Write CStr(objRS(“ID”)) + “ – “ + objRS(“Description”) + “<br>”

         objRS.MoveNext

    Loop

    objConn.Close

    %>

     

    Migrates easily to:

     

    <%@ Page aspcompat=true Language=”VB” AutoEventWireUp=”false” CodeFile=”Default.aspx.vb” >

    <%

    Dim objConn

    Dim objRS

    Set objConn = Server.CreateObject(“ADODB.Connection”)

    Set objRS = Server.CreateObject(“ADODB.Recordset”)

    objConn.ConnectionString = “DRIVER=(SQL Server);server=…”

    objConn.Open()

    objRS.Open (“SELECT * from Items”, objConn)

    Do While Not objRS.EOF

         Response.Write (CStr(objRS(“ID”).Value) + “ – “ + objRS(“Description”).Value + “<br>”)

         objRS.MoveNext

    Loop

    objConn.Close

     

    %>

     

    These are the task to do:

    1. Remove SET
    2. Any method in ASP.NET requires its parameters to go inside parenthesis
    3. ASP.NET does not have default properties so elements as objRS(“ID”) must be changed to objRS(“ID”).Value and objRS(“Description”) to objRS(“Description”).Value
    4. you must add the aspcompat=true property to the page because of the apartment threading issues
    5. You should change statements like Dim objRS to Dim objRS as Object it is not an error but it will help you make your code more clear.

     

    You can also download the Migration Assitant from ASP to ASP.NET from:

    http://www.asp.net/DownloadWizard.aspx?WizardTarget=AspToAspNetMigrationAssistant

     

  • Starting with the internationalization bla bla (Part Two)

    Ok enough theory. To start using the internationalization stuff lets start with a simple Form.

    Open  the Visual Studio IDE. And Start a Windows Forms project. And then create a simple Form. Add some labels and buttons and set their captions. Once you do that and finish the initial creation of the form, go to the properties pane and change the Localizable property to true and assign the desired value in the Language property. The Visual Studio designer will save the changes in additional resource files whose names will look like <FormName>.<culture>.resx

    Once you finish the texts, sizes, positions for the first culture and save it. The IDE creates the resource file for that culture. If you want to create a resource file for another language just change the Form property and assign the text for this new language.

     

    You can not only assign personalized translations for each region but also the position and size of components. This is useful because in some languages the buttons might need to be bigger because the labels could be bigger.

     

    All this work is supported by the .NET resource managers. System.Resources.ResourceManager class.

     

    I recommend you also using String Resource Tools like the ones at: http://www.codeplex.com/ResourceRefactoring

     

    These tools makes it even easier the task of moving all your strings to resource files:

     

  • Taking an application to the whole world (Series 1 of 3)

    Recently I was asked by some fellows some help to make a new version of their VB6 application in Spanish, but at the end we end up migrating the application to VB.Net and taking advantage of the .NET internationalization features.

     

    VB6 did not provided and out-of-box support for multiple cultures, but the .NET framework provides the developer with utilities to create applications that allow users in multiple regions use their applications according to their “Culture”.

     

    The .Net Framework is able to handle different cultures. These “cultures” are used to localize certain aspects of the application for particular geographic zones.

    When an application is not created with any cultural considerations it is said to use a Neutral Culture. It implies that independent of the machine configuration it will behave and display components in the same way.

     

    The Culture is assigned automactically using the machine settings or it can be altered programmatically. You can use the property System.Globalization.CultureInfo.CurrentUICulture for that purpose.

     

    Cultures have two elements: language and region. For example for Argentina where Spanish is spoken la culture will be es-AR (es is for Spanish: ESpañol and AR for Argentina)

     

    If no information is found at all for an language then the neutral culture is used.

     

    The information for user display is handler in assemblies usually called “satellite assemblies” which are loaded depending on the culture of the environment where the application is executed.

     

  • Is .NET hotter that Java

    This is a very controversial topic. Recently I have seen several blogs that state that the VB6 Programmers are moving to other platforms like PHP or Java instead of VB.NET
    For example see:
    Number of VB Developerts Declining in US
    By Steve Graegert
    “They’re also leaving VB.NET; which is down by 26%. This means Java now holds the market penetration lead at 45% (with developers using Java during some portion of their time), followed by C/C++ at 40%, and C# at 32%.”

    I also remember an Article I read in 2005 in JDJ (Java Developers Journal) that expressed that C# was a language created similar to C++ to aid the C++ programmers move to the .NET Framework, argument that I do not share.

    I have no evidence but I do not feel that it is that way. I'm am a Java Developer too. And both platforms have their merits. C# is a nice language, very similar to Java and C++ no doubt but it deserves its own place. Visual Studio has still a path to follow. But VS2005 provides some useful refactorings and the incredibly awaited feature that allows you to edit the code that you're running :)

    Maybe the 1.0 and 1.1 frameworks were not enterprise ready, but 2.0 and 3.0 frameworks are an excellent improvent.

    Java as well had to go thru a lot of JDK releases. They have just released the 1.6 and what about the J2EE releases, the Java Enterprise Beans fiasco. You can confirm that by looking at the rise of POJO (Plain Old Java Object) frameworks like spring.

    In general everything requires time to grow. I think that Java has been more time in the market and it has finally provided mechanisms that allow the development of enterprise services "easier" and it is giving it momentum.

    .NET components industry is common, there are lots of components and somethings are easier. So I'll wait some time, maybe a couple of year to really find out which is the hotter platform.
  • Invoke a WebService that needs Windows Authentication


    Passing client credentials to a Web service using Windows authentication

    Sometimes a Web Service is under a configuration that requires Windows Authentication. It is not a problem for .NET, all you need to do is set your environment to send the client credentials.
    1. First Create a Reference to a Web Service:





    To do that just go to the references of your project and add a Web Reference. Type the URL of your web service. This will find your Web Service Reference and you can update it. This will generate the proxy code you need to access your webservice.

    If you try to call your webservice with a call like:

    WebReference.MyService pService = new WebReference.MyService ();
    pService.doStuff("A", "B");

    You’ll get a HTTP 404 forbidden access exception.

    Now to send the user and password to call your service write some code like the following:

    WebReference.MyService pService = new WebReference.MyService ();
    pService.Credentials = new System.Net.NetworkCredential("user", "password");
    pService.doStuff("A", "B");

    You can also send the domain name as part of the parameter to the NetworkCredential class:

    pService.Credentials = new System.Net.NetworkCredential("user", "password","domain");

    It will be even better to have your user and password not in your code but in the configuration file for your program. You can then use the AppSettings class to access those properties.

  • ToolTip with Images

    The Tooltips that comes out of the box for winforms do not support images on them. This is a simple class that allows you to add Images and Text to your ToolTips

    using System;

    using System.Windows.Forms;

    using System.Collections.Generic;

    using System.Text;

    using System.Drawing;

    namespace CustomControls

    {

    class ToolTipWithImage : Control

    {

    private Image _img;

    private Control _ctl;

    private Timer _timer;

    string _imgfilename;

    string _tiptext;

    public String TipText

    {

    get { return _tiptext; }

    set { _tiptext = value; }

    }

    public String ImageFile

    {

    get

    {

    return _imgfilename;

    }

    set

    {

    if (_imgfilename == value)

    {

    }

    else

    {

    _imgfilename = value;

    try

    {

    _img = Image.FromFile(_imgfilename);

    this.Size = new Size(_img.Width + 70, _img.Height);

    }

    catch

    {

    _img = null;

    }

    }

    }

    }

    public ToolTipWithImage()

    {

    this.Location = new Point(0, 0);

    this.Visible = false;

    _timer = new Timer();

    _timer.Interval = 1000;

    _timer.Tick += new EventHandler(ShowTipOff);

    }

    public void SetToolTip(Control ctl)

    {

    _ctl = ctl;

    ctl.Parent.Controls.Add(this);

    ctl.Parent.Controls.SetChildIndex(this, 0);

    ctl.MouseMove += new MouseEventHandler(ShowTipOn);

    }

    protected override void OnPaint(PaintEventArgs e)

    {

    if (_img != null)

    {

    e.Graphics.DrawImage(_img, 0, 0);

    e.Graphics.DrawString(TipText, this.Font, Brushes.Black, _img.Width, 0);

    }

    }

    public void ShowTipOn(object sender, MouseEventArgs e)

    {

    if (!this.Visible)

    {

    _timer.Start();

    this.Left = _ctl.Left + e.X + 100;

    this.Top = _ctl.Top + e.Y;

    this.Visible = true;

    }

    }

    public void ShowTipOff(Object sender, EventArgs e)

    {

    _timer.Stop();

    this.Visible = false;

    }

    }

     

    }

     

    To use it just do something like

    ToolTipWithImage timg = new ToolTipWithImage();

    timg.TipText = "Hello";

    timg.ImageFile = @"C:\Hello.gif";

    timg.SetToolTip(btnOk);

Powered by Community Server (Non-Commercial Edition), by Telligent Systems