473,668 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding redundant storage. Share keyword?

In a VB.Net 2.0 project, I have a WizardClass that contains a List (of
RuleClass). Each rule needs access to variables within it's parent
wizard, so the constructor for RuleClass is New (ByVal Wizard as
WizardClass). Each rule then has a property Private Wiz as WizardClass,
which is set in Sub New with Wiz = Wizard. Currently there is only one
Wizard in the project but there might be more in the future to handle
different rulesets.

My understanding is that, with this, each instance of RuleClass has its
own isolated and independent copy of WizardClass, which would take up a
huge amount of memory if there are a lot of rules. This is part of a
service, so the memory would be permanently used. Is this actually the
case? If so, any suggestions on how to prevent this memory use?

I am thinking that I can declare each instance of WizardClass to be
Shared, so in the service module:

Private Shared Wizzie1 as WizardClass
Private Shared Wizzie2 as WizardClass

would create independent instances of WizardClass and each time Wizzie1
or Wizzie2 appear on the right of an = a pointer to the instance would
be generated rather than a copy. So in the LoadRuleset method of
WizardClass

mRulesList.Add( New RuleClass(Me))

would create only a pointer to the shared parent wizard rather than a
complete copy of the wizard object. Will this do what I think it will?
Will this happen automatically when the project is compiled and
(hopefully) optimized? Or am I barking up the wrong tree entirely?

Thanks in advance for any help.
--
Gregory Gadow
te******@serv.n et
http://www.serv.net/~techbear

"[W]e have never held that moral disapproval, without any other asserted

state interest, is a sufficient rationale under the Equal Protection
Clause to justify a law that discriminates among groups of persons."
- Sandra Day O`Conner, _Lawrence v Texas_
http://caselaw.lp.findlaw.com/script...0&invol=02-102

Dec 6 '05 #1
6 1060
"Gregory Gadow" <te******@serv. net> schrieb:
In a VB.Net 2.0 project, I have a WizardClass that contains a List (of
RuleClass). Each rule needs access to variables within it's parent
wizard, so the constructor for RuleClass is New (ByVal Wizard as
WizardClass). Each rule then has a property Private Wiz as WizardClass,
which is set in Sub New with Wiz = Wizard. Currently there is only one
Wizard in the project but there might be more in the future to handle
different rulesets.

My understanding is that, with this, each instance of RuleClass has its
own isolated and independent copy of WizardClass
If you are passing the reference to an existing 'WizardClass' object to the
new object and store the reference to this object there, only an additional
reference is created. Both instances will reference the same 'WizardClass'
object afterwards.
, which would take up a
huge amount of memory if there are a lot of rules. This is part of a
service, so the memory would be permanently used. Is this actually the
case? If so, any suggestions on how to prevent this memory use?
No, it isn't the case if 'WizardClass' is a reference type, e.g. a class.
I am thinking that I can declare each instance of WizardClass to be
Shared, so in the service module:

Private Shared Wizzie1 as WizardClass
Private Shared Wizzie2 as WizardClass

would create independent instances of WizardClass


The code above doesn't create instance of /classes/ at all. It only
declares variables which reference 'Nothing' by default. If you assign the
same reference to both variables later, only a single instance of
'WizardClass' exists.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 6 '05 #2
"Herfried K. Wagner [MVP]" wrote:
"Gregory Gadow" <te******@serv. net> schrieb:
In a VB.Net 2.0 project, I have a WizardClass that contains a List (of
RuleClass). Each rule needs access to variables within it's parent
wizard, so the constructor for RuleClass is New (ByVal Wizard as
WizardClass). Each rule then has a property Private Wiz as WizardClass,
which is set in Sub New with Wiz = Wizard. Currently there is only one
Wizard in the project but there might be more in the future to handle
different rulesets.

My understanding is that, with this, each instance of RuleClass has its
own isolated and independent copy of WizardClass


If you are passing the reference to an existing 'WizardClass' object to the
new object and store the reference to this object there, only an additional
reference is created. Both instances will reference the same 'WizardClass'
object afterwards.


I can make the whole issue go away by using ByRef in the constructor of
RuleClass?

In that case, this code in RuleClass will store only a reference and not a
complete instance

Private Wiz as WizardClass

Public Sub New(ByRef MyWizard as WizardClass)
Wiz = MyWizard
....
End Sub

and latter calls of

Wiz.DefaultPrin ter

will all reference the same block of memory?

/slaps forehead

Some things in .NET are too bloody obvious to be seen. Thanks.
, which would take up a
huge amount of memory if there are a lot of rules. This is part of a
service, so the memory would be permanently used. Is this actually the
case? If so, any suggestions on how to prevent this memory use?


No, it isn't the case if 'WizardClass' is a reference type, e.g. a class.
I am thinking that I can declare each instance of WizardClass to be
Shared, so in the service module:

Private Shared Wizzie1 as WizardClass
Private Shared Wizzie2 as WizardClass

would create independent instances of WizardClass


The code above doesn't create instance of /classes/ at all. It only
declares variables which reference 'Nothing' by default. If you assign the
same reference to both variables later, only a single instance of
'WizardClass' exists.


Correct, I should have written

Private Shared Wizzie1 as New WizardClass(<va rious parameters>)
Private Shared Wizzie2 as New WizardClass(<di fferent parameters>)

Not that it matters now.
--
Gregory Gadow
te******@serv.n et
http://www.serv.net/~techbear

"[W]e have never held that moral disapproval, without any other asserted
state interest, is a sufficient rationale under the Equal Protection
Clause to justify a law that discriminates among groups of persons."
- Sandra Day O`Conner, _Lawrence v Texas_
http://caselaw.lp.findlaw.com/script...0&invol=02-102
Dec 6 '05 #3
"Gregory Gadow" <te******@serv. net> schrieb:
> In a VB.Net 2.0 project, I have a WizardClass that contains a List (of
> RuleClass). Each rule needs access to variables within it's parent
> wizard, so the constructor for RuleClass is New (ByVal Wizard as
> WizardClass). Each rule then has a property Private Wiz as WizardClass,
> which is set in Sub New with Wiz = Wizard. Currently there is only one
> Wizard in the project but there might be more in the future to handle
> different rulesets.
>
> My understanding is that, with this, each instance of RuleClass has its
> own isolated and independent copy of WizardClass
If you are passing the reference to an existing 'WizardClass' object to
the
new object and store the reference to this object there, only an
additional
reference is created. Both instances will reference the same
'WizardClass'
object afterwards.


I can make the whole issue go away by using ByRef in the constructor of
RuleClass?


No, you do not need a 'ByRef' here as 'WizardClass' is already a reference
type. Even with 'ByRef', which I recommend in this sitation, no copy of the
'WizardClass' /object/ is created.
In that case, this code in RuleClass will store only a reference and not a
complete instance

Private Wiz as WizardClass

Public Sub New(ByRef MyWizard as WizardClass)
Wiz = MyWizard
...
End Sub
Even with 'ByVal' instead of 'ByRef' it will only store a reference.
and latter calls of

Wiz.DefaultPrin ter

will all reference the same block of memory?


Yes, they reference the same object (class instance).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 6 '05 #4
Herfried K. Wagner [MVP] <hi************ ***@gmx.at> wrote:
I can make the whole issue go away by using ByRef in the constructor of
RuleClass?


No, you do not need a 'ByRef' here as 'WizardClass' is already a reference
type. Even with 'ByRef', which I recommend in this sitation, no copy of the
'WizardClass' /object/ is created.


Did you mean "Event with 'ByVal', which I recommend [...]"? That's what
I'd expect :)

Gregory: See http://www.pobox.com/~skeet/csharp/parameters.html for
details of parameter passing. It's C#-based, but the basics are the
same for VB.NET. (There are a few differences, such as properties not
being able to be passed by reference in C#, but it should still be
useful in terms of concepts.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 8 '05 #5
Jon,

"Jon Skeet [C# MVP]" <sk***@pobox.co m> schrieb:
> I can make the whole issue go away by using ByRef in the constructor of
> RuleClass?


No, you do not need a 'ByRef' here as 'WizardClass' is already a
reference
type. Even with 'ByRef', which I recommend in this sitation, no copy of
the
'WizardClass' /object/ is created.


Did you mean "Event with 'ByVal', which I recommend [...]"? That's what
I'd expect :)


Yep. I wanted to type 'ByVal'. Thanks for pointing out this "bug" in my
post.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 8 '05 #6
"Jon Skeet [C# MVP]" wrote:
Herfried K. Wagner [MVP] <hi************ ***@gmx.at> wrote:
I can make the whole issue go away by using ByRef in the constructor of
RuleClass?


No, you do not need a 'ByRef' here as 'WizardClass' is already a reference
type. Even with 'ByRef', which I recommend in this sitation, no copy of the
'WizardClass' /object/ is created.


Did you mean "Event with 'ByVal', which I recommend [...]"? That's what
I'd expect :)

Gregory: See http://www.pobox.com/~skeet/csharp/parameters.html for
details of parameter passing. It's C#-based, but the basics are the
same for VB.NET. (There are a few differences, such as properties not
being able to be passed by reference in C#, but it should still be
useful in terms of concepts.)


I'm familiar enough with C and C++ that the basics of C# are no problem. Thanks,
the page is a nice resource.
--
Gregory Gadow
Dec 12 '05 #7

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

Similar topics

1
2508
by: ravi_shankar | last post by:
I know java,but I am just beginner in C.I have some confusions regarding extern storage specifier and default storage class specifier for a variable when it has file scope that is ,when it is not defined within any block -- Posted via http://dbforums.com
5
3853
by: aneesh | last post by:
Hi all, I have a program, this works fine but if we declare static below "int i" it shows different storage class specifier. what will be the reason. #include <stdlib.h> static int i ; int i; int main()
14
7904
by: aruna | last post by:
What is the disadvantage of using register storage class specifier?
3
2718
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to inhibit warning) keywords.c:60:11: Storage Keywords.Keyword released
40
3032
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what I mean, please look at this feedback my me: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3074c204-04e4-4383-9dd2-d266472a84ac If you think I am right, please vote for this feedback.
13
4499
by: John Devereux | last post by:
Hi, For a resource constrained embedded system I want to avoid malloc. Currently I have various "library" modules that hide their data behind opaque pointers. Because I read that was a good thing. For example in the interface foo.h typedef struct foo_t *foo_t;
14
2118
by: Richard Harter | last post by:
Apologies for the length - this post is best viewed with fixed font and a line width >= 72. Below is the source code for a C header file that provides a suite of storage management macros. I am asking for comments on it. In particular: Are there any gotchas that I have overlooked? Are there any suggestions for improvements? Is there a generally available superior packages to do the same thing with the same general licensing? ...
0
1307
by: terminator(jam) | last post by:
Since delaration of C++ the r/l-valueness of objects has been source of confusion there are some actions that can perform on lvalues but not on rvalues , especifically on initrinsic types .BTW the programmer is not capable of defining special interface for classes based on the r/l-valueness of the object(the way we do for const/volatile).Move proposal - about Hot(rvalue) refernce - has been an attempt to get that different behavior at...
11
3152
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global" storage is ButtonHasBeenClicked. In this simple example code in Form1 calls a routine in Module1 which then calls code back in Form1 (subroutine WhatEver). WhatEver needs to access ButtonHasBeenClicked but the reference to ButtonHasBeenClicked...
2
1837
by: asit | last post by:
Can functions have static storage clas ?? If yes, what is it's advantage ??
0
8459
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
8374
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
8653
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
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...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
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
2784
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
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.