打开GET / SET访问器 [英] Switch on GET/SET accessor

查看:69
本文介绍了打开GET / SET访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何实现这样的事情:

我希望根据其他属性的价值,只读一些属性。

它应该是这样的:


公共类测试

{

int m_int,j_int,k_int;

public int INT

{

get

{return m_int;}

set

{m_int = value;}

}


public int JNT

{

得到{return j_int;}


// if(m_int = 5)/ *这里我想知道JNT只读INT = 5

set

{j_int = value;}

}


// if(m_int!= 10)/ *这里我如果INT = 10,则不要想要这个属性

public int KNT

{

get

{return k_int;}

set

{k_int = value;}

}

}

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;}
}
}

推荐答案

您好Tamir,


当然这是不可能的。如果可能的话,我无法看到任何人如何使用你的班级。一个班级有财产或没有财产。怎么来

它有一个属性,突然间它就消失了。


你可以做的,想到的,就是抛出异常时比如设置属性

不能使用。即使在这种情况下,使用该课程也会很痛苦


-

B \ rgds

100 >
Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道

新闻:eE ************** @ TK2MSFTNGP09.phx.gbl ...
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

--
B\rgds
100
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
如何实现这样的事情:
我希望一些属性将是readonly,基于其他
属性的值。它应该是这样的:

公共课TEST
{m / int,j_int,k_int;
public int INT
{
得到
{return m_int;}
设置
{m_int = value;}
}
公共关系JNT
{
得到{return j_int;}
// if(m_int = 5)/ *这里我想知道如果INT = 5

{j_int = value;}
}

// if(m_int!= 10)/ *这里我不想要这个属性如果
INT = 10 public int KNT
{
获取
{return k_int;}
设置
{k_int = value;}
}
}
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;}
}
}



我通过使用#if指令找到了一些解决方法....还是lo期待更好

解决方案。


Stoitcho,我想解释一下这样的建议:

我有数据类有一些类型的属性。在某些类型中,其他

属性已过时,因此程序中的任何内容都不会使用或绑定

它,但如果有其他类型的类,则需要这些属性。

有可能构建抽象类并继承,但它不是很好的主意,当你的对象仅用于建议数据而不是<无论如何,
已经破灭了。

所以,如果你仍然认为它没有意义,那么我们将会讨论...... b $ b讨论...... :)


" Stoitcho Goutsev(100)[C#MVP]" < 10*@100.com>在消息中写道

news:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
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... :)

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
Hi Tamir,

当然这是不可能的。如果可能的话,我看不出有人会如何使用你的课程。一个班级有财产或没有财产。怎么来呢
它有一个属性,突然它就消失了。

你可以做的,想到的,就是在设置属性时抛出异常不能使用。即使在这种情况下,使用该课程也会很痛苦。

-
B'hgds
100
Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道
新闻:eE ************** @ TK2MSFTNGP09.phx.gbl ...
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

--
B\rgds
100
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...

它是怎么回事可能实现这样的事情:
我希望一些属性是readonly,基于其他
Hi,
How it is possible to implement such thing:
I want some properties will be readonly, based on value of other


属性的值。


properties.

它应该是这样的:

公共课测试
{
int m_int,j_int,k_int;
public int INT
{
获取
{return m_int; }
设置
{m_int = value;}
}
public int JNT
{
get {return j_int;}

// if(m_int = 5)/ *在这里我想知道如果INT = 5

{j_int = value;}
}

// if(m_int!= 10)/ *这里我根本不想要这个属性
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


INT=10

public int KNT
{
获得
{return k_int;}
设置
{k_int = value;}
}
}
public int KNT
{
get
{ return k_int;}
set
{k_int = value;}
}
}




您是否看过ObsoleteAttribute类?

HTH

Brian W

" Tamir Khason" < TA ********** @ tcon-NOSPAM.co.il>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Have you had a look at the ObsoleteAttribute class?
HTH
Brian W
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
我找到了一些使用#if指令的解决方法....仍在寻找
更好的解决方案。

Stoitcho,我想解释一下这个问题的建议:
我有数据类有一些类型的属性。在某些类型中,其他
属性已过时,因此程序中的任何内容都不会使用或绑定它,但如果有其他类型的类,则需要这些属性。
有可能构建抽象类并继承,但是当你的对象仅用于建议数据并且不会被
无论如何时,它都是
不是很好的主意。
所以,如果你仍然认为它没有意义,反应和我们将讨论...... :)

Stoitcho Goutsev(100)[C#MVP]" < 10*@100.com>在消息中写道
新闻:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
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... :)

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
Hi Tamir,
。一个班级有财产或没有财产。怎么来
它有一个属性,突然间它就消失了。

你可以做的,想到的,就是在设置
时抛出异常财产不能使用。即使在这种情况下,使用该课程也会很痛苦。

-
B'hgds
100
Tamir Khason < TA ********** @ tcon-NOSPAM.co.il>在消息中写道
新闻:eE ************** @ TK2MSFTNGP09.phx.gbl ...
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

--
B\rgds
100
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...

它是怎么回事可能实现这样的事情:
我希望一些属性是readonly,基于其他
Hi,
How it is possible to implement such thing:
I want some properties will be readonly, based on value of other


属性的值。


properties.

它应该是这样的:

公共课测试
{
int m_int,j_int,k_int;
public int INT
{
获取
{return m_int; }
设置
{m_int = value;}
}
public int JNT
{
get {return j_int;}

// if(m_int = 5)/ *在这里我想知道如果INT = 5

{j_int = value;}
}

// if(m_int!= 10)/ *这里我根本不想要这个属性
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


INT=10

public int KNT
{
获得
{return k_int;}
设置
{k_int = value;}
}
}
public int KNT
{
get
{ return k_int;}
set
{k_int = value;}
}
}





这篇关于打开GET / SET访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆