Creating Classes in VB.NET 2.0


G

Guest

I am having some trouble understanding scope when creating my component.

Basically I have a primary class entitled permissionRights. This class
creates several public properties of instances of other classes. Each of the
other classes are not child classes of permisions rights. Which is to say

public class PermissionRights
private pvPartition as new Partition
private pvDatabaseConnection as string
public readonly property Partition() as Partition
.... return pvPartition
end property
public readonly property DatabaseConnection() as string
...return pvDatabaseConnection
end property
end class

public class Partition
sub new()
dim test as new string=pvDatabaseConnection
end sub
end class

In the above code the class partition needs to access the value stored in
databaseconnection string. Put it is not aware of it. Now I know I can pass
it on the sub new byref but do I need to. Both classes are in the same
project/assembly. I should be able to access the
PermissionRights.DatabaseConnection public property as a min for the current
instance.

So do I make the Partition class a sub class of permission rights. When I
do that the code analyzer says I should make it private yet it does need to
be public?

Please help, just lost in scope at the moment :)

Cheers
Keith
 
Ad

Advertisements

J

Jack Jackson

I am having some trouble understanding scope when creating my component.

Basically I have a primary class entitled permissionRights. This class
creates several public properties of instances of other classes. Each of the
other classes are not child classes of permisions rights. Which is to say

public class PermissionRights
private pvPartition as new Partition
private pvDatabaseConnection as string
public readonly property Partition() as Partition
.... return pvPartition
end property
public readonly property DatabaseConnection() as string
...return pvDatabaseConnection
end property
end class

public class Partition
sub new()
dim test as new string=pvDatabaseConnection
end sub
end class

In the above code the class partition needs to access the value stored in
databaseconnection string. Put it is not aware of it. Now I know I can pass
it on the sub new byref but do I need to.

Yes you do. The instance of Partition that is created in
PermissionRights has no idea that you stored a reference to it in some
PermissionRights instance. .NET has no concept of instances belonging
to or being children of another instance.
Both classes are in the same
project/assembly. I should be able to access the
PermissionRights.DatabaseConnection public property as a min for the current
instance.

No. .NET doesn't work that way. There is no concept of implicit
instance references or 'current instance'. If you want to access the
DatabaseConnection property of some instance, you need to supply a
reference to the instance.
 
Ad

Advertisements

G

Guest

That is what I was beginning to think. That is the way it has allways been
in VB but I thought that perhaps .NET provided that scope somehow, but I
guess not.

So still back to the good old days of passing a ref in on creation of the
class.

Thanks for the clarrification...
Cheers
Keith Chadwick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top