Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

While I am working with Friend members in VB.net I got an issue.

As per my knowledge a friend member can be accessed inside the entire assembly.

Based on this, I created 2 main classess and 2 derived classes. Derived1 is dervied from Base1 and Derived2 is derived from Base2. I used a Friend member in Base1 and trying to use that in Derived2.

VB.net is giving an error stating that "Name B not declared"

Why I am unable to access fields B in different class' object?

Is my understanding on FRIENDS is incorrect?

Please help.
VB
Public Class Form1
    Public Class Base1
        Protected A As Integer
        Friend B As Integer
    End Class
    Public Class Derived1
        Inherits Base1
        Public Function show() As String
            Return A + B
        End Function
    End Class
    Public Class Base2
        Protected C As Integer
    End Class
    Public Class Derived2
        Inherits Base2
        Public Function show() As String
            Return C + b
        End Function
    End Class
 
End Class
Posted
Updated 3-Jan-12 0:02am
v2
Comments
Karthik Harve 3-Jan-12 6:02am    
[Edit] pre tags added.

There is no B member variable in Base2, nor in Derived2.
 
Share this answer
 
Comments
Nara Har 3-Jan-12 6:07am    
But, I declared B as friend in Base1. Won't it be accessible in Base2? If not, how can we use a friend member. Please explain.
phil.o 3-Jan-12 9:45am    
Base1 and Base2, according to the piece of code you gave, are not related at all.
Maybe you forgot to make Base2 inherit from Base1 ?
I'm assuming you're getting error in Derived2.Show(). That would be because There is no instance of Base1 or Derived1 declared in the scope of Derived2.Show(). Friend makes the member visible, not Shared (Static in C#).

In order for the code to work you either have to make Base1.B Shared or declare an instance of Base1 or Derived1 inside the Derived2 class or it's Show method.
 
Share this answer
 
Because class Base2 does not contain a member "B".
It isn't derived from any other class, so the statement
VB
Return C + b
Does not know about any object called "B" to use.

Perhaps, if you derived the class Base2 from Base1 or Derived1 it would work as you want?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900