|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
property access from the formI have a form in a windows application. In that form I have declared the public property InvoiceNo. I am trying to access this property from the other form. In the other form I declared the first form as public frmInvoice as InvoiceForm after the declaration I have on load event in which I am trying to access the property Private Sub ReportViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load dim inv as string inv = frmInvoice.InvoiceNo End Sub I get the error of the Object reference not set to an instance of an object. Now I can fix it by declaring public frmInvoice as new InvoiceForm but then I get frmInvoice.InvoiceNo as NOTHING Please help Hi,
Try to conceptualise what you are trying to do, first. A form is just an instance of a class, so we shouldn't treat it as if it were something else. Let us look at what you were trying to do, in order to understand the problem : (Assume that your ReportViewer Form is Class 2 and frmInvoice is Class 1) 1. Trying to access Class 1 from Class 2. Problem : Class 1 has not been instantiated. No object has been created. Error : Null Reference exception. Solution : Instantiate Class1. You did this by the statement "public frmInvoice as new InvoiceForm()" 2. Trying to access the property "InvoiceNo" of Class 1 from Class 2. Problem : Class 1 has been instantiated, but the property contains Nothing. In short, you are trying to access a property that has not been set. Solution : Set a default value for the property in Class 1 code. This solves the problem, but I don't think it is what you are trying to do. Instead, I believe you already have Class 1 (the InvoiceForm) running, and you want to access it's InvoiceNo property from Class 2 (the ReportViewer form). This means that we must not initialize InvoiceForm again. We must somehow get a reference to the already running InvoiceForm. So, the question boils down to : "How to transfer the value of property InvoiceNo in frmInvoice to ReportViewer". (Your original question was "How to access the property InvoiceNo in frmInvoice from ReportViewer). Can you see the difference ? You were thinking in the opposite direction. This can be solved simply by : (Assuming ReportViewer is loaded from frmInvoice) --------------------------------------------------- In frmInvoice: --------------- Public Property InvoiceNo as Integer : 'This procedure loads ReportViewer...: End Property Private Sub LoadReport() dim frm2 as New ReportViewer () frm2.myInvoiceNo = Me.InvoiceNo frm2.show() End Sub In ReportViewer: ------------------ Friend myInvoiceNo as Integer --------------------------------------------------- Hope this helps, Regards, Cerebrus. Thanks Cerebrus. It is the best explaination ever.
Vivek Show quoteHide quote "Cerebrus" <zorg***@sify.com> wrote in message news:1144819395.072724.109300@g10g2000cwb.googlegroups.com... > Hi, > > Try to conceptualise what you are trying to do, first. > > A form is just an instance of a class, so we shouldn't treat it as if > it were something else. > > Let us look at what you were trying to do, in order to understand the > problem : > (Assume that your ReportViewer Form is Class 2 and frmInvoice is Class > 1) > > 1. Trying to access Class 1 from Class 2. > Problem : Class 1 has not been instantiated. No object has been > created. > Error : Null Reference exception. > Solution : Instantiate Class1. You did this by the statement "public > frmInvoice as new InvoiceForm()" > > 2. Trying to access the property "InvoiceNo" of Class 1 from Class 2. > Problem : Class 1 has been instantiated, but the property contains > Nothing. In short, you > are trying to access a property that has not been set. > Solution : Set a default value for the property in Class 1 code. > > This solves the problem, but I don't think it is what you are trying to > do. Instead, I > believe you already have Class 1 (the InvoiceForm) running, and you > want to access it's > InvoiceNo property from Class 2 (the ReportViewer form). > > This means that we must not initialize InvoiceForm again. We must > somehow get a reference to the already running InvoiceForm. > > So, the question boils down to : "How to transfer the value of property > InvoiceNo in frmInvoice to ReportViewer". (Your original question was > "How to access the property InvoiceNo in frmInvoice from ReportViewer). > Can you see the difference ? You were thinking in the opposite > direction. > > This can be solved simply by : (Assuming ReportViewer is loaded from > frmInvoice) > --------------------------------------------------- > In frmInvoice: > --------------- > Public Property InvoiceNo as Integer > : > : > End Property > > 'This procedure loads ReportViewer... > Private Sub LoadReport() > dim frm2 as New ReportViewer () > frm2.myInvoiceNo = Me.InvoiceNo > frm2.show() > End Sub > > > In ReportViewer: > ------------------ > Friend myInvoiceNo as Integer > --------------------------------------------------- > Hope this helps, > > Regards, > > Cerebrus. > Vivek,
What you have done with "public frmInvoice as InvoiceForm" Is creating a public placeholder for the adres of a to create object frmInvoice To instance it (to create a New object) you have to use the New word. That can inside that declaration \\\ public frimInfoice as New InvoiceForm /// or otherwise as first statement in your load event. \\\ frmInvoice = New InvoiceForm /// Reading the message from Cerebrus again, I see that he writes in a way the same, so see this as addition with some samples. I hope this helps, Cor Cor Show quoteHide quote "Vivek Sharma" <vivekn***@gmail.com> schreef in bericht news:u7UgdVeXGHA.1564@TK2MSFTNGP03.phx.gbl... > Hi, > > I have a form in a windows application. In that form I have declared the > public property InvoiceNo. I am trying to access this property from the > other form. In the other form I declared the first form as > > public frmInvoice as InvoiceForm > > after the declaration I have on load event in which I am trying to access > the property > > > Private Sub ReportViewer_Load(ByVal sender As Object, ByVal e As > System.EventArgs) Handles MyBase.Load > > dim inv as string > > inv = frmInvoice.InvoiceNo > End Sub > > > > I get the error of the Object reference not set to an instance of an > object. > Now I can fix it by declaring > > public frmInvoice as new InvoiceForm > > but then I get frmInvoice.InvoiceNo as NOTHING > > Please help > > >
Check if TIF file in use
Request for permission of the type 'System.Security.Permissions.RegistryPermission...' failed ASP.NET page, hiding certain controls when printing Tree view node selection How to draw graphics on a bitmap ? Tool strip size Add User settings to My.Settings at Runtime Shared Methods In A Class ListView FocusedItem file system watcher prevents PC shutdown |
|||||||||||||||||||||||