Applying Absolute Value In VB.NET with ABS
VB.NET allows the conversion of figures (both positive and negative) into absolute figures easily. You can use the Mathematical Function as shown below
Math.ABS
For example, to change a figure value of -34167 into absolute figures, we can apply the codes as follows :
Dim myValue as Integer
myValue = Math.ABS(-34167)
MsgBox(myValue) 'The output now becomes 34167
That illustrates the use of the in-built Math.Abs function in VB.NET
VB.NET : Absolute Value (ABS)
Posted by Fluffy | 5:10 AM | Add Project Reference VB.NET, Math.ABS | 0 comments »Putting application to sleep for a specified miliseconds in VB.NET
In VB.NET, you can make an application idle ( or sleep ) for a specified miliseconds. For example, in your programming code, you might want to let your program to be idle for 500 miliseconds after executing a certain functions and then execute the next function.
Apply the code below between functions:
System.Threading.Thread.Sleep(500)
Note : The number used is in miliseconds
* 1 sec = 1000ms
VB.NET : Access to SQL Server Compact Edition
Posted by Fluffy | 4:59 AM | SQL Server CE, VB.NET | 0 comments »Accessing SQL Server Compact Edition Database using VB.NET
SQL Server Compact Edition, or simply SQL Server CE, allows a very convenient way of fetching data stored locally as cache. It is usually used in situations where commonly used data are stored locally and accessed directly instead of making queries to fetch the data again and again from the main database server. Accessing data stored locally can be many times faster compared to accessing data stored in the main database located somewhere on the network. Hence, by implementing and taking the full advantage of SQL Server CE, you can make your application runs faster.
Firstly, add the following reference at the top of your module programming code
Imports System.Data.SqlServerCe
Next, add the following as global variables so that you can call them anytime, anywhere in your code.
Friend SQLcommCE As SqlCeCommand 'SQL command
Friend daCE As SqlCeDataAdapter 'SQL adapter
Friend SQLConnCETempDB As SqlCeConnection 'SQL connection linking your local database
Then, add the following codes to establish the database connection to where your application loads when it runs. Data Source points to the path where your SQL Server CE database extension *.sdf is stored, and provides the password (if any) in the codes shown below.
SQLConnCETempDB = New SqlCeConnection("Data Source=C:\TempDB.sdf;Password='123'")
SQLConnCETempDB.Open()
Finally, in the programming code where you want to perform query to the database, you can simply use the codes shown as follows.
Dim SQLStm as String
SQLStm = "DELETE FROM LocalTable WHERE MYTABLE_ID = '123'"
SQLcommCE = New SqlCeCommand(SQLStm, SQLConnCETempDB) 'execute the query
SQLcommCE.ExecuteNonQuery()
That will delete the records with MYTABLE_ID = '123' in your LocalTable.
For INSERT INTO, UPDATE, DELETE or any other NonQuery database access, you can use
.ExecuteNonQuery
If you intend to query the table using the SELECT statement, you have to use the following code instead
.CommandType = CommandType.Text
See the example below:
Dim SQLStm as String
Dim ds as DataSet
Dim Rank, Name as String
SQLStm = "SELECT Rank, Name FROM LocalTable WHERE MYTABLE_ID = '123'"
SQLcommCE = New SqlCeCommand(SQLStm, SQLConnCETempDB) 'SQL query
SQLcommCE.CommandType = CommandType.Text
daCE = New SqlCeDataAdapter(SQLcommCE) 'Retrieve data from SQL Server CE
ds = New DataSet
daCE.Fill(ds) 'Populate dataset with retrieved data.
If ds.Tables(0).Rows.Count > 0 Then ' If record exists in LocalTable
Rank = ds.Tables(0).Rows(0).Item("Rank").ToString
Name = ds.Tables(0).Rows(0).Item("Name").ToString
Else 'record not exists in LocalTable
Rank = ""
Name = ""
End If
Now you can start implementing SQL Server CE in your applications
VB.NET : Mathematical Functions (Advanced)
Posted by Fluffy | 6:24 AM | Advanced Mathematical Functions, VB.NET | 0 comments »Using Advanced Mathematical Functions in VB.NET
More advanced mathematical functions in VB.NET utilizes the System.Math class. This class supports a wide variety of in-built functions such as :
Constants like E and Pi
Trigonometric functions like Sin, Cos & Tan
Logarithmic functions like Log & Log10
and other functions like Ceiling, Floor, Max, Min, Pow, Exp, Sqrt, Abs, Round etc.
The example below shows how some of these advanced mathematical functions are used.
Dim i, j, k as Integer
i = Math.Sqrt(25)
j = Math.Pow(2,5)
k = Math.Abs(-25)
Msgbox(i) '--- result has a value of 5
Msgbox(j) '--- result has a value of 32
Msgbox(k) '--- result has a value of 25
See also Simple Mathematical Functions

