Archive for the ‘Developement’ category

Double.Parse and Decimal.Parse with pointing symbols

July 5th, 2011

One problem I encounter on dotnet equally in Visual Basic or C#, is when I have to parse a decimal or a double from a string. When parsing a double or a decimal, we find a mistake because in math, punctuation symbol for the decimal point, so to make a Double.Parse(number) or Decimal.Parse(number), which makes is to ignore the point and therefore take the decimal part as if it were the whole. For this does not happen, we must indicate that the number system used is the mathematician, who does not vary with culture. An example.

string number = "15.3";
double doNumber;
decimal deNumber;

doNumber = double.Parse(number);
deNumber = decimal.Parse(number);

/*
Values:
doNumber = 153
deNumber = 153
*/

doNumber = double.Parse(number, CultureInfo.InvariantCulture);
deNumber = decimal.Parse(number, CultureInfo.InvariantCulture);

/*
Values:
doNumber = 15.3
deNumber = 15.3
*/
Dim number As String = "15.3";
Dim doNumber As Double;
Dim deNumber As Decimal;

doNumber = double.Parse(number);
deNumber = decimal.Parse(number);

'Values:
'doNumber = 153
'deNumber = 153

doNumber = double.Parse(number, CultureInfo.InvariantCulture);
deNumber = decimal.Parse(number, CultureInfo.InvariantCulture);

'Values:
'doNumber = 15.3
'deNumber = 15.3

CultureInfo is a class that provides information on cultures and in particular the property InvariantCulture or CultureInfo.InvariantCulture, is the provider of culture information that does not vary by localization, see the mathematical punctuation symbols. The CultureInfo class is in the System.Globalization namespace, leaving the path as: System.Globalization.CultureInfo.InvariantCulture.

Via: Double.Parse y Decimal.Parse con símbolos de putuación

Released version 0.2 of puntoengine

October 3rd, 2010

I posted the new version of Punto Engine PHP, or PEP in the version 0.2. This version brings significant improvements over the previous version, by managing the system better able to make templates for dynamic pages much easier and faster. Among the improvements are:

  • Added change log file
  • Added virtual path (accesible in the browser) at the kernel
  • Added servlet url in a class attribute
  • Added detailed exception trace where are configured in web.xml
  • Added in web.xml a config section with debug mode
  • Added debug atribute in servlet class to realice the traces if the project are in debug mode
  • Added base redirect same a aspnet. If redirect url starts with "~/", the url take the base project url
  • Added masterpage functionality with secction page and dinamic code same include and php code
  • Added functionality when a path has ~/ chageit for virtual path same a dotnet with runat server
  • Updated the error page message that are reference at IDCS Server and change its for "Punto Engine Server"
  • Updated the HomeController class to a TestController with many examples of templates use with masterpage
  • Fixed the some exception pages showed when have a multiple errors. Now stops the process when the error throwed
  • Delete temporaly the PepAdminController from web.xml to can't access it

The improvements in this release are quite substantial and allows for more fluid pages. For those who want to download it can do so by logging into the project puntoengine website.

Via: Publicada la versión 0.2 de puntoengine

Curious char methods, IsLetter and IsDigit

September 30th, 2010

The other day, we were about my boss and I make a control on a text field of a web application. The idea was that this area could include only text or numbers, but in no case should be possible to include strange characters. Let us be honest and if we needed to use this in languages where the framework was not prepared to differentiate what kind of character he was, just creating an array or dictionary, and comparing the characters or by using regular expressions. Of course, since the leap to Microsoft technology (in this case Visual Basic. Net) I decided to find some class or method to do just what I wanted, and found.

DotNet exists a class Char, which is the method Char.IsLetterOrDigit(boolean) which indicates a char or character, and this will return true or false if a number or letter and false if not. Ride a function to go character by character text field is alphanumeric checking whether or not the idea of erasing non-alphanumeric characters. Once mounted the role, I try a few times and everything works great. When we're going to have to get into production ... surprise, the last test failed (but impossible), we slipped the finger and put a "1", put a "º". When we investigate (ha try boars), we realize that this function requires that the characters "º" and "ª" are alphanumeric, we are lyrics, I suppose the "o" and "a".

We control it and go, but if someone is in the same situation and knows that if some function to work properly comment on it. And if anyone looking for internet is blocked and is found in this post, you can breathe easy, because there is a malfunction, it does not work quite right. Yes, I know that for some "º" and "ª" are letters, but they are really special characters.

Via: Char curiosidad de IsLetter e IsDigit

Virtual namespaces with javascript

September 27th, 2010

In most programming languages relatively mature and powerful, there is what we call a namespace. The namespace is a container name where we hold classes, functions and variables that can be repeated with the same name in other namespaces without involving an error. A example of what the real utility of organizing or namespaces is in the graphics libraries of programming languages, since in these libraries a very common element the Window class is usually referred to a window but may be in GTK, QT,. Net, Windows API, etc. (This is not quite correct since GTK would GtkWindow but for the example). Javascript as a language and more to mature over the years and relatively standardized by modern browsers, despite being a multiparadigm programming language, as do PHP, also has namespaces, but due to weakness of these namespaces variables are more virtual than real.

And understanding that it is serving a namespace to declare it in javascript, what we should do is use arrays to generate. Thanks to the weakness and dynamism of the variables in javascript, we can assign to a variable array of positions by name and access these through a punctuation mark "." and not their common key array ["key"], so here comes the trick. Basically what we do by declaring a namespace in javascript is assigned to a variable array with the positions of the namespace and then and within these positions, log in as if from .Net or Java in question. With an example is simpler.

var Indalcasa = { Tools : {} };
 
Indalcasa.Tools.MyClass = function() {
  this.variable = "value";
 
  this.myMethod = function(parameters) {
    return this.variable + " " + parameters;
  }
};
 
var iClass = new Indalcasa.Tools.MyClass();
iClass.variable = "new value";
alert(iClass.myMethod("test"));

The above example shows an alert window with a message such that "new value test", as we have instantiated the namespace class, we changed the value to the class attribute and the class method, concatenate the value of class attribute to the parameters that we have passed. This is a small and simple example of what can be done with javascript, namespaces and classes.

Via: Namespaces o espacios de nombre virtuales en javascript

Released version 0.1.1 of puntoengine

September 26th, 2010

Yesterday I posted the version 0.1.1 of puntoengine. Punto Engine PHP or puntoengine or shortened PEP, is an alpha-phase PHP MVC framework type Model View Controller. This framework bases its functionality in a mixture of different web frameworks such as ASP.Net, Java Servlets and PHP in and old framework called RLM Engine. Today, despite being in a very green stage of development, but it is fully operational and can build websites with a servlets as you would a website in JavaVersion 0.1.1 in revision 7 puntoengine is the last released version 0.1.1 of puntoengine but with full documentation of the code, making it easier for anyone who wants to see and study the code and its operation, make it easier to follow. For version 0.2 of puntoengine is expected to create an administrator, currently under development to manage including servlets installed and preparation for future plugins such as the CMS system.

This is a long-term and very ambitious to go far and I hope to use it to build several websites that I have in mind. According to a calculation made according to the COCOMO system, currently the project with almost 1500 lines of code, would have a private cost of 6,000 €. For those who want to take a look and even collaborate with the development, documentation or providing issues, you can do on the web puntoengine project.

Via: Publicada la version 0.1.1 de puntoengine

Valid Autocomplete=”off” for XHTML

September 12th, 2010

When writing a text in a text input type (<input type="text" />) we find that generally the browser is going to autocomplete what you are writing at that time with text that we have already written earlier, recalling for input called "name" the name or names you have entered.

The practice of auto is very helpful whenever we have repetitive forms such as registration forms are similarly always call and they always ask the same data, avoid rewrite all our data over and over again.

There are cases where you might not want that the browser will autocomplete the text, may be the case that we have an online store that paying by credit card and we do not want that credit card we autocomplete. To avoid this, there is an attribute that is used by the vast majority of browsers is the attribute autocomplete="on|off", if the value is cleared off, tells the browser not autocomplete the field.

Although in many cases it is good practice to use the autocomplete attribute, the W3C has recognized this attribute as an attribute of the input tag with which to try to validate and meet with the autocomplete attribute, we give a validation error. To resolve the validation error that will be using javascript to add this attribute, the functionality is still there, but we will not have validation issues.

The only thing we do is add the following code at the bottom of the page, or at least, just after declaring the input

<script type="text/javascript">
    //<![CDATA[
    var inputElement = document.getElementById("inputId")
    inputElement.setAttribute("autocomplete", "off");
    //]]>
  </script>

In this way we will ensure that when entering text in a input autocomplete not by the browser and if we can do for you autocomplete javascript.

A good and beautiful example of autocomplete fields is the use for muchoviaje in the areas of origin and destination.

Via: Autocomplete=”off” valido para XHTML

Diferences between do while loops in VisualBasic and CSharp

September 11th, 2010

I've been working with Visual Basic. Net, but as a person made for years in languages such C (C, C++, Java, C#, PHP, etc) way to program in Visual Basic, it costs a lot and things so commonplace as making a loop, a switch or the like can become a dangerous trap that will consume us many valuable minutes.

The latest that I have encountered has been the do while loop. In any language type C, practically independent of language these loops are equal:

  do {
    //loopcode
  } while(condition);

But since we are using Visual Basic and here everything works differently, is like our typical friend who always go against everything. For example to make a do while loop the syntax its:

  Do
    'loopcode
  Loop While condition

Maybe the people who know programming in Visual Basic or see what a folly, but for people like me for work are forced to program in that language and for many months leading still prefer C# to VB.Net, things so everyday can be a miniworld.

Already happened and to take advantage of the post, I explain that while do loops are loops that will travel like the while, as a function of a condition which, if true will loop over the loop and that the main difference between while loops and do while, is that the while, it first checks the condition before making a loop iteration, while the first will give a do while loop iteration and the final check whether iteration should continue. Put another way, the while, first check that we enter the loop and if true, we in the do while, first entered the loop and the end of this check if we should walk around more.

Via: Bucle do-while en VisualBasic

Manage Windows events in C#

August 27th, 2010

In dotnet exists a class that has a series of events specific to the operating system (Windows) that allows us to drive or be aware of certain changes in it, as might be the case for changes to hours, screen. to implement on the application and that this does not make strange.

The class responsible to catch for certain actions of the operating system is within in Microsoft.Win32 namespace and the class is abstract and non-heritable SystemEvents. This class provides different types of events listed below with a brief description.

  • DisplaySettingsChanged: occurs when the user changes the display settings.
  • DisplaySettingsChanging: occurs when configuration screen is changing.
  • EventsThreadShutdown: occurs before the end of the subthread listener for system threads.
  • InstalledFontsChanged: occurs when the user adds or removes fonts from the system.
  • LowMemory: occurs when the system runs out of available RAM.
  • PaletteChanged: occurs when the user switches to an application that uses a different palette.
  • PowerModeChanged: occurs when the user suspends or resumes the system.
  • SessionEnded: occurs when the user logoff or shutdown the system.
  • SessionEnding: occurs when the user tries to logoff or shutdown the system.
  • SessionSwitch: occurs when change the session between users.
  • TimeChanged: occurs when you change the system time.
  • TimerElapsed: occurs when the interval has passed a Windows timer.
  • UserPreferenceChanged: occurs when user preferences change.
  • UserPreferenceChanging: occurs when you are going to change user preferences.

In this short list I have not tried them all, but if some of them with proper operation. There are many events that can be used as the fact of low memory to launch the garbage collector or locking control system to keep data in memory to a file.

To use them would use the following code:

 using Microsoft.Win32;
 ...
 SystemEvents.TimeChanged +=
   new System.EventHandler(this.message);
 ...
 public void message(object sender, EventArgs e) {
   MessageBox.Show("System time had changed");
 }

Via: Controlar eventos de Windows en C#

Differences between preg_match and ereg

August 25th, 2010

Let me explain the difference between these two functions of php to evaluate and extract information on strings by using regular expressions or patterns.

Apparently these two functions are apparently equal. The differences between them are minimal, for example preg_match is required to establish some bars that define the pattern, or that this function returns an integer (int) from 0 to n, depending on the matches found, while ereg returns a boolean depending on whether or not matches found.

Where is the difference between preg_match and ereg? Well the main difference as both also return an array with matches by reference, is power of these functions. The function preg_match is very powerful and very useful as ereg, generally tend to use preg_match to extract information from text and the ereg to assess whether a text plays a pattern or not, as is the case of an email [a-zA-Z0-9]{1,}@[a-zA-Z0-9]{1,}\.[a-zA-Z]{2,3}. The problem comes when we perform a relatively complex pattern on a long text, that's when we see the true power difference between preg_match and ereg, as the preg_match could take up to 60 seconds to analyze it (I speak from experience) while the same pattern evaluated with ereg, take about 5 or 6 seconds.

In conclusion we could say that preg_match is an ideal function to analyze and make patterns on relatively small texts, such as the contents of a web page for example, and ereg is to evaluate fast patterns as words or emails and when the pattern to evaluate is very large.

Matias comments that from version 5.3 of PHP launches E_DEPRECATED warning and from version 6 of PHP removed ereg function of PHP function list.

Via: Diferencias entre preg_match y ereg

Connect url in c#

August 25th, 2010

Today you need to connect to web pages, either to extract information from a page or to connect to aWebService. DotNet or .Net provides us a tools to connect againt WebService, but there are excepcional cases, we need to connect in a more archaic and traditional, for example cURL library works.

In this case will show an example of how to connect to a URL of a WebService as fictitious and reflect their content and then be treated.

First create a String where to put the url of the link. Then contect objects using HttpWebRequest and HttpWebResponse and generate a StreamReader with the answer.

The code would look like:

  String baseUri = "http://rutaalwebserice";
  HttpWebRequest connection =
  (HttpWebRequest)HttpWebRequest.Create(baseUri);
 
  connection.Method = "GET";
  HttpWebResponse response =
  (HttpWebResponse)connection.GetResponse();
 
  StreamReader sr =
  new StreamReader(response.GetResponseStream(),
  Encoding.UTF8);

We have our variable sr (StreamReader) ara can use it to work with the assumption XML or HTML returned.

It is important to note, that to use these libraries, we need to include the following namespaces:

  using System.Web;
  using System.Net;
  using System.IO;
  using System.Text;

Via:  Diferencias entre preg_match y eregConectar a una url en C#