473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Accessing 'nested' class properties

I'm realising that there's something important that I don't understand
about acccessing class properties. In fact I'm not even sure that I
can explain clearly what it is that I don't understand! But let me
try:

Let's say that I have a specific parameter value that needs to be
available to several different classes within a program. What I'm
doing currently is to store this specific value in a property of
MyClass1. This class is then instantiated in MyClass2 and then the
property is actually assigned in Form1. So (without showing the
intermediate declarations) I'd have:

Public Class Form1
dim oMyClass2 as New MyClass2
Sub etc
oMyClass2.oMyClass1.MyProperty = MyValue
End class

Hopefully this is OK so far.

But now say I want to access the current value of MyValue from within
a couple of other distinct classes, eg MyClass3 and MyClass4. I guess
the only way of getting at MyValue is to declare a reference to Form1
in both classes MyClass3 and MyClass4.

Public Class MyClass3
dim frm1 as New Form1
Sub etc
x3 = frm1.oMyClass2.oMyClass1.MyValue
End Class

and

Public Class MyClass4
dim frm1 as New Form1
Sub etc
x4 = frm1.oMyClass2.oMyClass1.MyValue
End Class

I'm left with 2 questions:

First, I'm a little uncertain as to whether x3 and x4 will
categorically be assigned to the same value.

Second, is there a better way of handling this? I guess with VB6 I'd
have declared a global variable that didn't need any qualification as
to which its parent class might have been. But I don't seem to have
that option with .Net

Thanks to anyone able to make sense of this.
JGD
Nov 21 '05 #1
1 2496
Yes, you can create global singletons in .NET. Encapsulate your variable
into a class and create a single instance of this class in a module. The
class shown below also throws an exception if you attempt to create more
than one instance of the singleton (I didn't check or compile this code).
Module Module1

Public Class AB

' Your reference.......

Private m_Ref As SomeClass
' True if we already created an instance.

Private Shared m_bCreated As Boolean = False

' Implement constructor to check to see if we already created one.

Public Sub New()

If m_bCreated Then
Throw New Exception("Singleton already created.")
End If

m_bCreated = True
End Sub

' Get/Set the reference

Public Property TheRef() As SomeClass

Get
Return m_Ref
End Get

Set(ByVal Value As SomeClass)
m_Ref = Value
End Set

End Property

End Class

' Declare the global singleton.

Public s_AB As New AB
End Module

"John Dann" <ne**@prodata.co.uk> wrote in message
news:q4********************************@4ax.com...
I'm realising that there's something important that I don't understand
about acccessing class properties. In fact I'm not even sure that I
can explain clearly what it is that I don't understand! But let me
try:

Let's say that I have a specific parameter value that needs to be
available to several different classes within a program. What I'm
doing currently is to store this specific value in a property of
MyClass1. This class is then instantiated in MyClass2 and then the
property is actually assigned in Form1. So (without showing the
intermediate declarations) I'd have:

Public Class Form1
dim oMyClass2 as New MyClass2
Sub etc
oMyClass2.oMyClass1.MyProperty = MyValue
End class

Hopefully this is OK so far.

But now say I want to access the current value of MyValue from within
a couple of other distinct classes, eg MyClass3 and MyClass4. I guess
the only way of getting at MyValue is to declare a reference to Form1
in both classes MyClass3 and MyClass4.

Public Class MyClass3
dim frm1 as New Form1
Sub etc
x3 = frm1.oMyClass2.oMyClass1.MyValue
End Class

and

Public Class MyClass4
dim frm1 as New Form1
Sub etc
x4 = frm1.oMyClass2.oMyClass1.MyValue
End Class

I'm left with 2 questions:

First, I'm a little uncertain as to whether x3 and x4 will
categorically be assigned to the same value.

Second, is there a better way of handling this? I guess with VB6 I'd
have declared a global variable that didn't need any qualification as
to which its parent class might have been. But I don't seem to have
that option with .Net

Thanks to anyone able to make sense of this.
JGD

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: nobody | last post by:
I just tried to do something and realized I'm not sure of the proper (if there *IS* a proper) way to do it. I'm designing a class with a lot a methods/properties. I would like to group some of...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
1
by: Edward Diener | last post by:
I want to create a nested class within my component class. The nested class will have public properties. Naturally I want those properties to be serialized just as those properties of my component...
4
by: Steve Franks | last post by:
I have this cool nested master page scenario working great. However what is the correct way to be able to access a strongly typed property at the top level master from a content page that has a...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
6
by: Marc Hoeijmans | last post by:
How is it possible to us a property from a nested class. Example: namespace MyApp { public class MaxLength { public class Project {
1
by: Diane Yocom | last post by:
I have two nested master pages, each with public properties. From my content page, I am able to access properties from both pages using either Master.PropertyName (for the child master page) or...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.