0

I am studying VB.NET

I have a question about changing value in constructor.

I already create one constructor and I would like to access this constructor. And then , changing one part of constructor.

Is it possible to change it ?

For example, I have code...

Public Class Picbook
    Public title As String
    Dim price As String

    Public Sub New(t As String, p As String)
        title = t
        price = p
    End Sub


End Class

and I have main form class

This class add book type and price as pair

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim al() As String = {"Comic", "Textboox"}
        Dim als As New ArrayList ' = {"0,12123,3", "213,21312,124"}
        Dim ass As New ArrayList
        als.Add("0,12123,3")
        als.Add("213,21312,124")
        For I = 0 To al.Length - 1
            Dim c As New Picbook(al(I), als(I))
            ass.Add(c)
        Next     
' want to change price of comic book here


    End Sub

I would like to access constructor and then change price of Comic.

Should I remove constructor in arraylist and then re-insert "Comic " and "Price" again ?

or is there anyway to change value of price ?

thanks

Update

I think, properties might help to solve this problem

I used reference from

http://www.dreamincode.net/forums/topic/293428-defining-properties-and-constructors/

2 Answers 2

0

The constructor is only to set a first value to the fields, you can even have more than 1 constructor with different signature and call the one you need. You can change any field later modifying public fields which is not recommended, or through procedures or through properties which is normally the best option. By the way, if you are working with prices is better to change the type to decimal, the string type always requires more resources. Here is an example:

Option Strict On

Module Module1

Sub Main()


    Dim oPicbook As IList(Of Picbook) = New List(Of Picbook)

    For i As Integer = 0 To 1
        oPicbook.Add(New Picbook(Console.ReadLine(), Convert.ToDecimal(Console.ReadLine())))

    Next


    For Each item As Picbook In oPicbook
        Console.WriteLine("Title: {0} Price: {1}", item.title, item.price)
    Next

    Console.Read()
End Sub
Public Class Picbook
    Public title As String
    Public price As Decimal
    Public Sub New()

    End Sub
    Public Sub New(t As String, p As Decimal)
        title = t
        price = p
    End Sub

End Class

End Module

2
  • Thanks I thk , Property will be working for this case. However, I might need to use it carefully.
    – Bob Ma
    Apr 19, 2014 at 7:22
  • properties are safer than public fields anyway
    – user3477273
    Apr 19, 2014 at 7:25
0

Once the object has been created, you're done with the constructor. After that, you set the fields or, preferably, properties of the object to modify the days or contains

2
  • yep, I think, properties is only way to change value in the constructor. Thanks
    – Bob Ma
    Apr 19, 2014 at 7:24
  • It's not a case of changing values in the constructor. The constructor is, by definition, a special method that creates the object. That's it, that's all. Once the object exists, if you want to change the data in the object then you set a field, set a property or call a method. Apr 19, 2014 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.