Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET Structures 1

Status
Not open for further replies.

mcauliff

Programmer
Feb 26, 2007
71
0
0
US
I have a structure that contains variables (i.e. Name, Address, Cit, State, Zip, telephone).

Is there a way to clear the structure with 1 command, like structure.clear?

or must I clear each variable?
 
Code:
    Structure Customer
        Dim Name As String
        Dim Address As String

        Sub Clear()
            Name = ""
            Address = ""
        End Sub
    End Structure

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

        Dim c As New Customer
        c.Name = "John Doe"
        c.Address = "123 Main Street"
        MessageBox.Show("Name:  " & c.Name & "  Address:  " & c.Address)
        c.Clear()
        MessageBox.Show("Name:  " & c.Name & "  Address:  " & c.Address)

End Sub
 
Structures may also contain methods. You would have to clear each field individually, but only in the structure's Clear method:
Code:
Public Structure YourStruct

    Public Name As String
    ' ...
    Public Telephone As String

    Public Sub Clear()
        Name = String.Empty
        ' ...
        Telephone = String.Empty
    End Sub

End Structure
 
You could also just destroy your instance of the object, and then re-instatiate it, giving the same effect.
 
Structures gave birth to Classes in object oriented programming. You can write methods (functions, subs) to add them functionallity
 
progcompu said:
Your program may be using structures currently but
the preferred way is to use classes.

Says who? You can't really make sweeping generalizations like that with the little information provided about the project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top