473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.oMyCl ass1.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.MyVal ue
End Class

and

Public Class MyClass4
dim frm1 as New Form1
Sub etc
x4 = frm1.oMyClass2. oMyClass1.MyVal ue
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 2507
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("Sing leton 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.c o.uk> wrote in message
news:q4******** *************** *********@4ax.c om...
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.oMyCl ass1.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.MyVal ue
End Class

and

Public Class MyClass4
dim frm1 as New Form1
Sub etc
x4 = frm1.oMyClass2. oMyClass1.MyVal ue
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
1484
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 them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands". My first thought was a nested class, but how would the methods in the nested Commands object (easily)...
6
559
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 like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
1
1366
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 are serialized. Should my nested class be: 1) A struct, with my component class holding a value variable for it. 2) A class, with my component class holding a reference variable for it. If the latter, does the nested class need to be derived...
4
2460
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 nested master page between the content page and the top level master? For example, assume the very top level master is called CompanyWide.master. And the nested master page is called DepartmentA.master, which inherits from CompanyWide master. ...
9
3680
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? End Sub
5
2696
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 properties. I want to know if there is a way to access the methods and the properties of the Owner class for the class that's inside it? I.e. I want to find out within Engine what make the Car is which is exposed by the property Car.Model.
6
3102
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
1636
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 Master.Master.PropertyName (for the parent master page). The solution compiles and the HTML looks right, but my code has blue "error" underlines wherever I type Master.Master.PropertyName. When I hover over the underlined code, I get the...
4
7117
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 Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested objects, or will I have to write my own? public class LocationCell
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.