can't transfer value from class to form

D

dennist

can't transfer value from class to form

I have a very simple test application I wrote to learn
how to transfer a value from a class to the form that
called it. I have no problem setting the value in the
class. I have no problem updating the database. But
this isn't really an adonet question, it's a windows form
question.

If I use a public property(code follows), the message
boxes show the right numbers for the ID field, but the
textbox is filled with 0. If I try to use a public
shared property, mID is underlined with the
message 'cannot refer to an instance member of a class
from within a shared method or shared member initializer
without an explicit instance of the class'.

I don't know what to do with that statement. I'm also
afraid it'll carry me far afield without putting the
right number in the textbox. Can somebody tell me how to
get the right number in the textbox from a class. It has
to be from a class, because in the real application the
whole updating of the table occurs in a class.


Imports System.Data
Imports System.Data.OleDb


Public Class Form1
Inherits System.Windows.Forms.Form
Dim strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point
(176, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click

'Dim strConn as string
Dim strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)

Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")



Dim dsA As New ds1

da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "cxpQW"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
'rowDateType.ID = 11
'rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)
'da.InsertCommand = New OleDb.OleDbCommand
("INSERT INTO datetype
(ID,DateType,CreateDate,ChangeDate,Active) values
(?,?,?,?,?)", cn)
da.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO datetype(DateType,CreateDate,ChangeDate,Active)
values (?,?,?,?)", cn)
'INSERT INTO [Order Details] (OrderID, ProductID,
Quantity, UnitPrice) VALUES (?, ?, ?, ?)

'Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
AddHandler da.RowUpdated, AddressOf OnRowUpDated


'Try
' da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer, 4, "ID")
'Catch er As Exception
' MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'End Try

da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, 8, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, 8, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, 2, "Active")

Try
da.Update(dsA, "DateType")
Catch er As System.Exception
'MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'Finally
' cn.Close()
End Try

Dim dr As DataRow
Dim iRow As Integer

iRow = dsA.Tables(0).Rows.Count - 1
dr = dsA.Tables(0).Rows(iRow)
'dr("ID").

'You now have access to all fields in the last
row through the DataRow object (dr)

cn.Close()
Dim cls1 As New Class1
TextBox1.Text = cls1.gID


End Sub
Friend Sub OnRowUpDated(ByVal sender As Object, ByVal
args As OleDb.OleDbRowUpdatedEventArgs)

'include a variable and a command to retrieve the
'identity value from the access database
'Dim strConn, strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
'cn.Open()


Dim int1 As Integer = 0
Dim cmd1 As OleDb.OleDbCommand = New
OleDb.OleDbCommand("SELECT @@IDENTITY", cn)

If args.StatementType = StatementType.Insert Then
'Retrieve the identity value and store it in
the ID column
int1 = CInt(cmd1.ExecuteScalar)
MsgBox(int1)
Dim cls1 As New Class1
cls1.gID = int1
cls1.msg()
'MsgBox(int1)
End If

End Sub



Friend Sub HandleRowUpdated(ByVal sender As Object, _
ByVal e As
OleDbRowUpdatedEventArgs)
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
cn.Open()

Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("ID") = CType
(cmdGetIdentity.ExecuteScalar, Integer)
e.Row.AcceptChanges()
End If

End Sub

End Class


Public Class Class1
Private mID As Integer

Public Property gID()
Get
Return mID
End Get
Set(ByVal Value)
mID = Value
End Set
End Property


Sub msg()
MsgBox(gID.ToString)
End Sub

End Class

naturally, this statement must be changed each time:

rowDateType.DateType = "cxpQW"

dennist
 
Y

Ying-Shen Yu[MSFT]

Hi Dennis,

Thanks for your post!

Setting both the mID and the gID to Shared will solve the compile problem.
however I guess you want to update the textbox value when you updated the
gID, right?
In this case, you can take advantage of the .NET databinding feature,
however you may not use the Shared properties and variables, because .NET
databinding only support bind to a property.
Here is an way to work around this, you may declare the variable and
property as usual(not shared)
and in your Form define a Shared object for your class and bind it with the
textbox, then you can update the value in RowUpdated event and be sure to
call SuspendBinding and ResumeBinding to tell the textbox
to update the value. Here are some code snippets:
<code>
Public Class Form1
Inherits System.Windows.Forms.Form
Shared glob As New Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
TextBox1.DataBindings.Add("Text", glob, "gID")
End Sub
Private Sub btnBuild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles btnBuild.Click
glob.gID = 333
BindingContext(glob, "gID").SuspendBinding()
BindingContext(glob, "gID").ResumeBinding()
End Sub
End Class
</code>

If my work around doesn't solve your problem, please be free to repy this
thread to let me know.
Thanks!





Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
D

dennist

Thank you muchly for your workaround. In the end, I
found a way that's worked consistently that seems a bit
easier. Here it is in my regular app.

In my class DataChor, I dim int1 in a regular way

Public Class clsDataChor
Dim cn As New OleDbConnection(gstrConn)
Public Shared sTopicTitle As String
Public Shared sText As String
Dim int1 As Integer

Then I have a procedure in the class

Public Sub TransferID(ByRef txt As TextBox)

txt.Text = int1.ToString

End Sub

In another procedure in the class I determine the value
of int1

If args.StatementType = StatementType.Insert Then
'Retrieve the identity value and store it in
the ID column
int1 = CInt(cmd1.ExecuteScalar)

In the calling form I do the following:

clsDChor.NewTopic(Me) 'call the NewTopic
with only a single parameter Me. Because Me is a valid
reference to the current windows form.
clsDChor.TransferID(txtID)

Thanks a lot for the help, and also for the help in the
thread, passing forms as paramters.

dennist
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top