Results 1 to 17 of 17

Thread: [RESOLVED] Passing value to property

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    [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:
    1. Private Sub grdCli_DoubleClick(ByVal value As Object, ByVal e As System.EventArgs) Handles grdCli.DoubleClick
    2.         value = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
    3.     End Sub

    and here is the property I am trying to set:

    VB Code:
    1. Public Property CliID() As Integer
    2.         Get
    3.             Return Me._cliid
    4.         End Get
    5.         Set(ByVal Value As Integer)
    6.             Me._cliid = Value
    7.         End Set
    8.     End Property

    The value is being selected fine but I am not sure how to set the property within the class.

    any ideas appreciated.
    Last edited by CodeKiller; Nov 9th, 2006 at 09:38 AM. Reason: Resolved
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  3. #3
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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:
    1. dim  YourObject as new YourClass  'class in which you define the cliIDproperty
    2.  
    3. Private Sub grdCli_DoubleClick(ByVal value As Object, ByVal e As System.EventArgs) Handles grdCli.DoubleClick
    4.         yourobject.CliID = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
    5.     End Sub
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    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:
    1. 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.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Passing value to property

    can you post the code for the function you say doesn't see the value?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    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:
    1. Public Function getexistingdata()
    2.         Try
    3.             'load data for current selection
    4.             Dim da As New SqlClient.SqlDataAdapter
    5.  
    6.             SqlServerCon.Open()
    7.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    8.             "intClientID = '" & CliID & "'", SqlServerCon)
    9.             da.Fill(dt)
    10.  
    11.             SqlServerCon.Close()
    12.  
    13.         Catch ex As SqlClient.SqlException
    14.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    15.         Finally
    16.             If SqlServerCon.State = ConnectionState.Open Then
    17.                 SqlServerCon.Close()
    18.             End If
    19.         End Try
    20. 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.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Passing value to property

    So I imagine your class setup is something like this

    VB Code:
    1. Private _CliID As Integer = 0
    2.  
    3.     Public Function getexistingdata()
    4.         Try
    5.             'load data for current selection
    6.             Dim da As New SqlClient.SqlDataAdapter
    7.  
    8.             SqlServerCon.Open()
    9.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    10.             "intClientID = '" & _CliID & "'", SqlServerCon)
    11.             da.Fill(dt)
    12.  
    13.             SqlServerCon.Close()
    14.  
    15.         Catch ex As SqlClient.SqlException
    16.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    17.         Finally
    18.             If SqlServerCon.State = ConnectionState.Open Then
    19.                 SqlServerCon.Close()
    20.             End If
    21.         End Try
    22.     End Function
    23.  
    24.  
    25.     Public Property CliID() As Integer
    26.         Get
    27.             Return Me._cliid
    28.         End Get
    29.         Set(ByVal Value As Integer)
    30.             Me._cliid = Value
    31.         End Set
    32.     End Property

    This should work fine so long as you set the actual value of CliID BEFORE calling the getexistingdata function.

  8. #8
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    Re: Passing value to property

    You need to reference the instance of your class..

    VB Code:
    1. Public Function getexistingdata()
    2.         Try
    3.             'load data for current selection
    4.             Dim da As New SqlClient.SqlDataAdapter
    5.  
    6.             SqlServerCon.Open()
    7.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    8.             "intClientID = '" & [I]yourinstance.[/I]CliID & "'", SqlServerCon)
    9.             da.Fill(dt)
    10.  
    11.             SqlServerCon.Close()
    12.  
    13.         Catch ex As SqlClient.SqlException
    14.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    15.         Finally
    16.             If SqlServerCon.State = ConnectionState.Open Then
    17.                 SqlServerCon.Close()
    18.             End If
    19.         End Try
    20. end function

    Substitute the italic for the real instance of your class (probably clsClient)
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Passing value to property

    Quote Originally Posted by josep
    You need to reference the instance of your class..

    VB Code:
    1. Public Function getexistingdata()
    2.         Try
    3.             'load data for current selection
    4.             Dim da As New SqlClient.SqlDataAdapter
    5.  
    6.             SqlServerCon.Open()
    7.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    8.             "intClientID = '" & [I]yourinstance.[/I]CliID & "'", SqlServerCon)
    9.             da.Fill(dt)
    10.  
    11.             SqlServerCon.Close()
    12.  
    13.         Catch ex As SqlClient.SqlException
    14.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    15.         Finally
    16.             If SqlServerCon.State = ConnectionState.Open Then
    17.                 SqlServerCon.Close()
    18.             End If
    19.         End Try
    20. 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...

  10. #10
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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.
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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...

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    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.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. Public Property CliID() As Integer
    2.         Get
    3.             Return Me._cliid
    4.         End Get
    5.         Set(ByVal Value As Integer)
    6.             Me._cliid = Value
    7.         End Set
    8.     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...

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    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:
    1. dim clsClient as new clsClient
    2. ...
    3.  
    4. clsClient.CliID = grdCli.Item(grdCli.CurrentCell.RowNumber, grdCli.CurrentCell.ColumnNumber)
    5. 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:
    1. Private _CliID As Integer
    2.  
    3.     Public Sub getexistingdata()
    4.         Try
    5.             'load data for current selection
    6.             Dim da As New SqlClient.SqlDataAdapter
    7.             Dim clsClient as new clsClient
    8.  
    9.             MsgBox(clsClient.CliID) ' returns '0'
    10.  
    11.             SqlServerCon.Open()
    12.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    13.             "intClientID = '" & clsClient.CliID & "'", SqlServerCon)
    14.             da.Fill(dt)
    15.  
    16.             SqlServerCon.Close()
    17.  
    18.         Catch ex As SqlClient.SqlException
    19.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    20.         Finally
    21.             If SqlServerCon.State = ConnectionState.Open Then
    22.                 SqlServerCon.Close()
    23.             End If
    24.         End Try
    25.     End Sub
    26.  
    27.  
    28.     Public Property CliID() As Integer
    29.         Get
    30.             Return Me._cliid
    31.         End Get
    32.         Set(ByVal Value As Integer)
    33.             Me._cliid = Value
    34.         End Set
    35.     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
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Passing value to property

    you shouldn't be making a new class....

    VB Code:
    1. Dim clsClient as new clsClient
    2.  
    3.             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:
    1. Dim clsClient as new clsClient
    2.     clsClient.CliID = 50
    3.     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:
    1. Private _CliID As Integer
    2.  
    3.     Public Sub getexistingdata()
    4.         Try
    5.             'load data for current selection
    6.             Dim da As New SqlClient.SqlDataAdapter
    7.  
    8.             MsgBox(_CliID)
    9.  
    10.             SqlServerCon.Open()
    11.             da = New SqlClient.SqlDataAdapter("SELECT * FROM tblClient WHERE " & _
    12.             "intClientID = '" & _CliID & "'", SqlServerCon)
    13.             da.Fill(dt)
    14.  
    15.             SqlServerCon.Close()
    16.  
    17.         Catch ex As SqlClient.SqlException
    18.             MsgBox(ex.Message, MsgBoxStyle.OKOnly)
    19.         Finally
    20.             If SqlServerCon.State = ConnectionState.Open Then
    21.                 SqlServerCon.Close()
    22.             End If
    23.         End Try
    24.     End Sub
    25.  
    26.  
    27.     Public Property CliID() As Integer
    28.         Get
    29.             Return Me._cliid
    30.         End Get
    31.         Set(ByVal Value As Integer)
    32.             Me._cliid = Value
    33.         End Set
    34.     End Property

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    135

    Re: [RESOLVED] Passing value to property

    That's great, it's working now.

    thanks for your patience.
    ---------------------------------------------------
    VB 2003
    SQL Server 2000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width