Creating structs

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

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?
 
I wish to send in a struct called Str
I recommend that your first effort should always be to "just try it". :)


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)


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.
 
I'd like to send in an instance of the struct and pass in a
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.
 
"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 said:
[...]
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.

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.)
 
Back
Top