473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3442
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/vaconStructures AndClasses.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.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
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.co m> wrote in message
news:O4******** ******@TK2MSFTN GP03.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
4636
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
11241
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
31571
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, a Class can have private and protected members, but a Struct everything is Public by default. I laymans terms what would be an appropriate reason to choose a Struct over a Class? So why would one want to choose a Class over a Struct.
13
2232
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
1337
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 be implemented as a property): MyObject GetObjects(); or
3
5508
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 "hide" a parent method's implementation verses simply overriding it (ie take advantage of polymorphism)? Thanks, TJ
11
2226
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, Struct is may be better...but i am looking for some advise from the experts on this? Assumptions: it is possible for some operations, i may have 8000 to 10000 (or more) objects instantiated. what are some considerations in designing a system that...
3
3371
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 PHP version verses timezonedb version. Q2: I am also looking to find a table of timezonedb version verses Olsen timezone database used to construct it. For example, I think that the 2006.16 version corresponds to the Olsen tzdata2006p.
2
2297
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 pointers have to point to the full node type, and have to be referenced before that full node type is known. One solution is to use the 'fixpoint construction' to get an apparent circular dependency... class c_Final : public c_Layer2< c_Layer1...
0
8375
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
8290
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
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
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
5622
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
4149
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...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
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
1593
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.