Archive for the VB.NET Category

VB.Net with Dates and Times

Posted in Coding, VB.NET on 18 November 2008 by harisanto

VB.NET’s DateTime anatomy represents an burning in time and is usually bidding as a accurate date and time of the day. DateTime comes in accessible whenever you charge to actuate the system’s date and time; it aswell performs assorted operations on date/time variables. In this article, I appearance how to advance DateTime backdrop and methods so you can plan with DateTime ethics in VB.NET.

Here are listings of the a lot of frequently acclimated backdrop and methods of the DateTime structure.

Properties:

  1. Date: allotment the date basic of the DateTime value.
  2. Day: allotment the day of the ages basic of the DateTime value.
  3. DayOfWeek: allotment the day of the anniversary basic of the DateTime value.
  4. DayOfYear: allotment the day of the year basic of the DateTime value.
  5. Hour: allotment the hour basic of the DateTime value.
  6. Millisecond: allotment the milliseconds basic of the DateTime value.
  7. Minute: allotment the minute basic of the DateTime value.
  8. Month: allotment the ages basic of the DateTime value.
  9. Now: allotment a DateTime amount that is the accepted bounded date and time on this computer.
  10. Second: allotment the abnormal basic of the DateTime value.
  11. TimeOfDay: allotment the time of day of the DateTime value.
  12. Today: allotment the accepted arrangement date.
  13. UtcNow: allotment a DateTime amount that is the accepted bounded date and time on this computer bidding as the Coordinated Universal Time (UTC).
  14. Year: allotment the year basic of the DateTime value.

Methods :

  1. Add: adds the amount of the defined TimeSpan to the DateTime value.
  2. AddDays: adds the defined amount of canicule to the DateTime value.
  3. AddHours: adds the defined amount of hours to the DateTime value.
  4. AddMilliseconds: adds the defined amount of milliseconds to the DateTime value.
  5. AddMinutes: adds the defined amount of account to the DateTime value.
  6. AddMonths: adds the defined amount of months to the DateTime value.
  7. AddSeconds: adds the defined amount of abnormal to the DateTime value.
  8. AddYears: adds the defined amount of years to the DateTime value.
  9. DaysInMonth: allotment the amount of canicule in the defined ages of the defined year.
  10. IsLeapYear: allotment an adumbration of whether the defined year is a bound year.
  11. Subtract: subtracts the defined time or continuance from the DateTime value.
  12. ToLocalTime: converts the accepted Coordinated Universal Time (UTC) to bounded time.
  13. ToLongDateString: converts the amount of this instance to its agnate continued date cord representation.
  14. ToLongTimeString: converts the amount of this instance to its agnate continued time cord representation.
  15. ToShortDateString: converts the amount of this instance to its agnate abbreviate date cord representation.
  16. ToShortTimeString: converts the amount of this instance to its agnate abbreviate time cord representation.
  17. ToUniversalTime: converts the accepted bounded time to Coordinated Universal Time (UTC).

Comparing 2 Dates

Posted in Coding, VB.NET with tags , on 10 October 2008 by harisanto

I discovered a very interesting little twist on the differences between the Today property and the Now property in the DateTime Class. I was working on something recently and using the built-in Compare method of the DateTime Type. Essentially the user selected a date and I had a validation snippet that was supposed to check that they had not picked a future date. One of the ways they could choose their date was via a DateTimePicker control. I thought that this code might do the trick:

Dim EndDate As DateTime
Dim Current As DateTime = Today

If DateTime.Compare(Current, EndDate) < 0 then
    label6.text ="" "CONTRACT EXPIRED LESS THAN"
end if
if datetime.compare(current, enddate) ="" 0 then
    label6.text ="" "CONTRACT EXPIRED EQUALS"
end if
if datetime.compare(current, enddate) > 0 Then
    Label6.Text = "CONTRACT NOT EXPIRED GREATER"
End If

Membuat Type Data Null

Posted in Coding, VB.NET with tags , on 24 September 2008 by harisanto

Dengan support terbaru yang terdapat pada .NET, beberapa fitur telah ditambahkan. Salah satunya adalah “Build Typesafe Generic Classes“. Sekarang kita dapat menyelesaikan permasalahan pada type data NULL.

Apakah biasanya anda melakukan pelambangan pada data yang NULL? Saat ini .NET telah melakukan perbaikan dan solve problem.

Nilai NULL yang dikenali oleh Visual Basic sebagai flag istimewa yang menunjukkan data yang tidak muncul. kebanyakan nilai NULL menunjukan bahwa resource atau bisa kita ibaratkan benda tersebut ada tetapi tidak terlihat atau tidak terciptakan. Misalnya pada kode berikut, FileStream berisi Null referensi karena tidak ter-instantiated dengan New Keywords:

Dim fs As FileStream
If fs Is Nothing
    Console.WriteLine("Object contains a null reference.")
End If

Core tipe data seperti  integer dan string tidak dapat berisi NULL Values. Variable numerik akan otomatis bernilaoi 0 (Nol) dan boolean akan berisi variable False. Sedangkan tipe data String akan berisi kosong (“”) secara otomatis. Pada dasarnya, bila anda melakukan set type data dengan variable Nothing pada code, akan secara otomatis bila terjadi kekosongan data akan dikembalikan pada nilai kosong (0,False,””). Contoh:

Dim j As Integer = Nothing
If j = 0 Then
    Console.WriteLine("Non-nullable integer j = " & j)
End If

Bentuk ini kadang-kadang menyebabkan masalah, dimana tidak ada pembeda antara nilai kosong (NULL) dengan nilai yang tidak pernah ada. Secara sederhana saya contohkan, bila anda membuat code dimana anda harus mengembalikan nilai text pada textbox dan pada saat itu diset 0, maka anda tidak dapat tahu, apakah textbox tersebut memang kosong atau 0 adalah nilai awal (Tentu saja bila tidak disertai Flag-flag tertentu sebagai konfigurasi).

.NET 2.0 telah memberikan solusinya, yaitu System.Nullable Class yang dapat melingkupi berbagai macam tipe data. Bila anda membuat suatu definisi yang tidak mempunyai nilai awal (atau nullable) atau tidak melakukan setting nilai dasar, maka anda dapat melakukan test dengan metode Nullable.Hastype() dan anda akan mendapatkan nilai balik Nullable.Value

Berikut contohnya:

Dim i As Nullable(Of Integer)
If Not i.HasValue Then
    Console.WriteLine("i is a null value")
End If

i = 100
If i.HasValue Then
    Console.WriteLine("Nullable integer i = " & i.Value)
End If

…Memakai Nullable sebagai referensi object sangat bermanfaat walaupun anda tidak membutuhkannya, ini akan tetap dapat memberikan anda keuntungan, yaitu paling tidak kode anda akan lebih mudah untuk dibaca HasValue() yaitu metode yang lebih baik untuk melakukan tes untuk value Nothing. Yang terbaik adalah, anda dapat mengadakan perubahan secara berkesinambungan, karena Class Nullable mempunyai kemampuan untuk melakukan konversi antara Nullable dengan type data itu sendiri.

Mengambil Informasi Applikasi

Posted in Coding, VB.NET with tags , , on 23 September 2008 by harisanto
My.Application obyek adalah pusat informasi yang dengan cepat dapat anda dapatkan. Mengambil informasi dari sana dapat semudah anda menariknya dari system properti.

Memakai My.Application adalah salah satu cara untuk mengambil versi, lokasi dan parameter yang anda gunakan untuk applikasi anda.

Read more »

Make Simple Data Types Nullable

Posted in Coding, VB.NET with tags on 22 August 2008 by harisanto
A null value (identified in Visual Basic by the keyword Nothing), is a special flag that indicates no data is present. Most developers are familiar with null object references, which indicate that the object has been defined but not created. For example, in the following code, the FileStream contains a null reference because it hasn’t been instantiated with the New keyword:

Dim fs As FileStream
If fs Is Nothing
    ' This is always true because the FileStream hasn't
    ' been created yet.
    Console.WriteLine("Object contains a null reference.")
End If

Core data types like integers and strings can’t contain null values. Numeric variables are automatically initialized to 0. Boolean variables are False. String variables are set to an empty string (””) automatically. In fact, even if you explicitly set a simple data type variable to Nothing in your code, it will automatically revert to the empty value (0, False, or “”), as the following code demonstrates:

Dim j As Integer = Nothing
If j = 0 Then
    ' This is always true because there is an
    ' implicit conversion between Nothing and 0 for integers.
    Console.WriteLine("Non-nullable integer j = " & j)
End If

This design sometimes causes problems, because there’s no way to distinguish between an empty value and a value that was never supplied in the first place. For example, imagine you create code that needs to retrieve the number of times the user has placed an order from a text file. Later on, you examine this value. The problem occurs if this value is 0. Quite simply, you have no way to know whether this is valid data (the user placed no orders), or it represents missing information (the setting couldn’t be retrieved or the current user isn’t a registered customer).

Thanks to generics, .NET 2.0 has a solutiona System.Nullable class that can wrap any other data type. When you create an instance of Nullable you specify the data type. If you don’t set a value, this instance contains a null reference. You can test whether this is true by testing the Nullable.HasType( ) method, and you can retrieve the underlying object through the Nullable.Value property.

Here’s the sample code you need to create a nullable integer:

Dim i As Nullable(Of Integer)
If Not i.HasValue Then
    ' This is true, because no value has been assigned.
    Console.WriteLine("i is a null value")
End If

' Assign a value. Note that you must assign directly to i, not i.Value.
' The i.Value property is read-only, and it always reflects the
' currently assigned object, if it is not Nothing.
i = 100
If i.HasValue Then
    ' This is true, because a value (100) is now present.
    Console.WriteLine("Nullable integer i = " & i.Value)
End If

using Nullable with full-fledged reference objects? Although you don’t need this ability (because reference types can contain a null reference), it still gives you some advantages. Namely, you can use the slightly more readable HasValue() method instead of testing for Nothing. Best of all, you can make this change seamlessly, because the Nullable class has the remarkable ability to allow implicit conversions between Nullable and the type it wraps.

Use the My Objects to Program Common Tasks

Posted in Coding, VB.NET with tags , , , , , on 27 February 2008 by harisanto

The new My objects provide easy access to various features that developers often need but don’t necessarily know where to find in the sprawling .NET class library. Essentially, the My objects offer one-stop shopping, with access to everything from the Windows registry to the current network connection. Best of all, the My object hierarchy is organized according to use and is easy to navigate using Visual Studio IntelliSense.

Tired of hunting through the extensive . NET class library in search of what you need? With the new My objects, you can quickly find some of the most useful features . NET has to offer.

There are seven first-level My objects. Out of these, three core objects centralize functionality from the .NET Framework and provide computer information. These include:

My.Computer
This object provides information about the current computer, including its network connection, the mouse and keyboard state, the printer and screen, and the clock. You can also use this object as a jumping-off point to play a sound, find a file, access the registry, or use the Windows clipboard.

My.Application
This object provides information about the current application and its context, including the assembly and its version, the folder where the application is running, the culture, and the command-line arguments that were used to start the application. You can also use this object to log an application event.

My.User
This object provides information about the current user. You can use this object to check the user’s Windows account and test what groups the user is a member of.
Along with these three objects, there are another two objects that provide default instances. Default instances are objects that .NET creates automatically for certain types of classes defined in your application. They include:

My.Forms
This object provides a default instance of each Windows form in your application. You can use this object to communicate between forms without needing to track form references in another class.

My.WebServices
This object provides a default proxy-class instance for every web service. For example, if your project uses two web references, you can access a ready-made proxy class for each one through this object.

Finally, there are two other My objects that provide easy access to the configuration settings and resources:

My.Settings
This object allows you to retrieve custom settings from your application’s XML configuration file.

My.Resources
This object allows you to retrieve resourcesblocks of binary or text data that are compiled into your application assembly. Resources are typically used to store localized strings, images, and audio files.

Some of the My classes are defined in the Microsoft.VisualBasic.MyServices namespace, while others, such as the classes used for the My.Settings and My.Resources objects, are created dynamically by Visual Studio 2005 when you modify application settings and add resources to the current project.

Warning: Note that the My objects are influenced by the project type. For example, when creating a web or console application, you won’t be able to use My.Forms.

Apa Itu Dot Net?

Posted in Coding, VB.NET with tags on 27 February 2008 by harisanto

Ketika .NET diperkenalkan tahun 1999, Microsoft memposisikan teknologi sebagai suatu panggung untuk membangun dan mengkonsumsi Extensible Markup Language (XML) sebagai bahasa formal dalam pengembangan web. XML mengijinkan para penggunannya mengakses informasi apapun yang diinginkan pada suatu applikasi Windows berbasis browser dan menjalankannya dari sistem komputernya serta mengakses data2 dari server. Protokol-protokol yang digunakan adalah protokol standard seperti SOAP, HTTP dan SMTP.

Kerangka awal dari sistem pengembangan .NET adalah pengembangan applikasi terdistribusi sempurna. Sistem manajemen IT yang aman didukung sistem arsitektur yang sederhana adalah tujuan akhir dari sistem .NET.