Class Inheritance

vellaima

Centurion
Joined
Jan 29, 2003
Messages
109
Hello,

Visual Basic:
Public Class salesrepadd
    Inherits System.Windows.Forms.Form

   Private Sub Add() 
        address.text = "Trial"
        Dim ins As New InsertSalesRep()
        ins.insert() 
   End Sub

End Class

Public Class InsertSalesRep
    Inherits salesrepadd

  Public Sub insert()
        MsgBox(address.Text)   ------> Displays blank  
  End Sub
End Class

In the above program eventhough i have inherited the window form i am not able to retreive the values of the textbox etc.,.

Could you please tell me where have i gone wrong.
 
Did you just leave out the constructor for simplification or is there no constructor where you explicitly call the parent forms constructor as in MyBase.New()?

Edit: ok so maybe I was on the wrong path....:(
 
They are totally separate classes. Your second class has no knowledge of the first one's textbox called "address".

To rectify this, you could pass the textbox to the Insert routine in the second class, or associate the second class with the first when you create it, in the constructor.
 
Can you please tell me how to associate the second class with the first in the constructor as i am new VB.NET.
 
he's inheriting the first in the second, so he does have access to it without passing references. are these hand-coded inherited forms or are you using visual studio?
 
in the constructor you just need to call the base classes new ie. MyBase.New() but Visual Studio.NET takes care of this setup stuff for you if you're using it.
 
The problem i am having is i am not able to get the values of the textboxes in the class InsertSalesRep. Can anyone please help me solve this problem. Sample code will be helpful.
 
Trimmed a bit (hopefully I didn't snip any of the important stuff) but....
Visual Basic:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    
    Private components As System.ComponentModel.IContainer
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox1.Location = New System.Drawing.Point(80, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Text = "TextBox1"
        Me.ClientSize = New System.Drawing.Size(624, 502)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
    End Sub
End Class

Public Class Form2
    Inherits Form1

    Public Sub New()
        MyBase.New()

        InitializeComponent()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox(Me.TextBox1.Text)
    End Sub
End Class
 
I think I see Vidya's problem :

Visual Basic:
..
Dim ins As New InsertSalesRep()
..

The above line creates a new instance of InsertSalesRep, meaning a new instance of the textbox 'address'. The value will never be the same as that of the 'address' text field on the base form (another instance on its own)

I would use divil's suggestion but will pass a text value only, e.g.

Visual Basic:
'insert function in your derived form
Public Sub insert(value as String)
        MsgBox(value)   ------> should now displays the value  
End Sub

Hope it helps

PS I just read the tread again and I guess thats what you meant in your reply divil...
 
Last edited:
Back
Top