## Monday, September 30, 2013

### [For .NET beginners] How to save (serialize) an instance of an object

When I began to learn Visual Basic .NET (that is when I started programming, because it's the first language I studied), after I have understood the Object-Oriented paradigm, I asked mysellf the question: "How can I save an object to a file?", and, after some Google searches, I found that the answer is named Serialization. In this post I will try to make a basic explanation of the XML serialization, hoping it will be useful for people that are learning a .NET language (VB or C#) and is asking the same question.

### Why XML?

eXtensible Markup Language is not the only serialization format that can be used in .NET, but as can be read from discussions like this, it's the best, because XML files are human-readable, and files generated from your program can be read in other programs (and vice versa).

### Make your .NET object XML-serializable

First of all, edit your class to make it serializable; before starting, it will probably be something like this:
VB:
Public Class MyClass
Public field1 As String
Public field2 As Integer
Public field3 As OtherType

Sub doSomething()

End Sub
End Class

C#:
public class MyClass
{
public string field1;
public int field2;
public OtherType field3;

public void doSomething()
{

}
}

First of all, you have to include the necessary namespaces; add at the beginning of your file:
VB:
Imports System.Xml.Serialization
Imports System.IO
C#:
using System.Xml.Serialization;
using System.IO;
Then, declare that your class can be serialized by editing the class declaration:
VB:
<Serializable()> Public Class MyClass
C#:
[Serializable()] public class addonManifest
Now, the most important part: let's add into the class the methods to load and save the object from and to a file:
VB:
    Public Shared Function load(ByVal f As String) As MyClass
Dim xs As New XmlSerializer(GetType(MyClass))
Dim sr As New StreamReader(f)
Dim data As MyClass = CType(xs.Deserialize(sr), MyClass)
sr.Close()
Return data
End Function

Public Sub save(ByVal f As String)
Dim xs As New XmlSerializer(GetType(MyClass))
Dim sw As New StreamWriter(f)
xs.Serialize(sw, Me)
sw.Close()
End Sub

C#:
public static MyClass load(string f)
{
XmlSerializer xs = new XmlSerializer(typeof(MyClass));
MyClass data = (MyClass)xs.Deserialize(sr);
sr.Close();
return data;
}

public void save(string f)
{
XmlSerializer xs = new XmlSerializer(typeof(MyClass));
StreamWriter sw = new StreamWriter(f);
xs.Serialize(sw, this);
sw.Close();
}


### Call the save/load methods

Now instances of your class can be loaded and saved from and to XML files by simply calling the save and load methods we've just written, passing as a parameter the file's path:
Dim MyClassInstance As MyClass = MyClass.load("path\to\file.xml")
MyClass MyClassInstance = MyClass.load("path\\to\\file.xml");
Save (VB):
MyClassInstance.save("path")
Save (C#):
MyClass MyClassInstance = MyClass.load("path\\to\\file.xml");

### Conclusion

If you have some problems implementing this, leave a comment, I'll try to improve the article.
See you at the next post!

## Sunday, September 22, 2013

### How to use your Android device as a PHP/MySQL development environment with free apps

Actually smartphones and tablets are very versatile devices, and people use them to do lot of things that a little time ago were possible only on a PC: web browsing, social networking, gaming, etc. So, why not using them also for programming? Surfing the Google Play Store, I discovered that doing dynamic web development on an Android device is possible, also using only free apps (for dynamic web I intend PHP/MySql).
These are the apps I tried and found very useful for web development on Android.

### AndroPHP

This application is the only free Android dynamic webserver I found on the Play Store, but it isn't very popular on the Store. You can download it here. It has a very simple GUI, but it does what it promises: runs a full PHP/MySql server. It doesn't needs root privileges unless you want to use ports between 0 and 1024 (but the default port, 8080, is perfect for development). It also includes PhpMyAdmin to easily manipulate databases.
To use AndroPHP, just open the setting screen and set the server's root directory, then place there your website/webapp. Next, come back to the app's main screen and move the switch to on: now you will be able to acces your site by opening your browser and typing localhost:8080 in the address bar.
Here some screenshots:
 AndroPHP's main screen
 AndroPHP's setting screen

 Chrome showing the serve's phpinfo

### DroidEdit free

To edit the code of your web application, you obviously need an editor: I chose DroidEdit free (download): it's a very powerful editor that supports lots of programming, scripting and markup languages, including HTML, CSS, JS and PHP. There is also a paid version with additional features, like cloud support and Emmet/ZenCoding support (you can buy it here).
Here a screenshot:
 A PHP file opened in DroidEdit free
Note: I tested this app on my tablet, but I think that programming on a phone's screen wouldn't be very easy; in both cases, I suggest to use Hacker's keyboard, a developer-friendly input method.

## Monday, September 16, 2013

### Simple tip to manage the layout of a site in PHP

Update (02 dec 2013)

In this post I will describe the simple method I use to manage the layout of a website using PHP. Probably there are better solutions, but I prefer this way because I think it's simple and intuitive; obviously, any suggestion or criticism (possibly constructive) is well accepted :) .

### The layout file

First of all you need to create a php file in your site's root directory that will define the layout of the site. You can name it, for example layout.php. Once created it you can design in it the layout of your site, using this structure:

<!DOCTYPE html>
<html>
<!--Shared head: scripts, stylesheets,... -->
<link href="/style.css" rel="stylesheet" type="text/css">
<script src="/jquery.min.js"></script>
<!--End of the shared head -->
<body>
<!--The site's layout (example: title, menu and footer) -->
<h1>Tile</h1>
<!-- Put the following php line where you want to load the page's content -->
<?php getBody(); ?>
<div>Footer</div>
</body>
</html>


### The site's pages

Now we've created a PHP files that calls the two functions getHead() and getBody(), but where are they defined? Simple: they will be defined in each page file, because any page file will be structured as this:

<?php function getHead(){ ?>
<!-- Page's title and other head elements of this page -->
<title>My page</title>
<?php }
function getBody(){ ?>
<!-- Page's content -->
<p>Hello, world!</p>
<?php }
include("layout.php");
?>


As you can see, at the end of the file the script includes the layout page, so your page will be displayed in the layout that you previously created; if you put the pages in subdirectories of the web root, remember to adjust the reference to the layout file (for example, ../layout.php instead of layout.php).

## Monday, September 9, 2013

### How to connect over Wi-Fi an Android device to a Windows PC mounting it as a local drive

There are lots of methods to connect an Android device to a PC for the file transferring over wi-fi; there are, for example, applications that runs an HTTP server on the device, so it can be accessed from the PC's web browser, and other applications that works as an FTP server to allow the user to access the device's content from the computer using an FTP client. But there is a trick that allows you to mount the device in a Windows PC making it recognize as a local drive, assigning it a letter exactly as happens with USB pen-drives.
The idea is to use an FTP server running on the device, but on the computer, instead using an FTP client, using NetDrive, a software for Windows that mounts FTP server as local drives (I had already talked about it here). In this tutorial I will explain how to set up it.

### What you need

• An FTP server app on your Android Device. I use ES File Manager, that is a great file manager that includes an FTP server; if you prefer something more lightweight you can choose from the lots of the FTP servers available on the Google Play Store. These apps usually don't require root privileges (unless you want to use a port number littler than 1024, but this is not necessary).
• NetDrive installed on your PC. For personal/home use it's free and can be downloaded here; for commercial use, you need to buy a license.
• The two devices being in the same subnet (for example, connected to your home router; this isn't really necessary, but if the devices were in two differents networks you would have to expose your Android device on the public Internet with a port forwarding rule, and it would be a bit dangerous).

### Preparing the device

First of all, you need to set up the FTP server on the Android device. To do this, you have to open the app you installed and find the settings menu. In ES File Manager, you have to reach the server with a tap on the "Tools" voice in the left menu, then on "Remote access manager" and finally on the "Settings" button on the bottom of the screen. Here are some screenshots:
 The main screen of the ES File Manager's built-in FTP server
 The settings screen of the ES File Manager's built-in FTP server
These are the settings you need to care about:
• Port: the server's port. The default should be OK; if you get errors saying this port is already in use, try to change it. You can use any number between 1024 and 65535.
• Root directory: this is the top level directory you will be able to acces. The default value (usually "/sdcard") should make avaiable the whole device's internal storage accessible to the user; you can change it, for example, to access your external SD card, or, if you have root privileges, the protected archive.
• Manage account: the credentials you will use to access the device from the PC.If you wants to protect your devices form unauthorized accesses (but don't worry, it would be possible only from inside your subnet), change the default values (they usually are "admin"/"admin").
Once completed the setup, you can start the server.

### Preparing the PC

Now you have to set up NetDrive on your PC. When you start it, you will see this dialog:
To connect to your phone/tablet, click New site and configure the connection parameters in the right panel:
• Site name: a name you will use to identify the device (for example, "My phone" or "My tablet").
• Site IP or URL: the IP address of your device in your network. Usually it is indicated in the server app and is in the form 192.168.X.Y.
• Port: the port you have chosen in the configuration of the server.
• Server type: leave FTP (the other option is WebDAV, another protocol).
• Drive: the drive letter which will be assigned to the device. You can choose any letter not already in use.
• Account and password: the credentials you have chosen in the server configuration.
Now you can click Connect to mount the device as a local drive. Note: if at the first connection after its installation NetDrive reports an error, try to restart the computer (probably it's necessary because the virtual driver needs to be initialized at the startup of the system).
At the end, to disconnect, just click Disconnect in the NetDrive interface and stop the server on the device (usually it can be done with the same button used to start it).
If you have problems, probably I've explained badly the procedure, so leave a comment and I will try to improve this tutorial.
 My NetDrive configuration...
 ... and the drive mounted.

## Tuesday, September 3, 2013

### Description and current status of the UniversalIDE open source project

UniversalIDE is the open source project that I started about two years ago. It is an Integrated Development Environment written in VB.NET and based on an add-on architecture that is designed to make the environment compatible with any programming language and development platform. This is the home of the project, hosted on Sourceforge: http://universalide.sf.net/. As I said, I've been working on it since two years, however, it is still in beta; actually, I'm starting to develop the first stable version, the 1.0.
Actually, the available add-ons are two, that works in pair and are used for web developement: one manages the "web server" (yes, in quotes before it is a little program written by me and I don't think it can be considered a real web server XD), and the other is a HTML/CSS code highlighter; for more info, see this wiki article.
I'm planning, after the stable version and after having improved the web-development add-ons (especially with support for real web servers like Apache), to write an add-on for Java development; but this IDE, to become as most Universal as possible, needs more add-on developers, because without add-ons the UniversalIDE core is completely useless. If you are interested to collaborate, post in the forum.

## Sunday, September 1, 2013

### Mapping FTP and WebDav servers to Windows drive letters

Some days ago, surfing the net, I found a very particular FTP/WebDav Client for Windows: NetDrive. As you can see by the title of this post, this program mounts the servers as they were local drives.

 A screenshot of NetDrive
It is free for personal/home use; for commercial use, you have to buy a license. The program runs only on Windows, because it's designed to work with Windows drive letters (the other OSs, like OSX and Linux distributions, doesn't use drive letters but they use mount points) and can be downloaded here.
I found this software very useful because you can work on a remote server exactly as you were working on a local hard drive; however, it has some disadvantages: it's slower than normal FTP clients like FileZilla, and it doesn't support secure FTP connections (SFTP).

Tip: when I installed this program on my Windows 7 PC, it seemed not to work (it showed error messages each time I tried to mount a drive), but after a restart, it begun to work well. So, if you have problems after the installation, try to reboot your system.