C#语言问题 [英] C# language question

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

问题描述

我有一个情况,我不知道我想做什么是可能的。


我有一个名为Account的类,有一些属性,我为这些设置了值

这样的房产。


public void foo()

{

account.BillingCity = Wichita;

account.BillingCountry =" US"

account.BillingState =" KA";

// some更多属性

account.create();

}


是否可以做这样的事情

public void bar()

{

string [] propName = {BillingCity,BillingCountry,BillingState}


for (int i = 0; i< propName.length; i ++)

{

account.propName [i] = someVal;

} < br $> b $ b account.create();

}


我想要做的是拥有一些属性名称列表和那些

名称设置该属性的值。


我这样做不想使用soem开关/ case或if / else语句

解决方案



您是否希望设置所有属性或者只是一些?


也许在您的Account类中,您可以拥有一个构造函数,

接受所有属性,其他属性用于最常设置的属性 - 就像你提到的3个。

公共类帐户

{

private string城市,州,国家;


公共帐户(字符串城市,字符串状态,字符串国家/地区)

{

this.City = city;

this.State = state;

this.Country = country;


// othercode

}


公共账号()

{

//其他代码

}

//其他代码,perhap get / set accessors

} //课程结束帐号


....

...

...

....


然后叫它


....

...

.. 。

....

public void foo()

{

//其他代码

账户账户=新账户(Wichita,KA,US);


//其他代码
}


或者我是不是基于你的问题?

MsJuLiE


On Thu,2004年10月7日03:13:01 -0700,einar

< ei *** @ discussion.microsoft.com>写道:

我有一个情况,我不知道我想做什么是可能的。

我有一个名为Account的课程属性和我为这些
属性设置值。

public void foo()
{
account.BillingCity =" Wichita";
account.BillingCountry =" US";
account.BillingState =" KA" ;;
//更多属性
account.create();
}

是否可以做这样的事情
public void bar()
{
string [] propName = {BillingCity,BillingCountry,BillingState}

for(int i = 0; i< propName.length; i ++)
{
account.propName [i] = someVal;
}
account.create();
}

我想要做的是拥有一些属性名称列表,并为那些
名称设置该属性的值。

我不知道想使用soem switch / case或if / else语句


einar< ei *** @ discussion.microsoft.com>写道:

我有一个情况,我不知道我想做什么是可能的。

我有一个名为Account的类,有一些属性,我设置像这样的这些
属性的值。

public void foo()
{
account.BillingCity =" Wichita";
account.BillingCountry = US;
account.BillingState =" KA";
//更多属性
account.create();
}

是否可以做这样的事情
public void bar()
{
string [] propName = {BillingCity,BillingCountry,BillingState}

for(int i = 0; i< propName.length; i ++)
{
account.propName [i] = someVal;
}
account.create();
}

我想要做的是获得一些属性名称列表,并为那些
名称设置该属性的值。

我不想使用soem开关/案例或if / else st atements




好​​吧,你可以使用反射来做到这一点 - 使用Type.GetProperty和

PropertyInfo.SetValue。你会失去类型安全,而且性能将会下降。


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟

如果回复小组,请不要给我发邮件


嗨einar,

你可以用至少3种方式做到这一点:

1.使用一个接受所有属性的构造函数

2.使用反射来迭代所有属性并设置它们的值

3.使用构造函数接受具有属性名称

的Hastable,然后使用反射来设置值。


让我知道我是否可以提供更多帮助。


HTH,

Rakesh Rajan


" einar"写道:

我有一个情况,我不知道我想做什么是可能的。

我有一个名为Account的课程属性和我为这些
属性设置值。

public void foo()
{
account.BillingCity =" Wichita";
account.BillingCountry =" US";
account.BillingState =" KA" ;;
//更多属性
account.create();
}

是否可以做这样的事情
public void bar()
{
string [] propName = {BillingCity,BillingCountry,BillingState}

for(int i = 0; i< propName.length; i ++)
{
account.propName [i] = someVal;
}
account.create();
}

我想要做的是拥有一些属性名称列表,并为那些
名称设置该属性的值。

我不知道想要使用soem开关/盒子或if / else语句



I have aa situation and i don''t know if what i want to do is possible.

I have a class named Account with some properties and i set values for these
properties like this.

public void foo()
{
account.BillingCity = "Wichita";
account.BillingCountry = "US";
account.BillingState = "KA";
// some more properties
account.create();
}

Is it possible to do something like this
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.propName[i] = someVal;
}
account.create();
}

what i want to do is to have some list of properties names and for those
names set the value of that propertie.

I do not want to use soem switch/case or if/else statements

解决方案


Are you wishing to set all the properties at once or just some?

Perhaps within your Account class you can have a constructor that
accepts all properties and others for the most commonly set properties
upon initial creation of an Account -- like the 3 you mentioned.
public class Account
{
private string City, State, Country;

public Account(string city, string state, string country)
{
this.City = city;
this.State = state;
this.Country = country;

// othercode
}

public Account()
{
// other code
}
// other code, perhap get/set accessors
} // End of class Account

....
...
...
....

then calling it

....
...
...
....
public void foo()
{
// other code

Account account = new Account("Wichita", "KA", "US");

// other code
}


Or am I off base on your question?
MsJuLiE

On Thu, 7 Oct 2004 03:13:01 -0700, "einar"
<ei***@discussions.microsoft.com> wrote:

I have aa situation and i don''t know if what i want to do is possible.

I have a class named Account with some properties and i set values for these
properties like this.

public void foo()
{
account.BillingCity = "Wichita";
account.BillingCountry = "US";
account.BillingState = "KA";
// some more properties
account.create();
}

Is it possible to do something like this
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.propName[i] = someVal;
}
account.create();
}

what i want to do is to have some list of properties names and for those
names set the value of that propertie.

I do not want to use soem switch/case or if/else statements




einar <ei***@discussions.microsoft.com> wrote:

I have aa situation and i don''t know if what i want to do is possible.

I have a class named Account with some properties and i set values for these
properties like this.

public void foo()
{
account.BillingCity = "Wichita";
account.BillingCountry = "US";
account.BillingState = "KA";
// some more properties
account.create();
}

Is it possible to do something like this
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.propName[i] = someVal;
}
account.create();
}

what i want to do is to have some list of properties names and for those
names set the value of that propertie.

I do not want to use soem switch/case or if/else statements



Well, you could use reflection to do that - use Type.GetProperty and
PropertyInfo.SetValue. You lose type safety though, and performance
will go down.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi einar,

You can do this in at least 3 ways:
1. use a constructor which would accept all the properties
2. use reflection to iterate thru all the properties and set their values
3. use a constructor which would accept a Hastable with the property names
and then use reflection to set the values.

Let me know if I could be of more help.

HTH,
Rakesh Rajan

"einar" wrote:

I have aa situation and i don''t know if what i want to do is possible.

I have a class named Account with some properties and i set values for these
properties like this.

public void foo()
{
account.BillingCity = "Wichita";
account.BillingCountry = "US";
account.BillingState = "KA";
// some more properties
account.create();
}

Is it possible to do something like this
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.propName[i] = someVal;
}
account.create();
}

what i want to do is to have some list of properties names and for those
names set the value of that propertie.

I do not want to use soem switch/case or if/else statements



这篇关于C#语言问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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