Switch on GET/SET accessor

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

Hi,
How it is possible to implement such thing:
I want some properties will be readonly, based on value of other properties.
It should be something like this:

public class TEST
{
int m_int,j_int,k_int;
public int INT
{
get
{ return m_int;}
set
{m_int = value;}
}

public int JNT
{
get { return j_int;}

//if (m_int=5) /* HERE I WANT JNT READ ONLY IF INT=5
set
{j_int = value;}
}

//if(m_int!=10) /* HERE I DO NOT WANT THIS PROPERTY AT ALL IF INT=10
public int KNT
{
get
{ return k_int;}
set
{k_int = value;}
}
}
 
Hi Tamir,

Of course this is not possible. If it was possible I can't see how anyone
would use your class. A class has property or doesn't have it. How come at
one moment it has a property and all of a sudden it disappears.

What you can do, thought, is to throw an exception when, say, set property
cannot be used. Even in this case it would be pain to use that class
 
I found some workaround by using #if directives.... Still looking for better
solution.

Stoitcho, I want to explain the propose of such thing:
I have data class which has some propertie of type. In some type other
properties are obsolete, so nothing in the program will go to use or bind
it, but if there is other type of the class those properties are needed.
Of couse it possible to build abstract class and inherit from, but it is not
very good idea, when your objects are for data propose only and will not be
runed anyway.
So, if you still think that it does not make sense, response and we'll
discuss about... :)
 
Yes, but it is not exactly what I need.
I want some class instance will have X properties if TYPE A and Y properties
if TYPE B, ObsoleteAttribute just disable the property, but it is not hat I
need.

Thank you any way
 
Hi Tamir
I found some workaround by using #if directives.... Still looking for better
solution.

#if directives are preprocessor directives. You can include/exclude some
portion of your source files for compiling. You cannot use preprocessor
directives at run time. As far as I can see from your code you want to
add/remove properties at runtime. This is not posible.
Stoitcho, I want to explain the propose of such thing:
I have data class which has some propertie of type. In some type other
properties are obsolete, so nothing in the program will go to use or bind
it, but if there is other type of the class those properties are needed.
Of couse it possible to build abstract class and inherit from, but it is not
very good idea, when your objects are for data propose only and will not be
runed anyway.
So, if you still think that it does not make sense, response and we'll
discuss about... :)

Frankly, it still doesn't make sense to me. IMHO you need to completely
different classes. How you said they can share common base class or
interface. There are some design patterns that might help you:
ObjectFactory, Adapter or even visitor.

Ofcourse maybe I'm not gettig your problem correctly. If you post some
simple example of what do you need it may help us a lot.

B\rgds
100
 
Hi Tamir,

look at this

interface IFoo
{
PropA
{
get;
set;
}

PropB
{
get;
set;
}
}

class FooPar: IFoo
{
public PropA
{
get{....}
set{....}
}

//Pay attention on the explicit implementation of this interface member
IFoo.PropB
{
get{....}
set{....}
}
}


FooBar fooBar = new FooBar();

fooBar.PropA = ....; //OK
int xxx = fooBar.PropA; //OK

fooBar.PropB =... //Error fooBar deosn't support PropB
xxx = fooBarPropB; //Error fooBar deosn't support PropB

IFoo foo = fooBar;
fooBar.PropA = ....; //OK
int xxx = fooBar.PropA; //OK

fooBar.PropB =... //OK
xxx = fooBarPropB; //OK

Is it what you want?

Anyway you can't do this just for one of the accessors

B\rgds
100
 
The common way of doing something like this is to provide both the getter
and setter, but throw an InvalidOperation exception if the current object
state is what expected.
 
Thank you for response, but I sill have problem with it :)

I'll try to be more specific...

Two patterns:

1#
I have class named ItemInTheStore it has type (car, dressware, bread)
If the type of Item is FOOD, I do not need property named bra size, but I
still want to present it in the grid with NULL value in column size, but not
0 (or -1) 'cos it's integer

2#
I have class of experiment and it has type (type a and type b) in case of
type A value of hemoglobin level (for example) is obsolete and make no
sense, but ExperimentCollection has membes of both types and they should be
displayed together in the grid, but hemoglobin level value (float) column
should be NULL (not 0) in case of experiment type A

I believe it's clearer now ;)
 
Hi Tamir,

Thank you for posting in the community!

Based on your discussion with Stoitcho, I understand what you need little
by little. :-)

I think in your last reply, the word "grid" refers to the WinForm DataGrid
control. you implement your customized class instance as the DataGrid's
datasource. Then you want its property to be displayed in datagrid
dynamicly.

I think this can be done through implementing ICustomTypeDescriptor
interface. This interface enable you to create or remove the a class's
properties at runtime.

You can override ICustomTypeDescriptor.GetProperties method, then you can
remove or add properties with the property collection. Then the datagrid
will display with your mind.

For a sample, please refer to "Bending the .NET PropertyGrid to Your Will":
http://www.codeproject.com/cs/miscctrl/bending_property.asp

There is a full implementation of ICustomTypeDescriptor in this article's
source code.

Althrough this article is with PropertyGrid, DataGrid control will also
query ICustomTypeDescriptor interface for class instance.

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Tamir,

Is your problem resolved?

If you still have anything unclear, please feel free to feedback. I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Tamir,

Thanks very much for your feedback!

I am glad you have found a workaround for your issue. Anyway, if you have
any further issue, please post, you will benefit from the community.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top