473,385 Members | 1,620 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,385 software developers and data experts.

Non-default values for a struct ?

Hi

struct MyStruct
{
int value = 64; // does not compile

public MyStruct()
{
value = 64; //does not compile
}
}

What do I do to create a struct so that default(MyStruct) returns a MyStruct
with value already set to 64 ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 15 '06 #1
3 7947

"Joanna Carter [TeamB]"
Hi

struct MyStruct
{
int value = 64; // does not compile

public MyStruct()
{
value = 64; //does not compile
}
}

What do I do to create a struct so that default(MyStruct) returns a
MyStruct
with value already set to 64 ?

Joanna


Hi Joanna,

The .net framework disallows default (parameterless) constructors for
structs. I'm not sure why, but my guess is that because you can use structs
without "new", the framework cannot guarantee that the default constructor
will always be called.

Suppose the framework did allow your code to compile, then what would be the
value of "value" after the following code:

{
MyStruct struct;
Console.WriteLine("Value is {0}",struct.value);
}

The default value for int is 0, and that would be the output of the code
above, which could allow for some nasty bugs in your code. But that's only
my guess, please someone jump in if I'm wrong.
All that said, you are still allowed to write parameterized constructors,
and your code could look like:

struct MyStruct
{
int value;

public MyStruct(int initval)
{
value = initval;
}
}
Hope that helps.

By the way, it's amazing to see how many delphi dissidents I'm finding
around here. To speak the truth, all my friends who used to program in
delphi before are now converted to C#...

Cheers

Padu
Mar 15 '06 #2
Hello Padu,

The reason is that .net can't guarantee to call parameterless constructor.
Jon Skeet described this here http://www.yoda.arachsys.com/csharp/...t.constructors

P> The .net framework disallows default (parameterless) constructors for
P> structs. I'm not sure why, but my guess is that because you can use
P> structs without "new", the framework cannot guarantee that the
P> default constructor will always be called.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 15 '06 #3
"Padu" <pa**@merlotti.com> a écrit dans le message de news:
Rb******************************@iswest.net...

| By the way, it's amazing to see how many delphi dissidents I'm finding
| around here. To speak the truth, all my friends who used to program in
| delphi before are now converted to C#...

Hey, don't forget that Borland Developer Studio 2006 allows us "Delphiites"
to program in C# within the same IDE that we use for Delphi for Win32 or
..NET. So at least we don't have to completely defect to "the dark side" :-))

Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 15 '06 #4

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.