473,406 Members | 2,956 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,406 software developers and data experts.

Structure Declaration (class level or proc level)

Hello,

I would like to understand the difference between declaring a
structure/variable in a class body, and doing the same thing in a
procedure body.

Example :
In the following code, what are the differences between mCustomer1 &
mCustomer2 ?
class MyClass
{
struct CustomerStruct
{
public string sName;
public string sPhone;
}
CustomerStruct mCustomer1;

public void Test()
{
CustomerStruct mCustomer2;

mCustomer1.sName="John";
mCustomer2.sName="Arnold";
mCustomer1.sPhone="111";
mCustomer2.sPhone="222";

// What are the differences between the 2 structure
// variables ?
}
}

Regards,
Cybertof.

Nov 15 '05 #1
2 1789

"Cybertof" <cy****************@gmx.net> a écrit dans le message de
news:MP***********************@msnews.microsoft.co m...
Hello,

I would like to understand the difference between declaring a
structure/variable in a class body, and doing the same thing in a
procedure body.

Example :
In the following code, what are the differences between mCustomer1 &
mCustomer2 ?
class MyClass
{
struct CustomerStruct
{
public string sName;
public string sPhone;
}
CustomerStruct mCustomer1;

public void Test()
{
CustomerStruct mCustomer2;

mCustomer1.sName="John";
mCustomer2.sName="Arnold";
mCustomer1.sPhone="111";
mCustomer2.sPhone="222";

// What are the differences between the 2 structure
// variables ?
}
}


mCustomer1 will exist if an instance of MyClass exists.
mCustomer2 will exist only if the Test function is called, and will be
destroyed when Test() ends
The changes done on mCustomer2 are unusefull as they will be 'forgotten'
when the Test function ends (mCustomer2 wont exist anymore)
Nov 15 '05 #2
Cybertof <cy****************@gmx.net> wrote:
I would like to understand the difference between declaring a
structure/variable in a class body, and doing the same thing in a
procedure body.


You can't declare a class or structure within a method - only in a
namespace or in a class directly. However, the differences with
variables are:

o Local variables (those declared in a method) are only "valid" for the
lifetime of that method. After that, they "go away" - there's no
way of getting the value back. Also, there's one set of variables
for every method call - if you call it recursively, you get that
many sets of variables, rather than them all being the same.

o Instance variables (declared in a class or struct, but not static):
there's one set of these for each instance, and they survive for
the lifetime of the instance.

o Static variables (declared in a class or struct, with the static
modifier): there's one set of these for the whole type, which means
(in most cases, ie where you're not using multiple AppDomains etc)
you have one set for the lifetime of the app. They're not tied
to any particular instance.

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

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: John Dann | last post by:
I'd like to use the same different variables of the same structure in multiple classes etc. But even though I declare a structure as eg: Public Structure ExampleStructure Dim A as short Dim B...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
7
by: Jimakos Bilakis | last post by:
Hi guys! I'm using the C++ Builder 6 Enterprise Edition where I create some tables in Paradox and with the help of a structure i pass my data from the form (Edit boxes) to the Paradox table with...
4
by: shapper | last post by:
Hello, I have a class which I am using in my profile. I made this class Serializable by using: <Serializable()_ Public Class Options One of the properties in this class is of type Level...
6
by: Scoots | last post by:
I know the usually applied workaround for multiple definitions of header files, but I have a problem on this one. This time, I can't just ifndef the header file that defines my structure. So...
17
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the interface that exists in the .h file). It uses a...
2
by: Phoenix | last post by:
Hi Friends , Could anyone please help me to resolve the following issue : I pass an array of structures to a dll written in VC++ 6.0 whih fills it with data . The following works well for VB...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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
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...
0
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,...

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.