Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Creating structs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 20 Mar 2008 20:26 GMT
I wish to send in a struct called Str
as a parameter to a method. Do i have
to create it first and then send it
in or is it possible to create in
upon construction?

For a class i'd do this.

 doSomething (new MyClass (1));

How can i do this for a struct?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Peter Duniho - 20 Mar 2008 20:35 GMT
> I wish to send in a struct called Str
> as a parameter to a method. Do i have
> to create it first and then send it in or is it possible to create in  
> upon construction?

I recommend that your first effort should always be to "just try it".  :)

> For a class i'd do this.
>
>   doSomething (new MyClass (1));
>
> How can i do this for a struct?

Just as you do for a class.  You can use "new" to "instantiate" the needed  
value for the struct to pass to the method.  Just as with the example  
above, you'll only have access to that value from within the method unless  
the method copies the value somewhere else.  But as long as that's okay,  
there's no problem (in fact, unlike with a class, you'd have to pass by  
reference for any changes within the method to be visible outside the  
method anyway, so if anything it's less likely to be a problem to pass a  
struct like that)

Pete
K Viltersten - 20 Mar 2008 21:39 GMT
>> I wish to send in a struct called Str
>> as a parameter to a method. Do i have
[quoted text clipped - 17 lines]
> method anyway, so if anything it's less likely to be a problem to pass a
> struct like that)

I'd like to send in an instance of the struct and pass in a
value to a constructor. However, constructors are not
allowed for structs.

As far i see i can't do the following.

 doSomething (new MyStruct (1));

My compiler complains.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Peter Duniho - 20 Mar 2008 21:46 GMT
> I'd like to send in an instance of the struct and pass in a
> value to a constructor. However, constructors are not
> allowed for structs.

What makes you say that?  It's not true.

> As far i see i can't do the following.
>
>   doSomething (new MyStruct (1));
>
> My compiler complains.

Well, as far as the code you posted goes, of course it would.  There's no  
declaration for MyStruct, nor for doSomething().  My compiler would  
complain too.

Now, if you have some other code, the context in which the above line  
_would_ otherwise compile (that is, it exists in a larger setting in which  
MyStruct and doSomething() are actually legitimate, declared identifiers),  
you should show that code.  Likewise, you should be more specific about  
what's not working than "my compiler complains".  There's no way for  
anyone to just guess at what you're doing wrong.  You have to _show_ us.

But there's nothing fundamentally wrong with a line of code that looks  
like what you posted, assuming you've correctly declared the struct and  
the method being called.

Pete
K Viltersten - 20 Mar 2008 22:36 GMT
>> I'd like to send in an instance of the struct and pass in a
>> value to a constructor. However, constructors are not
>> allowed for structs.
>
> What makes you say that?  It's not true.

To answer your question - the error message i get from
the compiler. It read exactly:

"Structs cannot contain explicit parameterless constructors"

See, now read it omitting the word "parameterless". Note,
you need to OMIT it. Otherwise, it might sound like if one
actually could declare constructors for structs.   :)

In plain text - i pulled a donkey. Sorry for confusing both of us.

Thanks for the help.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Rene - 21 Mar 2008 03:11 GMT
> "Structs cannot contain explicit parameterless constructors"

A struct inherently contains a parameterless constructor, it must do so
because if it didn’t then how would you be able to do something like the
following:

MyStruct[] myArray = new MyStruct[100];

If the struct didn’t have the parametrless constructor the array above could
not possible be created.

The question ofcourse would be, why won't the compiler allow you to define
your own parameterless constructor and override the default one...... the
answer to that is..... I duno!
Peter Duniho - 21 Mar 2008 03:40 GMT
> [...]
> The question ofcourse would be, why won't the compiler allow you to  
> define your own parameterless constructor and override the default  
> one...... the answer to that is..... I duno!

I don't know the answer myself, but if I had to guess, it'd be that it's  
because the default constructor for a struct basically always fills the  
struct with zeros.  This means that the compiler knows that it doesn't  
have to do anything special for an array of structs; it's just filled with  
zeros the way any memory allocated from the heap would be.  Likewise for  
structs found as fields in classes (which also get filled with zeros, not  
counting instance-initialization -- i.e. parameterless constructor or  
field initializers -- that _is_ supported for classes and so which might  
occur).

If the language supported defining your own parameterless constructor (or  
field initializers, which are also not allowed in structs), it would  
introduce a special case in which the compiler sometimes would have to  
generate a bunch of code to iterate over an array, or to add implicit  
calls to the instance-initialization code for class members that are  
structs.  It makes sense to me that rather than introduce that  
complication, the language designers just decided to forego the utility of  
user-defined default constructors.  It was a trade-off in which simplicity  
of the language and compiler won (which IMHO is generally the right way to  
go, though I'm sure not everyone agrees).

Basically, value types remain nice and simple with minimal overhead and  
consistent, predictable behavior.  If you want more complex behavior, you  
need a class.  It seems like a reasonable rule to me.

Pete
Jon Skeet [C# MVP] - 21 Mar 2008 09:43 GMT
> > [...]
> > The question ofcourse would be, why won't the compiler allow you to  
[quoted text clipped - 6 lines]
> have to do anything special for an array of structs; it's just filled with  
> zeros the way any memory allocated from the heap would be.

Bingo. That's also the case when the struct is used as a member
variable - types are initialized by just wiping out memory.

One interesting (to me anyway) point is that from a *language* point of
view all value types have a parameterless constructor - but from a
*runtime* point of view, value types *never* have a parameterless
constructor. (They're initialized with a different op-code.) You can
see this by trying to fetch the constructors for a value type using
reflection - you won't see the parameterless one.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.