Posts Tagged ‘visualstudio’

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

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#