|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Reference to a non-shared member requires an object referenceI'm creating small aspnet application. The main purpose of application is to manage list of contracts (adding, updating, deleting) and creating reports based on database of contracts. So I've created a class Contract, which have all contract's properties and method like InsertContract etc. In my InsertContract method I want to pass Contract object to my database provider class Insert method. I'm receiving an error "Reference to a non-shared member requires an object reference". It's understandable, because I didn't instantiate database provider object before. I can workaround this error using "shared" keyword in my database provider method definition. My question is, if it's correct solution and if not, what shall I do. Here is my code: Public Class Contract Public Property Trademark() As String Get Return _trademark End Get Set(ByVal value As String) _trademark = value End Set End Property ' other properties Public Sub New() End Sub 'other methods Public Sub InsertContract () DatabaseProvider.InsertContract (contract) End Sub End Class Public Class DatabaseProvider Public Shared Sub InsertContract (record as Contract) 'adding contract to database End Class Przemek
Show quote
Hide quote
> Several things here, mainly design issues. I would make "InsertContract" a > I'm creating small aspnet application. The main purpose of application > is to manage list of contracts (adding, updating, deleting) and > creating reports based on database of contracts. So I've created a > class Contract, which have all contract's properties and method like > InsertContract etc. In my InsertContract method I want to pass Contract > object to my database provider class Insert method. I'm receiving an > error "Reference to a non-shared member requires an object reference". > It's understandable, because I didn't instantiate database provider > object before. I can workaround this error using "shared" keyword in my > database provider method definition. My question is, if it's correct > solution and if not, what shall I do. Here is my code: > > Public Class Contract > > Public Property Trademark() As String > Get > Return _trademark > End Get > Set(ByVal value As String) > _trademark = value > End Set > End Property > > ' other properties > method on your Database provider. So you pass in a Contract object to the provider, rather than try to make the contract itself "know" about the provider ( ie. "myProvider.InsertContract ( myContract ). If you want to do the latter, you need to store an reference to the provider in the contract, which I'm sure you will agree is not quite so easy to do. You could of course make your provider a global singleton, and then reference it with your contract object, but I don't think that would be a very good idea.
Forcing Checkbox to Return integer instead of boolean
Excel automation Determining Sql Column name? Web.config issue System.Web.Mail problem different language windows Create a Folder - Newbie Question Sorting a DataTable in a DataSet TreeNode with BOLD font get truncated SendKeys.SendWait + Vista = SecurityException |
|||||||||||||||||||||||