473,670 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance problem/confusion

Ok, this is the structure of my classes.
" class Global
" |
" -------------------------------------------
" | |
"class SomeForm1 class SomeForm2
Now I declare an int in "Global" and initialize in the main of
Someform1. When I try to access this int from SomeForm2 it ignores the
changes I made to this int from SomeForm1.

What should I do to have a true global variable that can be modified
anywhere and accessed anywhere without having to worry about these
instances?

hope this is clear.

Nov 16 '05 #1
9 1360

"vidalsasoo n" <kj*****@gmail. com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
Ok, this is the structure of my classes.
" class Global
" |
" -------------------------------------------
" | |
"class SomeForm1 class SomeForm2
Now I declare an int in "Global" and initialize in the main of
Someform1. When I try to access this int from SomeForm2 it ignores the
changes I made to this int from SomeForm1.

What should I do to have a true global variable that can be modified
anywhere and accessed anywhere without having to worry about these
instances?

hope this is clear.


It isn't entirely clear, because I can't tell if SomeFormN are subclasses or
instances.

Regardless, the way to make a field have the same value among all instances
is to declare it static.
Nov 16 '05 #2
I never had a clear idea on why to use static. with that answer it all
makes sense (and works perfectly).

ty

Nov 16 '05 #3
The initilization you have done thru the main(or is it the contstructor?) of
SomeForm1. Now this value cannot be accecced from SomeForm2 as SomeForm2 is a
sub class of Gobal not of SomeForm1.

For accessing this value, you need to create an instance of SomeForm1 within
SomeForm2.

I hope you are trying to create instances of SomeForm1 and SomeForm2 and use
the value initialized by SomeForm1 from SomeForm2. Here the int has object
scope, so every instance will have its on copy of int . You need to declare
the int as static if u wana share with all instances.

"vidalsasoo n" wrote:
I never had a clear idea on why to use static. with that answer it all
makes sense (and works perfectly).

ty

Nov 16 '05 #4
A word of advice: read up on classes and inheritance, and objects and
instantiation. Those two pairs of words refer to two very different
concepts.

I'm not trying to be picky and pedantic. I'm trying to help. Really.
First, once you have the difference between these two concepts clear in
your mind, the quality of your programs will increase dramatically.
Second, once you understand the difference, and use the correct terms
in your posts, you will recieve far swifter and more accurate
responses. In this case, Mike took a guess at what you meant and he
guessed right, however he might just as easily have spun off into a
discussion of base classes and derived properties that had nothing to
do with your problem.

Again, I don't want you to think that I'm being superior. I just want
you to know that understanding the differences between classes and
instances, and inheritance and instantiation, will be a tremendous boon
to you.

Nov 16 '05 #5
If Mike Schilling's solution "works perfectly" then you have a
nomenclature problem in your original post.

You need to get a clear idea of the difference between a class (which,
when talking about it in context with its parent class, is sometimes
called a "subclass") an an instance. It's a very, very important
distinction, both in programming and in asking questions here.

I'm not trying to be picky and pedantic; I'm trying to help. Really. If
you master the difference between a class and an instance, and between
inheritance and instantiation, not only will your programming skills
improve dramatically, but when you ask questions here you'll get far
quicker and more accurate answers. In this case, Mike took an educated
guess at what was really going on and guessed correctly. Next time you
may get completely wrong advice because the explanation of the problem
was incorrect.

Anyway, good luck learning C#. It's a great language, and fun to work
in.

Nov 16 '05 #6
my problem was object scope. I had incorrectly assumed that objects
declared in the parent would be modifiable throughout all the
subclasses (as if it was declared static).

I'm still getting the hang of OO and the more I learn the more I
realise how logical/beautiful it is :P

Nov 16 '05 #7

"vidalsasoo n" <kj*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
my problem was object scope. I had incorrectly assumed that objects
declared in the parent would be modifiable throughout all the
subclasses (as if it was declared static).


I think you do need to learn and practice the correct terminology, because
you still have me puzzled about instances vs. subclasses. I think what
you're saying, in standard terminology, is:

My problem was name scoping. I had incorrectly assumed that fields
declared in a parent class
are modifiable via a reference to a subclass.

The thing is, that's a *correct* assumprion, which is one reason I'm
puzzled. My best guess at your incorrect assumption is:

I had incorrectly assumed that fields declared in a parent class are
common to all instances of subclasses.

This is true only for static fields.

There really isn't space here to explain the concepts and terminology. Any
good beginning book on C# (or Java or C++, for that matter) will explain
both (mod the different terminology C++ uses for fields and methods.)
Nov 16 '05 #8
Simply declare a static member. A static member will be available
throughout the namespace in which it is declared.

For instance,

namespace TestStatic
{
public class ClassStaticTest
{
public static int intTemp;
}
public class Class2
{
int temp;
public Class2()
{
ClassStaticTest .intTemp=12;
}
}
}

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9
Ravichandran J.V. <jv************ @yahoo.com> wrote:
Simply declare a static member. A static member will be available
throughout the namespace in which it is declared.


Well, a public static member will be available throughout the whole
AppDomain. An internal static member will be available throughout the
whole assembly. Unlike in Java, namespaces have no effect on access -
they're purely there for avoiding name collisions (and making it easier
to find names, of course).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

4
1401
by: Matthew Bell | last post by:
I've got a conceptual problem to do with inheritance. I'd be grateful if someone could help to clear up my confusion. An example. Say I need a class that's basically a list, with all the normal list methods, but I want a custom __init__ so that the list that is created is rather than (yes, it's a bogus example, but it does to make the point). Without bothering with inheritance, I could do:
5
1336
by: ma740988 | last post by:
Prefer composition to inheritance (can't recall which text I stole that line from) is one of the fundamental tenets thats engrained in my mind. Having said that inheritance requires careful thought. To compound things, when dealing with inheritance 'virtuality' can only be experienced through base pointers to derived. There are ocassions though that I dont need a (particulay) 'virtual' function in base. In which case I'll have to...
22
23350
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
2
2161
by: Flavian Musyoka Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify my confusion: interface IControl { void Paint();
3
2560
by: Hazz | last post by:
I am just beginning to design a Treeview display (winforms) for wine regions. Problem. Some wine growing regions belong to two counties. Eg. Carneros is in both Napa and Sonoma Counties. Although most nodes will have only one parent, a few will have two parents. The Treeview which seems to me to lend itself to the Windows Explorer type of app could not possibly show one file as a leaf belonging to two different parent folders. You either...
0
8471
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
8815
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...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
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...
1
6216
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
5686
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
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
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
2044
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.