473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help tracking class property initializations

I have a class with public members, and I need to know whether those
members have been explicitly initialized. I could accomplish that
with a class like this:

class C
{
private bool m_isSet = false;
private object m_object;

public object MyObj
{
get
{
return m_object;
}
set
{
m_object = value;
m_isSet = true;
}
}
}

....but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?

class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}

Jul 23 '07 #1
4 1116
Hi,

<dv*****@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
...but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?

class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}
In general there is no way of doing it, if you only have references types
you could check if they are not null. This fails for valued types, as they
always have a value. Additionally the method will also fails with references
types that the class initialize in the constructor.

You will have to use the verbose method I think
Jul 23 '07 #2
That only applies if null is not a valid state for the reference type
fields that he has. If you can explicitly set the value to null, then you
still need the flag.

On the flip side, as of .NET 2.0, you could use a nullable type for the
backing field (assuming null is not allowed as a value in the range of
values) for value types, and then check for null to see if the value has
been set (while the property exposed is not nullable).

You could get away with this by using a context bound object, and
intercepting the calls, but that is a lot of work, and there is going to be
a bit of overhead to intercept the calls. However, it would allow for a
more concise implementation (the interception layer for the context bound
object would handle whether or not a property was explicitly set).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2***************@TK2MSFTNGP04.phx.gbl...
Hi,

<dv*****@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
>...but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?

class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}

In general there is no way of doing it, if you only have references types
you could check if they are not null. This fails for valued types, as they
always have a value. Additionally the method will also fails with
references types that the class initialize in the constructor.

You will have to use the verbose method I think

Jul 23 '07 #3
I want to use both value and reference types, and null may be a valid
value. I will check into context-bound objects (haven't heard of them
before).

In the meantime, I can get a reasonably concise class definition using
a generic MessageProperty class, as shown below. Implicit conversion
operators allow me to use the C.MyObj member as whatever type I
choose. I don't see a significant downside to this approach, but I
welcome comment.

public class MessageProperty<T>
{
private T m_value;
private bool m_isSet = false;

public T Value
{
get
{
return m_value;
}
set
{
m_value = value;
m_isSet = true;
}
}

public bool IsSet
{
get { return m_isSet; }
}

static public implicit operator T(MessageProperty<Tproperty)
{
return property.Value;
}

static public implicit operator MessageProperty<T>(T value)
{
MessageProperty<Tprop = new MessageProperty<T>();
prop.Value = value;
return prop;
}
}

public class C
{
public MessageProperty<object MyObj = new
MessageProperty<object>();
}

Jul 23 '07 #4
I actually think that's a pretty elegant way to handle it. It's much
better than context bound objects, as you won't have the overhead of
interception. I'd definitely use this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<dv*****@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
>I want to use both value and reference types, and null may be a valid
value. I will check into context-bound objects (haven't heard of them
before).

In the meantime, I can get a reasonably concise class definition using
a generic MessageProperty class, as shown below. Implicit conversion
operators allow me to use the C.MyObj member as whatever type I
choose. I don't see a significant downside to this approach, but I
welcome comment.

public class MessageProperty<T>
{
private T m_value;
private bool m_isSet = false;

public T Value
{
get
{
return m_value;
}
set
{
m_value = value;
m_isSet = true;
}
}

public bool IsSet
{
get { return m_isSet; }
}

static public implicit operator T(MessageProperty<Tproperty)
{
return property.Value;
}

static public implicit operator MessageProperty<T>(T value)
{
MessageProperty<Tprop = new MessageProperty<T>();
prop.Value = value;
return prop;
}
}

public class C
{
public MessageProperty<object MyObj = new
MessageProperty<object>();
}

Jul 23 '07 #5

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

Similar topics

15
by: Tim Henderson | last post by:
Hi i have some errors in my code that i can't find the reason for here is the error i have 3 classes: -Song -Album -Artist Song has 3 pieces of data with getter and setter meathods: -Name...
1
by: Robert Oschler | last post by:
This is my current strategy for tracking hyperlink clicking by a site visitor (Internet Explorer example): Using Javascript I: Attach an event to the document "onclick" handler. When a click...
2
by: mark | last post by:
I've been working on an Access 2000 database for a couple of weeks now. I took a course in access about a year ago, a crash course, and I learned a ton, but I didn't touch Access for the year since...
7
by: Hyoung Lee | last post by:
A simple method of simulating a class, such as C++ or UML class, in C would be using the struct construct. Similarly, we use C functions to simulate a methods; e.g., rv method_a (&struct,...
2
by: MyNameIsnt | last post by:
Can anyone tell me why, when I click on the buttons it register 2 characters on the display? if you use the right mousebutton it works ok, but the buttons dont flash?? it works fine without the...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
3
by: tc | last post by:
I'm trying to put together a small control. It's not, but for arguments sake, let's say it's a progress bar. The user will make a change to a setting, lets call it 'value'. I want to trap the...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
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
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...
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
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...
1
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...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.