473,772 Members | 3,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(MyStruc t) returns a MyStruct
with value already set to 64 ?

Joanna

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

"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(MyStruc t) 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.WriteLi ne("Value is {0}",struct.val ue);
}

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
4430
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 characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
17
3280
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
3754
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 their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
3
12266
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 nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
25
7645
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4526
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
6118
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 pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
0
2345
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 jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
12938
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 to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
12
29915
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
9620
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
9454
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,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.