Click to See Complete Forum and Search --> : is this the way to program in classes? or ......


reinier
March 9th, 2004, 03:43 PM
I am not used to programming win classes, yes I am a newbie :-x in classes
(windows app, SQL database, VB.studio net v2003)


I used the following code to get a connection string wich is located in
an XML-file.
I cannot do this in a form_open_event because the classes are triggered
before any action takas place in opening/activating the form, wich results in error,
because there is no connection string.

The code puts the connection string to a modal declared string (m_connString),
in the class.


SO, I put the code in the class, right below the my base new.....blabla


IS this the right way to do, or is there a wiser method to get data into
a class?
*****************************************************
Here is de coding:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Option Explicit On
Imports System.Data.SqlClient
Imports System.Xml.Serialization
Imports System.IO
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Public Class DataServiceSQL
Private m_connString As String
Private m_con As SqlConnection
Friend WithEvents m_da As New SqlDataAdapter

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Public Sub New()
Dim p As New XML
Dim x As New XmlSerializer(p.GetType)

Dim objStreamReader As New StreamReader("D:\Documents and Settings\Administrator\Mijn documenten\Visual Studio Projects\WindowsApplication49\DMS.xml")
Dim p2 As New XML
p2 = x.Deserialize(objStreamReader)
objStreamReader.Close()

m_connString = p2.strMyConn
m_con = New SqlConnection(m_connString)
End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Public Property GetConnString() As String
Get
Return m_connString
End Get
Set(ByVal Value As String)
m_connString = Value

End Set

****************************************************
Have a day,

reinier:wave:

Craig Gemmill
March 9th, 2004, 11:06 PM
Keep in mind that even though classes have guidelines, there is a good deal of personal preference involved when designing them. So there may be more then one way to do the same thing, and there usually is.

Before I make any suggestions.. is that all of the code for the class?

reinier
March 10th, 2004, 11:27 AM
HI Craig,

Of course we are all free in implementing our prgram codes,
but because this (class programming) is new to me, there
might be a neater more efficiant way of programming.

There is more in the class, the coding I showed was only
for retrieving the connection string from a xml file.

More in this class is for receiving the dataset, receiving
update,delete and insert commands for the dataadapter.
(nothing special)

regards,
reinier
:wave:

Craig Gemmill
March 10th, 2004, 11:54 AM
With out seeing all of the code, it's hard to comment on whether this is a good idea or not. The one thing that I might suggest is to turn it into a shared member class. It appears that you are only going to use this class once, so it would be a good candidate for shared members.

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308371&ID=kb;en-us;Q308371&SD=MSDN

reinier
March 10th, 2004, 12:18 PM
Thanks Craig!

I did look onto it very quikly,
I looks very promising.

Do you know by change, if this is a way
a sending data from a module to a class?

Regards,
reinier:wave:

Craig Gemmill
March 10th, 2004, 12:27 PM
You can add a parameter to the method in the class.

Public Class1

Public Sub DoSomething( sText as String )
Msgbox(sText)
End Sub

End Class


Non-shared way to call that method:

Dim oMyClass as New Class1
oMyClass.DoSomething("Hello")

Shared way to call that method:

Class1.DoSomething("Hello")

reinier
March 10th, 2004, 12:30 PM
T H A N K S

reinier:wave: