Sunday 22 September 2013

string to double in vb.net

DEMO PROJECT TO CONVERT STRING VALUE TO DOUBLE USING VB2010


To convert a string  to double in .net there are many methods available . A string value is collection of number of characters within a string variable eg:-

Dim a as string = " A String Value "

Double values are  floating point numbers . These are values with precession To convert a string value to double value the main avaliable methods are :-

* CDBL Method    :-                 Cdbl ( string_value ) 

* Convert . ToDouble   :-          Convert . ToDouble ( string_value )  

* TryParse Method 

The Best Method to convert string value to double from methods given above is TryParse . TryParse will first check whether given string value can be converted to double or not If these values cannot be converted into Double then it does not return anything . TryParse method is used with if condition to check whether string value can be converted to double or not If it cannot be converted to Double value then we can Pass string in message box that "This Value Cannot Be Converted To  Double"

Design of Project string to double in vb.net



Code for string to double in vb.net


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim string_value As String
        string_value = TextBox1. Text
        Dim double_value As Double
        If Double.TryParse(string_value, double_value) Then
            MsgBox(double_value)
        Else
MsgBox("This String Value Cannot Be Converted To Double")
        End If
    End Sub

End Class

Explanation of Code :- string to double in vb.net



  1. Public class Form1
    In first line, i have declared a class . The access specifier of class is Public so that in case of using inheritance other class can easily access this class by using an instance or object of this class
  2.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. Here, We have declared a Private Sub Procedure name Button1. This sub-procedure is of object Named or control named Button1 . 
  • Button1_Click , Here we are declaring an Event for Button Control. We are using click event so that the procedure will execute or code that is written under this Button1 control will run when click the button at runtime.
  • Dim String_value as string , In this line of code we have simply declared a variable named "string_value" . Dim is a keyword that is used in visual basic to declare a variable . followed by name of variable by keeping in mind the rules for declaring the variable in visual baisc. Then 'as ' is also a keyword this keyword is used to specify followed by it that what type of data we are going to store in the variable that we are creating now. Then we will provide the datatype of variable

     4. " string_value = TextBox1. Text" :- Now we are simply assigning the value that we enter in the textbox at runtime into string variable we have created in previous step

    5.  Dim double_value As Double , In this line of code we have simply declared a variable named "double_value" . Dim is a keyword that is used in visual basic to declare a variable . followed by name of variable by keeping in mind the rules for declaring the variable in visual baisc. Then 'as ' is also a keyword this keyword is used to specify followed by it that what type of data we are going to store in the variable that we are creating now. Then we will provide the datatype of variable

    6. In code below we have used Try.Parse this is mainly used to try that the value given as parameter of type string can be converted into double value or not . If this value cannot be converted from string to double in .net then it will execute the else statement telling the Trying to Parse string value into double value is unsuccessfull . If given value can be converted then it will convert it and given the message along with string to double converted value
     If Double.TryParse(string_value, double_value) Then
                MsgBox(double_value)
            Else
    MsgBox("This String Value Cannot Be Converted To Double")
            End If

    7. Then End Sub will close the Button1_click subprocedure  and End Class will end the class form1

Share this

0 Comment to "string to double in vb.net"

Post a Comment