dcsimg
 

Creating a Simple OOP Diary with Visual Basic.NET

Friday Mar 1st 2019 by Hannes DuPreez
Creating a Simple OOP Diary with Visual Basic.NET

Learn how to make a simple diary in Visual Basic.NET.

Introduction

Hello, and welcome to my article. In this short article, you will learn how to make a simple diary in Visual Basic.NET.

Practical

Create a new Visual Basic.NET Console Application. After the application has loaded, add a Class named clsEntry and add the following Properties and methods into it.

Class clsEntry

   Public Property dtDateOfentry As DateTime
   Public Property strContent As String

   Public Sub New(ByVal dtDate As DateTime, _
         ByVal strText As String)

      dtDateOfentry = dtDate
      strContent = strText

   End Sub

   Public Overrides Function ToString() As String

      Return dtDateOfentry & " " & strContent

   End Function

End Class

clsEntry contains two properties: DateOfEntry and strContent. The plan is to be able to enter a date and then supply the Diary entry. The plan further is to be able to search for entries as well as Delete entries based on the Date the entries were made. Add a Class named clsDatabase to the project.

Add the following Namespace to clsDatabase:

Imports System.Collections.Generic

We need this Namespace because we will be working with generic types. Add a List object to store the diary entries.

   Private lstEntries As List(Of clsEntry)

Add the Constructor:

   Public Sub New()

      lstEntries = New List(Of clsEntry)()

   End Sub

This instantiates the lstEntries object, and it is now ready to be used. Add the following two sub procedures to Add items or to Delete items:

   Public Sub Add(ByVal dtDate As DateTime, ByVal strText _
         As String)

      lstEntries.Add(New clsEntry(dtDate, strText))

   End Sub
   Public Sub Delete(ByVal dtDate As DateTime)

      Dim lstResults As List(Of clsEntry) = Find(dtDate, True)

      For Each Entry As clsEntry In lstResults

         lstEntries.Remove(Entry)

      Next

   End Sub

Add adds an entry based on the entered text and date. Delete deletes the entry or entries which were entered on a certain date. Add the Find method.

   Public Function Find(ByVal dtDate As DateTime, ByVal blnTime _
         As Boolean) As List(Of clsEntry)

      Dim lstResults As List(Of clsEntry) = New List(Of clsEntry)()

      For Each Entry As clsEntry In lstEntries

         If ((blnTime) AndAlso (Entry.dtDateOfentry = _
            dtDate)) OrElse ((Not blnTime) AndAlso _
            (Entry.dtDateOfentry.Date = dtDate.Date))
         Then lstResults.Add(Entry)

      Next

      Return lstResults

   End Function

This searches for Entries. Add the clsDiary class and add the following:

Class clsDiary

   Private dbData As clsDatabase

   Public Sub New()

      dbData = New clsDatabase()

   End Sub

   Private Function GetDate() As DateTime

      Console.WriteLine("Enter Date and Time")

      Dim dtDate As DateTime

      While Not DateTime.TryParse(Console.ReadLine(), dtDate)

         Console.WriteLine("Error. Try again:")

      End While

      Return dtDate

   End Function

   Public Sub Print(ByVal dtDay As DateTime)

      Dim lstResults As List(Of clsEntry) = dbData.Find(dtDay, _
         False)

      For Each Entry As clsEntry In lstResults

         Console.WriteLine(Entry)

      Next

   End Sub

   Public Sub Add()

      Dim dtDate As DateTime = GetDate()

      Console.WriteLine("Enter the entry text:")

      Dim strText As String = Console.ReadLine()

      dbData.Add(dtDate, strText)

   End Sub

   Public Sub Search()

      Dim dtDate As DateTime = GetDate()

      Dim lstResults As List(Of clsEntry) = dbData.Find(dtDate, _
         False)

      If lstResults.Count() > 0 Then

         Console.WriteLine("Found:")

         For Each Entry As clsEntry In lstResults

            Console.WriteLine(Entry)

         Next

      Else

         Console.WriteLine("Nothing found.")

      End If

   End Sub

   Public Sub Delete()

      Dim dtDate As DateTime = GetDate()

      dbData.Delete(dtDate)

   End Sub

   Public Sub Welcome()

      Console.Clear()
      Console.WriteLine("Welcome to your virtual diary!")
      Console.WriteLine("Today is: {0}", DateTime.Now)
      Console.WriteLine()

      Console.WriteLine("Diary Entries For Today:")

      Print(DateTime.Today)

      Console.WriteLine()
      Console.WriteLine("Diary Entries For Tomorrow:")

      Print(DateTime.Now.AddDays(1))

      Console.WriteLine()

   End Sub

End Class

You may see some similarities with the clsDatabase class; this is due to separation of the data from the business logic. The Database class serves as the storage manager, whereas the Diary class simply needs to invoke those methods or subs.

Make it work. Add the following code for the Module:

Module Module1

   Sub Main(ByVal args As String())

      Dim objDiary As clsDiary = New clsDiary()

      Dim cSelection As Char = "0"c

      While cSelection <> "4"c

         objDiary.Welcome()
         Console.WriteLine()

         Console.WriteLine("Choose:")
         Console.WriteLine("1 - Add an entry")
         Console.WriteLine("2 - Search for entries")
         Console.WriteLine("3 - Delete entries")
         Console.WriteLine("4 - Exit")

         cSelection = Console.ReadKey().KeyChar
         Console.WriteLine()

         Select Case cSelection

            Case "1"c

               objDiary.Add()

            Case "2"c

               objDiary.Search()

            Case "3"c

               objDiary.Delete()

            Case "4"c

               Console.WriteLine("Press any key to quit.")

            Case Else

               Console.WriteLine("Error.")

         End Select

         Console.ReadKey()

      End While

   End Sub

End Module

This sets up a new clsDiary object, and then determines what you have entered. Based on your supplied value, it will either add entries, delete entries, or search for entries. This is shown in Figures 1-3.

Start
Figure 1: Start

Entries Added
Figure 2: Entries Added

Entries Found
Figure 3: Entries Found

Conclusion

In this article, you have seen how quickly you can make a simple diary. Although it is not connected to a real world database, the principles are similar.

Home
Mobile Site | Full Site