[RESOLVED] Passing value to property
Hi All,
I am trying to pass a value to a public property when I double click a row in a datagrid.
The grid is on a form and my property is in a separate class, here is my double click code:
VB Code:
Private Sub grdCli_DoubleClick(ByVal value As Object, ByVal e As System.EventArgs) Handles grdCli.DoubleClick
value = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
End Sub
and here is the property I am trying to set:
VB Code:
Public Property CliID() As Integer
Get
Return Me._cliid
End Get
Set(ByVal Value As Integer)
Me._cliid = Value
End Set
End Property
The value is being selected fine but I am not sure how to set the property within the class.
any ideas appreciated.
Re: Passing value to property
is your public property a property of the form that your grid is in, or another object?
The way you set a property is to simply set it like any variable, but you must include the variable reference to the class that the property belongs to..
So if your CliID property is a property of the form, you could simply say
Me.CliID = Value Here
if the property is a property of some other class, then you would simply say
InstanceOfClass.CliID = Value Here
Anything in italics should be substituted for a real value.
Re: Passing value to property
Hi,
Value is a parameter passed byval, so you can change its value as you want this will be valid during the execution of the double click event, and inside this method.
If you want to pass a value to a property of and object you will need to asgin it.
using your property definition, you can do on the dobleclick event
VB Code:
dim YourObject as new YourClass 'class in which you define the cliIDproperty
Private Sub grdCli_DoubleClick(ByVal value As Object, ByVal e As System.EventArgs) Handles grdCli.DoubleClick
yourobject.CliID = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
End Sub
Re: Passing value to property
Hi,
thanks for both of your replies. Yes the property is in a different class.
Where I seem to be having the problem is when I set the property like so:
VB Code:
clsClient.CliID = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
I can see the value fine, but within the clsClient class I have a function that returns a value dependant upon the result returned clsClient.CliID
When this function fires it doesnt see the value! Do I need to declare this somehow within the class also.
Sorry, I don't think I explained this too well.
Re: Passing value to property
can you post the code for the function you say doesn't see the value?
Re: Passing value to property
Yes, here it is. It is actually a query that I am using to poulate a datatable based on the client id.
VB Code:
Public Function getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
end function
when I have done this in the past working with shared variables it has worked fine but I am just getting into using properties so apologies if I am not very clear on some things.
Re: Passing value to property
So I imagine your class setup is something like this
VB Code:
Private _CliID As Integer = 0
Public Function getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & _CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
End Function
Public Property CliID() As Integer
Get
Return Me._cliid
End Get
Set(ByVal Value As Integer)
Me._cliid = Value
End Set
End Property
This should work fine so long as you set the actual value of CliID BEFORE calling the getexistingdata function.
Re: Passing value to property
You need to reference the instance of your class..
VB Code:
Public Function getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & [I]yourinstance.[/I]CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
end function
Substitute the italic for the real instance of your class (probably clsClient)
Re: Passing value to property
Quote:
Originally Posted by josep
You need to reference the instance of your class..
VB Code:
Public Function getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & [I]yourinstance.[/I]CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
end function
Substitute the italic for the real instance of your class (probably clsClient)
I was assuming the function and property were all in the same class...
Re: Passing value to property
Kleinma
it is not inside the class, it shouldwork as it was inside the class (calling to the property, that finally will return the private variable inside the class); I do not recomend but it will work.
I thought this function is out written out of the class.
Re: Passing value to property
well that function is technically not even sytactically correct since there is no return type, and it doesn't even return a value. Its basically a Sub Routine... (he must not have option strict on)
I guess we need to hear from the poster to hear more about the actual structure of this code... otherwise we are just making educated guesses...
Re: Passing value to property
Quote:
Originally Posted by kleinma
I guess we need to hear from the poster to hear more about the actual structure of this code... otherwise we are just making educated guesses...
I agree... If the poster doens't care to check on the status of his own thread and update the info then we might just be wasting our time trying to solve something that wasn't the problem in the first place.
Re: Passing value to property
stanav, I am at work and I do need to do other things as well as request help on the forum. I appreciate all help offered here and I have never been put on a time limit before to respond.
Yes kleinma, it is all in the same class and it looks exactly as you have laid it out. And you are correct, it did actually start out as a function but does need to be changed to a sub.
What I have not done however is to set the value as you suggested here:
Private _CliID As Integer = 0
I am now at home but will try this as soon as I get into the office tomorrow. Many thanks for everyones suggestions.
Re: Passing value to property
well you must have declared _CliID somewhere in your class right? because you used it in this code you showed
VB Code:
Public Property CliID() As Integer
Get
Return Me._cliid
End Get
Set(ByVal Value As Integer)
Me._cliid = Value
End Set
End Property
I set it to 0 for completeness and efficiency, but if you don't set it to 0, 0 is its default value anyway...
Re: Passing value to property
Hi all,
I have been going through this again and the value is being set correctly within the form with the datagrid.
When I double click the datagrid I have added a msgbox to show that the value is being catptured. This is working fine and returns the value as '3' which it should.
VB Code:
dim clsClient as new clsClient
...
clsClient.CliID = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
msgbox(clsClient.CliID) 'returns 3
now when I add a message box to the following, it returns 0 as though it has lost the value.
VB Code:
Private _CliID As Integer
Public Sub getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
Dim clsClient as new clsClient
MsgBox(clsClient.CliID) ' returns '0'
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & clsClient.CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
End Sub
Public Property CliID() As Integer
Get
Return Me._cliid
End Get
Set(ByVal Value As Integer)
Me._cliid = Value
End Set
End Property
If I change this: intClientID = '" & clsClient.CliID & "' to this:intClientID = 3 it all runs fine. So I am definitely losing the value somewhere between doubleclicking and setting the value to running the sub within the client class.
any ideas appreciated
Re: Passing value to property
you shouldn't be making a new class....
VB Code:
Dim clsClient as new clsClient
MsgBox(clsClient.CliID) ' returns '0'
it makes perfect sense that its returning 0 because you are using a brand new instance of the class and the value has not been set yet...
if you did
VB Code:
Dim clsClient as new clsClient
clsClient.CliID = 50
MsgBox(clsClient.CliID) ' returns '50'
it wouldn't be 0, but this is all in a single class, so you don't need to make a new instance.. just use the instance you are in.
Try something like this...
VB Code:
Private _CliID As Integer
Public Sub getexistingdata()
Try
'load data for current selection
Dim da As New SqlClient.SqlDataAdapter
MsgBox(_CliID)
SqlServerCon.Open()
da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
"intClientID = '" & _CliID & "'", SqlServerCon)
da.Fill(dt)
SqlServerCon.Close()
Catch ex As SqlClient.SqlException
MsgBox(ex.Message, MsgBoxStyle.OKOnly)
Finally
If SqlServerCon.State = ConnectionState.Open Then
SqlServerCon.Close()
End If
End Try
End Sub
Public Property CliID() As Integer
Get
Return Me._cliid
End Get
Set(ByVal Value As Integer)
Me._cliid = Value
End Set
End Property
Re: [RESOLVED] Passing value to property
That's great, it's working now.
thanks for your patience.