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

Value type intialization...

Hi!

The C# does not allow me to access
the value type variable before it is initialized:

MyStruct a;
a.Size(10);

Is there a way around that or is there a special
line where the constructors are called really
necessary

Thanks!
Atmapuri
Aug 31 '06 #1
3 1329
Nope; it must be assigned; what you have is comparable to:

int a;
a.ToString();

which fails for the same reason; you /must/ give it a starting value, even
if it is just "new MyStruct();"

Marc
Aug 31 '06 #2
As Marc correctly explained this is not possible. If you don't want to
create constructors I'd suggest using one standard that can be found in
almost all framework value types that is ro have a property Empty that
returns initialzied *empty* object. You can create and store such an object
in a static variable so you don't have to create a new one each time. Having
such a property you can write a code like the following:

Foo f = Foo.Empty;

Then you need to give value only to the field you use.
--
HTH
Stoitcho Goutsev (100)

"Marc Gravell" <ma**********@gmail.comwrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
Nope; it must be assigned; what you have is comparable to:

int a;
a.ToString();

which fails for the same reason; you /must/ give it a starting value, even
if it is just "new MyStruct();"

Marc

Aug 31 '06 #3
Hi!

Thanks guys :)
Atmapuri

"Stoitcho Goutsev (100)" <10*@100.comwrote in message
news:Ob**************@TK2MSFTNGP06.phx.gbl...
As Marc correctly explained this is not possible. If you don't want to
create constructors I'd suggest using one standard that can be found in
almost all framework value types that is ro have a property Empty that
returns initialzied *empty* object. You can create and store such an
object in a static variable so you don't have to create a new one each
time. Having such a property you can write a code like the following:

Foo f = Foo.Empty;

Then you need to give value only to the field you use.
--
HTH
Stoitcho Goutsev (100)

"Marc Gravell" <ma**********@gmail.comwrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
>Nope; it must be assigned; what you have is comparable to:

int a;
a.ToString();

which fails for the same reason; you /must/ give it a starting value,
even if it is just "new MyStruct();"

Marc


Sep 2 '06 #4

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

Similar topics

16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
3
by: =?Utf-8?B?Umljb2hEZXZlbG9wZXI=?= | last post by:
I have a class library project that uses unmanaged C dll to perform some image handling. This DLL requires me to pass in a structure containing image coordinates. In VB6, I could use a...
4
by: ballygowanboy | last post by:
i've put this code together. there's a variable "s" giving me some grief at the mo, i'm actualy supprised it half works. it's a simple shopping cart, you pick the quantitly of items, and it...
1
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two...
8
by: Ralf Goertz | last post by:
Hi, I want to do able to declare an object bar of a templated class foo where the actual template type of foo is only know at runtime: ----- #include <string> template <class Tclass foo {
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
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...

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.