473,657 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking Validity of uninitialized object

I have a struct that I am creating an object for but not initializing
it. Later on under certain conditions it gets
set to the values of another struct. However I have to check this
struct for an valid instance and sometimes it is
not a valid instance and gives an exception. Since I cannot use a null
for a value type how would I check for the
validity of the object.

For example:

one.cs contains
------------------------
in a loop :
{
myObject firstInstance;

if firstInstance is valid then
do...something.

setvals ();
}

second.cs contains:
------------------------------
setvals()
{
firstInstance = validInstance;
}

May 16 '06 #1
1 2129
You need an "isValid" boolean or something like it in your struct. The
CLR guarantees that when your struct is first created all booleans will
be "false".

If you are using .NET 2.0 then you can get this same effect by using
the "?" suffix to your type, e.g.:

myStruct? firstInstance;

will create a "nullable" struct.

May 16 '06 #2

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

Similar topics

9
2660
by: Bart Nessux | last post by:
Are these equivelent? Is one approach prefered over the other #check to see if var contains something... if so proceed. if var is not None: continue #check to see if var is empty... if so prompt user again.
15
12772
by: Robert Mark Bram | last post by:
Hi All! I have the following code in an asp page whose language tag is: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> // Find request variables. var edition = Request.Form ("edition"); var language = Request.Form ("language"); Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and value &quot;" + edition + "&quot;<br>");
7
1463
by: Michael Sgier | last post by:
Hi folks class X { private: CTexture *modelTex; // texture data } CTexture* X::Load()
99
5132
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
3
1277
by: Sam Martin | last post by:
hi all, I'm looking for your opinions on whether to validate and subsequenly throw exceptions based on the data set() to an object's property or whether you should do it on say a Save() method. For example, I have a series of Business Components that have fairly straight forward properties, and methods to Save(). Should I...
3
2006
by: The Other Mike | last post by:
In my existing Powerbuilder application I have several statements that check to make sure an object is instatiated. ie. If isvalid(form1) then.... Is there a way to check for this in VB.NET? I have pasted the definition of IsValid from Powerbuilder. I appologize for being such a newbie.
351
12948
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea that the faulty code is written by blackguards bent on foulling the language. The term...
0
1194
by: rdemyan | last post by:
Is there a way to check the validity of front-end table links on a backend file where all permissions to data have been revoked. To get at the data, the front end uses RWOP queries. What I'm trying to do is to avoid having to always delete table links and recreate them at startup since this takes quite awhile. I know you can try to open a recordset, but this fails because unless the user is the owner, they have no permissions on the...
9
2967
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r"); if(!$fp) {
0
8394
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...
1
8503
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
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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
6164
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
4152
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
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.