473,770 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structure vs Class Objects

If I have a structure like;

Public Structure myStructureDef
Public b() as Byte
Public t as String
End Structure

If I pass this structure, will the values in the array b be stored on the
stack or will just a pointer to the array be stored on the stack? I am
trying to decide whether to use Structures or Pointers. I know that M'soft
recommends to use a class if the length is over about 16 bytes but does this
include all the array elements or just pointers to the array?
--
Dennis in Houston
Apr 2 '06
14 1805
Herfried,

All decisions should be seen for me in relation to the time that they were
taken.

I can only see this as a reason if it should be used on computers with a
very low amount of memory. Than it is for me a valid criteria.

Just my thought,

Cor
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schreef in bericht
news:uc******** ******@TK2MSFTN GP10.phx.gbl...
"Cor Ligthert [MVP]" <no************ @planet.nl> schrieb:
It's a Microsoft recommendation that definitely makes sense.


Do you have a real world example for me?


Sorry, but there must be a set of criteria to base the decision whether to
use classes or structures on. Otherwise some people would only use
structures and others would never use structures. In other words, that's
the reason why 'Point' is a structure and 'Form' isn't.

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

Apr 2 '06 #11
Doh! typo

| However the array that parameter.b references is the
| same array that local.b references, so changing an element of the array
| "parameter. b(0) = 5" also changes "local.i".

Should be:

"parameter. b(0) = 5" also changes "local.b(0) ".

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
| Kevin,
|| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
| which
|| includes a copy of the array and the string.
| As you stated, Arrays & Strings are reference types. The myStructureDef
| structure contains a reference to the actual objects on the heap. Not a
copy
| of the actual object!
|
| If you pass myStructureDef to a method you will be passing a copy of the
| structure, which includes a copy of the *references* to the array & the
| string objects. There will only be a single instance of the array &
string
| object on the heap!
|
| Because Strings are immutable its hard to notice a difference. However
| Arrays & most other reference types are mutable, consider the following:
|
| Public Structure myStructureDef
| Public i As Integer
| Public b() As Byte
| Public t As String
| End Structure
|
| Private Sub Something(ByVal parameter As myStructureDef)
| parameter.i = 2
| parameter.b(0) = 5
| parameter.b(1) = 6
| parameter.b(2) = 7
| End Sub
|
| Dim local As myStructureDef
| local.b = New Byte() {1, 2, 3}
| local.t = "Hello"
| Something(local )
|
| "parameter" will be a copy of the "local" myStructureDef, parameter.i is
| inline in the structure as its a value type, so changing parameter.i does
| not change local.i. However the array that parameter.b references is the
| same array that local.b references, so changing an element of the array
| "parameter. b(0) = 5" also changes "local.i". However changing the
reference
| itself, will change the reference itself "parameter. b = New Byte() {4, 5,
| 6}" will create a new array object on the heap, replacing the reference
that
| "parameter. b" is...
|
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| "Kevin Westhead" <ma***********@ nospam.nospam> wrote in message
| news:uz******** ******@TK2MSFTN GP15.phx.gbl...
|| Arrays and strings are both reference types, so they'll be allocated on
| the
|| GC heap. I think the 16 byte rule is just one guideline aimed at getting
| you
|| to consider whether you really want value type or reference type
| semantics.
|| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
| which
|| includes a copy of the array and the string. If you assign one instance
to
|| another, again you'll be assigning a copy rather than having both
| instances
|| reference the same data. If you find yourself passing myStructureDef
|| arguments ByRef alot then you should really consider using a reference
| type
|| instead. You should also think about how often myStructureDef will be
| boxed
|| in your core scenarios.
||
|| --
|| Kevin Westhead
||
|| "Dennis" <De****@discuss ions.microsoft. com> wrote in message
|| news:15******** *************** ***********@mic rosoft.com...
|| > If I have a structure like;
|| >
|| > Public Structure myStructureDef
|| > Public b() as Byte
|| > Public t as String
|| > End Structure
|| >
|| > If I pass this structure, will the values in the array b be stored on
| the
|| > stack or will just a pointer to the array be stored on the stack? I am
|| > trying to decide whether to use Structures or Pointers. I know that
|| > M'soft
|| > recommends to use a class if the length is over about 16 bytes but does
|| > this
|| > include all the array elements or just pointers to the array?
|| > --
|| > Dennis in Houston
||
||
|
|
Apr 2 '06 #12
Kevin,Jay - as a test, I did the following:

'In my Main Program
Public Structure myStruct
Public b As Byte()
Public s As String
End Structure
Dim st As myStruct
ReDim st.b(3)
st.b(0) = 1 : st.b(1) = 2 : st.b(2) = 3
st.s = "Original String"
Something(st)
'Break here and check values of st which were, st.s=Original String,
st.b(0)=25, st.b(1)=30, st.b(2)=35

Private Sub Something(ByVal c() As Byte)
st.s = "New String"
st.b(0) = 25: st.b(1) = 30: st.b(2) = 35
End Sub

It is obvious that that only a reference to the array object is stored in
the structure and that is what is passed. Of course the string wasn't
changed since it's immutable (whatever that means).
--
Dennis in Houston
"Jay B. Harlow [MVP - Outlook]" wrote:
Doh! typo

| However the array that parameter.b references is the
| same array that local.b references, so changing an element of the array
| "parameter. b(0) = 5" also changes "local.i".

Should be:

"parameter. b(0) = 5" also changes "local.b(0) ".

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
| Kevin,
|| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
| which
|| includes a copy of the array and the string.
| As you stated, Arrays & Strings are reference types. The myStructureDef
| structure contains a reference to the actual objects on the heap. Not a
copy
| of the actual object!
|
| If you pass myStructureDef to a method you will be passing a copy of the
| structure, which includes a copy of the *references* to the array & the
| string objects. There will only be a single instance of the array &
string
| object on the heap!
|
| Because Strings are immutable its hard to notice a difference. However
| Arrays & most other reference types are mutable, consider the following:
|
| Public Structure myStructureDef
| Public i As Integer
| Public b() As Byte
| Public t As String
| End Structure
|
| Private Sub Something(ByVal parameter As myStructureDef)
| parameter.i = 2
| parameter.b(0) = 5
| parameter.b(1) = 6
| parameter.b(2) = 7
| End Sub
|
| Dim local As myStructureDef
| local.b = New Byte() {1, 2, 3}
| local.t = "Hello"
| Something(local )
|
| "parameter" will be a copy of the "local" myStructureDef, parameter.i is
| inline in the structure as its a value type, so changing parameter.i does
| not change local.i. However the array that parameter.b references is the
| same array that local.b references, so changing an element of the array
| "parameter. b(0) = 5" also changes "local.i". However changing the
reference
| itself, will change the reference itself "parameter. b = New Byte() {4, 5,
| 6}" will create a new array object on the heap, replacing the reference
that
| "parameter. b" is...
|
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| "Kevin Westhead" <ma***********@ nospam.nospam> wrote in message
| news:uz******** ******@TK2MSFTN GP15.phx.gbl...
|| Arrays and strings are both reference types, so they'll be allocated on
| the
|| GC heap. I think the 16 byte rule is just one guideline aimed at getting
| you
|| to consider whether you really want value type or reference type
| semantics.
|| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
| which
|| includes a copy of the array and the string. If you assign one instance
to
|| another, again you'll be assigning a copy rather than having both
| instances
|| reference the same data. If you find yourself passing myStructureDef
|| arguments ByRef alot then you should really consider using a reference
| type
|| instead. You should also think about how often myStructureDef will be
| boxed
|| in your core scenarios.
||
|| --
|| Kevin Westhead
||
|| "Dennis" <De****@discuss ions.microsoft. com> wrote in message
|| news:15******** *************** ***********@mic rosoft.com...
|| > If I have a structure like;
|| >
|| > Public Structure myStructureDef
|| > Public b() as Byte
|| > Public t as String
|| > End Structure
|| >
|| > If I pass this structure, will the values in the array b be stored on
| the
|| > stack or will just a pointer to the array be stored on the stack? I am
|| > trying to decide whether to use Structures or Pointers. I know that
|| > M'soft
|| > recommends to use a class if the length is over about 16 bytes but does
|| > this
|| > include all the array elements or just pointers to the array?
|| > --
|| > Dennis in Houston
||
||
|
|

Apr 2 '06 #13
Point has a direct counterpart in the Win32 API. Making it a structure
parallels the Win32 API.

From the Windows GDI SDK:

POINT

The POINT structure defines the x- and y- coordinates of a point.
typedef struct tagPOINT {
LONG x;
LONG y;
} POINT, *PPOINT;

Members
x Specifies the x-coordinate of the point.
y Specifies the y-coordinate of the point.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Windef.h; include Windows.h.
Mike Ober.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uc******** ******@TK2MSFTN GP10.phx.gbl...
"Cor Ligthert [MVP]" <no************ @planet.nl> schrieb:
It's a Microsoft recommendation that definitely makes sense.


Do you have a real world example for me?


Sorry, but there must be a set of criteria to base the decision whether to
use classes or structures on. Otherwise some people would only use
structures and others would never use structures. In other words, that's
the reason why 'Point' is a structure and 'Form' isn't.

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


Apr 3 '06 #14
Dennis,
| It is obvious that that only a reference to the array object is stored in
| the structure and that is what is passed.
Yes! I believe that is what I stated.

| Of course the string wasn't
| changed since it's immutable (whatever that means).
You changed a reference to the string, you did not change the string.

Immutable means that the object itself is unchangeable (not modifiable).

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:62******** *************** ***********@mic rosoft.com...
| Kevin,Jay - as a test, I did the following:
|
| 'In my Main Program
| Public Structure myStruct
| Public b As Byte()
| Public s As String
| End Structure
| Dim st As myStruct
| ReDim st.b(3)
| st.b(0) = 1 : st.b(1) = 2 : st.b(2) = 3
| st.s = "Original String"
| Something(st)
| 'Break here and check values of st which were, st.s=Original String,
| st.b(0)=25, st.b(1)=30, st.b(2)=35
|
| Private Sub Something(ByVal c() As Byte)
| st.s = "New String"
| st.b(0) = 25: st.b(1) = 30: st.b(2) = 35
| End Sub
|
| It is obvious that that only a reference to the array object is stored in
| the structure and that is what is passed. Of course the string wasn't
| changed since it's immutable (whatever that means).
| --
| Dennis in Houston
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Doh! typo
| >
| > | However the array that parameter.b references is the
| > | same array that local.b references, so changing an element of the
array
| > | "parameter. b(0) = 5" also changes "local.i".
| >
| > Should be:
| >
| > "parameter. b(0) = 5" also changes "local.b(0) ".
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > ..NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
| > message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
| > | Kevin,
| > || E.g. if you pass myStructureDef to a method, you'll be passing a
copy,
| > | which
| > || includes a copy of the array and the string.
| > | As you stated, Arrays & Strings are reference types. The
myStructureDef
| > | structure contains a reference to the actual objects on the heap. Not
a
| > copy
| > | of the actual object!
| > |
| > | If you pass myStructureDef to a method you will be passing a copy of
the
| > | structure, which includes a copy of the *references* to the array &
the
| > | string objects. There will only be a single instance of the array &
| > string
| > | object on the heap!
| > |
| > | Because Strings are immutable its hard to notice a difference. However
| > | Arrays & most other reference types are mutable, consider the
following:
| > |
| > | Public Structure myStructureDef
| > | Public i As Integer
| > | Public b() As Byte
| > | Public t As String
| > | End Structure
| > |
| > | Private Sub Something(ByVal parameter As myStructureDef)
| > | parameter.i = 2
| > | parameter.b(0) = 5
| > | parameter.b(1) = 6
| > | parameter.b(2) = 7
| > | End Sub
| > |
| > | Dim local As myStructureDef
| > | local.b = New Byte() {1, 2, 3}
| > | local.t = "Hello"
| > | Something(local )
| > |
| > | "parameter" will be a copy of the "local" myStructureDef, parameter.i
is
| > | inline in the structure as its a value type, so changing parameter.i
does
| > | not change local.i. However the array that parameter.b references is
the
| > | same array that local.b references, so changing an element of the
array
| > | "parameter. b(0) = 5" also changes "local.i". However changing the
| > reference
| > | itself, will change the reference itself "parameter. b = New Byte() {4,
5,
| > | 6}" will create a new array object on the heap, replacing the
reference
| > that
| > | "parameter. b" is...
| > |
| > |
| > | --
| > | Hope this helps
| > | Jay [MVP - Outlook]
| > | .NET Application Architect, Enthusiast, & Evangelist
| > | T.S. Bradley - http://www.tsbradley.net
| > |
| > |
| > | "Kevin Westhead" <ma***********@ nospam.nospam> wrote in message
| > | news:uz******** ******@TK2MSFTN GP15.phx.gbl...
| > || Arrays and strings are both reference types, so they'll be allocated
on
| > | the
| > || GC heap. I think the 16 byte rule is just one guideline aimed at
getting
| > | you
| > || to consider whether you really want value type or reference type
| > | semantics.
| > || E.g. if you pass myStructureDef to a method, you'll be passing a
copy,
| > | which
| > || includes a copy of the array and the string. If you assign one
instance
| > to
| > || another, again you'll be assigning a copy rather than having both
| > | instances
| > || reference the same data. If you find yourself passing myStructureDef
| > || arguments ByRef alot then you should really consider using a
reference
| > | type
| > || instead. You should also think about how often myStructureDef will be
| > | boxed
| > || in your core scenarios.
| > ||
| > || --
| > || Kevin Westhead
| > ||
| > || "Dennis" <De****@discuss ions.microsoft. com> wrote in message
| > || news:15******** *************** ***********@mic rosoft.com...
| > || > If I have a structure like;
| > || >
| > || > Public Structure myStructureDef
| > || > Public b() as Byte
| > || > Public t as String
| > || > End Structure
| > || >
| > || > If I pass this structure, will the values in the array b be stored
on
| > | the
| > || > stack or will just a pointer to the array be stored on the stack?
I am
| > || > trying to decide whether to use Structures or Pointers. I know
that
| > || > M'soft
| > || > recommends to use a class if the length is over about 16 bytes but
does
| > || > this
| > || > include all the array elements or just pointers to the array?
| > || > --
| > || > Dennis in Houston
| > ||
| > ||
| > |
| > |
| >
| >
| >
Apr 3 '06 #15

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

Similar topics

1
2085
by: Victor Hannak | last post by:
I have two classes derived from a base class. The two derived classes each utilize a structure that is slightly different from one another. i.e. DerivedClass1: struct NodeStruct { float NodeValue; ListStruct *NextNode; }
4
2372
by: Albert Yan Qing Ge | last post by:
typedef struct AA { } A; when use gcc compiles, sizeof(A) is 0 when use g++ compiles, sizeof(A) is 1 So I can known the length of this pointer is 1 but when I add one member to the structure, typedef struct AA {
7
2091
by: Adam | last post by:
Hi all, In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and how I might modify a structure in an arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change items based on certain conditions. Thanks in advance!
3
1286
by: dan heskett | last post by:
Hello group, I am trying to get used to vb.net coming from a far far far away set of tools. What i want to do is setup an object in my application that contains other object types, that act like collections. For example: objQuotes.Quotes("2004-10-08").Item("MSFT").StockQuote = "45.45"
15
8241
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns "Default caption". Can anyone tell me why, and how I can make this work? <code> Imports System
6
1403
by: JSheble | last post by:
Are there any reasons why I shouldn't or couldn't use a structure as a property in a class? Specifically since structures are value types and objects are reference types? I have a Shipping object that has 4 different types of addresses. Initially, I created 4 instances of an Address object, but seem inefficient to me since the Address itslef doesn't really have (or need) and methods... I was thinking a structure would be more...
12
2431
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript distinguishes value types from reference types by seeing the variable content?
11
2830
by: JerryWEC | last post by:
I want to be able to create a structure like I did in VB6, that have variables Name As String * 10 Age As String * 3 for the size of the strings. Can I do this some way in VB.net? I'm playing with <StructLayoutArribute()and <MarshalAs()attributes, but I'm not really wanting to expose these structrues to COM. I only want to use
10
6382
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
0
9617
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
9454
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
10099
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
8931
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
7456
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
6710
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
5354
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
4007
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
3
2849
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.