Archive for the ‘Visual Basic .Net’ 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

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

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