Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,303 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 766 people online right now. Registration is fast and FREE... Join Now!




VB Classes

 
Reply to this topicStart new topic

VB Classes, Building a class

James Bond C++ Spy
23 Jan, 2008 - 07:13 PM
Post #1

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 89



Thanked: 1 times
My Contributions
I am trying to build a class. I get several errors. The first of which is option strict on disallows implicit conversions from integer to string. Here is my code if someone can help me. Thanks

client.vb
CODE


Public Class client
    Private info As String
    Private firstName As String
    Private lastName As String
    Private account As String
    Private balance As String

    Sub New(ByVal firstName As String, ByVal lastName As String, ByVal account As String, ByVal balance As String)

    End Sub

    Public Property stats() As String
        Get
            Return info
        End Get
        Set(ByVal value As String)
            info = value
        End Set
    End Property

End Class



and

AccountInformation.vb file
CODE


Public Class AccountInformationForm

    Private clients(0 To 8) As Client     ' Client object
    Private position As Integer = 0 ' current account

    'create array of Client objects
    Private Sub AccountInformationForm_Load(ByVal sender As _
       System.Object, ByVal e As System.EventArgs) _
       Handles MyBase.Load

        Dim count As Integer ' counter variable

        'array of first names
        Dim firstName() As String = _
           New String() {"John", "Sarah", "Jack", "Adam", "Diane", _
              "David", "Kristin", "Jennifer", "James"}

        ' array of last names
        Dim lastName() As String = _
           New String() {"Blue", "White", "Red", "Brown", _
              "Yellow", "Black", "Green", "Orange", "Gold"}

        'array of account numbers
        Dim account() As Integer = _
           New Integer() {1234652, 1234666, 1234678, 1234681, _
              1234690, 1234770, 1234787, 1234887, 1234007}

        ' array of account balances
        Dim balance() As Decimal = _
           New Decimal() {Convert.ToDecimal(1000.78), _
              Convert.ToDecimal(2056.24), Convert.ToDecimal(978.65), _
              Convert.ToDecimal(990.0), Convert.ToDecimal(432.75), _
              Convert.ToDecimal(780.78), Convert.ToDecimal(4590.63), _
              Convert.ToDecimal(7910.11), Convert.ToDecimal(9999.99)}

        ' loop and create 10 Client objects
        For count = 0 To clients.GetUpperBound(0)

            ' create new object and store into Client array
            clients(count) = New Client(firstName(count), _
            lastName(count), account(count), _
            balance(count))
        Next

    End Sub ' AccountInformationForm_Load

    ' display next object
    Private Sub nextButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles nextButton.Click

        position += 1 ' increment position

        ' if position is last (top) object
        If position > clients.GetUpperBound(0) Then
            position = 0    ' set to first position in array
            DisplayInformation() ' display information
        Else
            DisplayInformation()
        End If

    End Sub ' nextButton_Click

    ' display previous object
    Private Sub preveiousButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles previousButton.Click

        position -= 1 ' decrement position

        ' if position is last (bottom) object
        If position < 0 Then

            ' set to last position in array
            position = clients.GetUpperBound(0)
            DisplayInformation()
        Else
            DisplayInformation() ' display information
        End If

    End Sub ' previousButton_Click

    ' display information
    Private Sub DisplayInformation()

        ' use Position as index for each object
        firstTextBox.Text = clients(position).First
        lastTextBox.Text = clients(position).Last
        accountTextBox.Text = _
           Convert.ToString(clients(position).Account)

        ' format as currency
        balanceTextBox.Text = _
           String.Format("{0:C}", clients(position).Balance)

    End Sub ' DisplayInformation
End Class ' AccountInformationForm



User is offlineProfile CardPM
+Quote Post


Jayman
RE: VB Classes
23 Jan, 2008 - 09:29 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Moved to VB.NET.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: VB Classes
24 Jan, 2008 - 03:57 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 3,572



Thanked: 268 times
Dream Kudos: 525
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
One problem I see imediately is that you're not assigning any values in the client constructor.

client.vb
CODE

   Sub New(ByVal firstName As String, ByVal lastName As String, ByVal account As String, ByVal balance As String)
      Me.firstName = firstName
      Me.lastName = lastName
      ' ...
   End Sub


Hope this helps.

User is offlineProfile CardPM
+Quote Post

James Bond C++ Spy
RE: VB Classes
24 Jan, 2008 - 10:07 AM
Post #4

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 89



Thanked: 1 times
My Contributions
OK here is the new code. I have it working fine. Is there a better way of doing this? (the client.vb file) Any tips
Just curious Learning biggrin.gif

Client.vb file

CODE


Public Class client
    Private firstName As String
    Private lastName As String
    Private account As Integer
    Private balance As Decimal

    Sub New(ByVal firstName As String, ByVal lastName As String, ByVal account As Integer, ByVal balance As Decimal)
        Me.firstName = firstName
        Me.lastName = lastName
        Me.account = account
        Me.balance = balance
    End Sub

    Public Property fName() As String
        Get
            Return firstName
        End Get
        Set(ByVal value As String)
            firstName = value
        End Set
    End Property

    Public Property lName() As String
        Get
            Return lastName
        End Get
        Set(ByVal value As String)
            lastName = value
        End Set
    End Property

    Public Property acc() As Integer
        Get
            Return account
        End Get
        Set(ByVal value As Integer)
            account = value
        End Set
    End Property

    Public Property bal() As Decimal
        Get
            Return balance
        End Get
        Set(ByVal value As Decimal)
            balance = value
        End Set
    End Property

End Class



AccountInformation.vb file

CODE


Public Class AccountInformationForm

    Private clients(0 To 8) As Client     ' Client object
    Private position As Integer = 0 ' current account

    'create array of Client objects
    Private Sub AccountInformationForm_Load(ByVal sender As _
       System.Object, ByVal e As System.EventArgs) _
       Handles MyBase.Load

        Dim count As Integer ' counter variable

        'array of first names
        Dim firstName() As String = _
           New String() {"John", "Sarah", "Jack", "Adam", "Diane", _
              "David", "Kristin", "Jennifer", "James"}

        ' array of last names
        Dim lastName() As String = _
           New String() {"Blue", "White", "Red", "Brown", _
              "Yellow", "Black", "Green", "Orange", "Bond"}

        'array of account numbers
        Dim account() As Integer = _
           New Integer() {1234652, 1234666, 1234678, 1234681, _
              1234690, 1234770, 1234787, 1234887, 1234007}

        ' array of account balances
        Dim balance() As Decimal = _
           New Decimal() {Convert.ToDecimal(1000.78), _
              Convert.ToDecimal(2056.24), Convert.ToDecimal(978.65), _
              Convert.ToDecimal(990.0), Convert.ToDecimal(432.75), _
              Convert.ToDecimal(780.78), Convert.ToDecimal(4590.63), _
              Convert.ToDecimal(7910.11), Convert.ToDecimal(9999.99)}

        ' loop and create 10 Client objects
        For count = 0 To clients.GetUpperBound(0)

            ' create new object and store into Client array
            clients(count) = New Client(firstName(count), _
            lastName(count), account(count), _
            balance(count))
        Next

    End Sub ' AccountInformationForm_Load

    ' display next object
    Private Sub nextButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles nextButton.Click

        position += 1 ' increment position

        ' if position is last (top) object
        If position > clients.GetUpperBound(0) Then
            position = 0    ' set to first position in array
            DisplayInformation() ' display information
        Else
            DisplayInformation()
        End If

    End Sub ' nextButton_Click

    ' display previous object
    Private Sub preveiousButton_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles previousButton.Click

        position -= 1 ' decrement position

        ' if position is last (bottom) object
        If position < 0 Then

            ' set to last position in array
            position = clients.GetUpperBound(0)
            DisplayInformation()
        Else
            DisplayInformation() ' display information
        End If

    End Sub ' previousButton_Click

    ' display information
    Private Sub DisplayInformation()

        ' use Position as index for each object
        firstTextBox.Text = clients(position).fName
        lastTextBox.Text = clients(position).lName
        accountTextBox.Text = _
           Convert.ToString(clients(position).acc)

        ' format as currency
        balanceTextBox.Text = _
           String.Format("{0:C}", clients(position).bal)

    End Sub ' DisplayInformation
End Class ' AccountInformationForm


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:49PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month