none
Referencing a function in a class from within another class in the same namespace RRS feed

  • Question

  • i have a class say Namespace 123 Public Class abc Public Function abc1() as sqlclient.sqlconnection return connection information end function End Class Public Class xyz public function Function1() as boolean myConnection= abc1<------------------ here lies the problem end function End Class End Namespace ''FORM CODE Dim myClass as new Class1.xyz Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myclass.Function1 End Sub how can i access function abc1 in Class abc from class xyz. as i want to connect to the database from one class and add reference to my form of the other class only. that class may internally access the connection information and get the data from SQL Server. How to accomplish this?
    Tuesday, June 1, 2010 4:42 AM

Answers

  • The xyz class needs to expose the connection as a public property (I'm not 100% sure of VB's property syntax, as I use C# ... I used a converter for this code, so forgive me if it's in error):

    Public Class xyz 
    	Public Property myConnection As SqlConnection
    		End Property
    
    	public function Function1() as boolean 
    		myConnection= abc1
    	end function 
    End Class 
    
    ''FORM CODE 
    Dim myClass as new Class1.xyz 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    	myclass.Function1 
    	SqlConnection oConn = myclass.myConnection
    End Sub 

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by liurong luo Monday, June 7, 2010 6:19 AM
    • Marked as answer by NET HAWK Monday, June 7, 2010 6:34 AM
    Saturday, June 5, 2010 4:13 PM