Posts Tagged ‘microsoft’

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

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#