Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Suggestions for Workstation Config File
Message
De
17/03/2003 18:47:05
Keith Payne
Technical Marketing Solutions
Floride, États-Unis
 
 
À
17/03/2003 17:48:17
Keith Payne
Technical Marketing Solutions
Floride, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00766747
Message ID:
00766810
Vues:
25
This message has been marked as the solution to the initial question of the thread.
Some details on defining a class in VB.NET may be helpful.

To create a new class:

1. Right-click the project name in the project explorer window.
2. Choose Add/Add New Item ... from the menu.
3. Choose the Class template and change the name of the file to WorkstationSettings.vb.

You are presented with a blank class template that you can fill in with properties, fields and methods. Since the class will be used for storing data, you can concentrate on properties and fields only.

Something like the following will get you started:
Public Class WorkstationSettings

    ' Internal fields for storing the data

    Private _SQLServerName As String
    Private _IP As String
    Private _ColorScheme As String

    ' Public Properties for exposing the data

    Public Property SQLServerName() As String
        Get
            Return _SQLServerName
        End Get
        Set(ByVal Value As String)
            _SQLServerName = Value
        End Set
    End Property

    Public Property IP() As String
        Get
            Return _IP
        End Get
        Set(ByVal Value As String)
            _IP = Value
        End Set
    End Property

    Public Property ColorScheme() As String
        Get
            Return _ColorScheme
        End Get
        Set(ByVal Value As String)
            _ColorScheme = Value
        End Set
    End Property

End Class
Of special note is that the class only has public properties, not public fields. This is important when defining classes because you can add functionality to the properties at a later date without the need to re-compile the programs that use the class.

Once you have the preceding code saved in your WorkstationSettings.vb file, you can use the class to read and write the settings in your startup program:
' Place the Imports statements at the top of your code file
Imports System.Xml.Serialization
Imports System.IO

        ' Create an object to hold the settings based on the WorkstationSettings Class
        Dim Settings As new WorkstationSettings

        ' Create an XmlSerializer object to perform the reading and writing of the XML file
        Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(WorkstationSettings))


        ' Read the settings from the XML file:

        ' Open the XML file
        Dim myFileStream As FileStream = New FileStream("myFileName.xml", FileMode.Open)
        ' Read the XML file and copy the contents directly to the object that holds the settings
        Settings = mySerializer.Deserialize(myFileStream)
        ' Don't forget to close the file!
        myFileStream.Close()


        ' Set the properties of the Settings object:

        With Settings
            .SQLServerName = "localhost"
            .IP = "192.168.1.1"
            .ColorScheme = "Clouds"
        End With


        ' Write the settings to an XML file:

        ' Create/Overwrite the XML File
        Dim myWriter As StreamWriter = New StreamWriter("myFileName.xml")
        ' Write the object contents directly to the XML file
        mySerializer.Serialize(myWriter, Settings)
        ' Close the file
        myWriter.Close()
I've left out any error checking for brevity's sake. The code requires some Try..Catch blocks to perform the error trapping because you are reading and writing files.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform