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

Struct verses class.

Hi all,

Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.

Because if i have code like
class a
{

}
struct a1
{
}

As above declaration i have declared class as well as struct with no
paramter. Still it is acceptable.

So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.

thanks in advance.

Jun 1 '06 #1
7 3429
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.
That's completely incorrect.

<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.


You're doing nothing wrong. Where did you hear the above statement? I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)

Jon

Jun 1 '06 #2

Jon Skeet [C# MVP] wrote:
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.
That's completely incorrect.


But true of the almost-equivalent things in VB.NET.
<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.
You're doing nothing wrong. Where did you hear the above statement?


<http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconStructuresAndClasses.asp>,
or similar, possibly.
I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)


Context is everything, isn't it :)

--
Larry Lard
Replies to group please

Jun 1 '06 #3
Hi Jon,

Thanks for your reply.

Larry has providied link of document which contains this statement.

Thats why i was asking question.

thanks

Jon Skeet [C# MVP] wrote:
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.


That's completely incorrect.

<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.


You're doing nothing wrong. Where did you hear the above statement? I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)

Jon


Jun 2 '06 #4
archana <tr**************@yahoo.com> wrote:
Thanks for your reply.

Larry has providied link of document which contains this statement.

Thats why i was asking question.


Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 2 '06 #5
>Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.


Not much of a restriction in practice, is it? What good is an empty
struct anyway? If you only have static members you may as well us a
static class.

The CLR requires value types to either have instance fields or an
explicit size so for empty structs the C# compiler must effectively
add [StructLayout(..., Size=1)]. So now you have a type that appears
to be empty but still actually takes up one byte. I'm not sure I like
that better than what the VB compiler is doing.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 2 '06 #6
Mattias Sjögren wrote:
Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.


Not much of a restriction in practice, is it? What good is an empty
struct anyway? If you only have static members you may as well us a
static class.

The CLR requires value types to either have instance fields or an
explicit size so for empty structs the C# compiler must effectively
add [StructLayout(..., Size=1)]. So now you have a type that appears
to be empty but still actually takes up one byte. I'm not sure I like
that better than what the VB compiler is doing.
Mattias


Truly not a restriction in practice.

I can not come up with a single use for an empty structure. A value type
that doesn't carry it's own value is totally useless.

The closest thing to something useful I can think of is creating
different empty structures that would override the ToString method and
return different values. Then you could put instances of the different
structs in something like an ArrayList, and then you could use the
ToString method on the objects in the ArrayList without needing to know
what structure it really was, and they would return different values.
But then again you could just put the strings in the ArrayList and it
would have exactly the same effect...
Jun 3 '06 #7
"Göran Andersson" <gu***@guffa.com> wrote in message
news:O4**************@TK2MSFTNGP03.phx.gbl...
I can not come up with a single use for an empty structure. A value type
that doesn't carry it's own value is totally useless.


Yes. An empty class can be useful, as a placeholder for derived behavior you
know you'll eventually add, but that doesn't apply to structs.

///ark
Jun 3 '06 #8

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

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
13
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment. Thanks
2
by: Tom Jones | last post by:
I have a class that contains a collection of reference types. This class needs to have a method that returns the collection to the caller. The method's signature could be either (it could also...
3
by: Tom Jones | last post by:
I do not understand what is meant when someone states that a given method is "hidden" verses overriden. Would someone please provide a short example of both cases and why you might want to...
11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
3
by: ak | last post by:
Q1: I am looking to find/build a table of PHP version verses timezonedb version shipped with it. For example, I think that 5.2.0 updated the timezonedb to the 2006.14 version. Is there a table of...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.