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

Home Posts Topics Members FAQ

C# language question

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
Nov 16 '05 #1
12 1141

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


Nov 16 '05 #2
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
Nov 16 '05 #3
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

Nov 16 '05 #4
This class that i have is a proxy class so i dont want to add a constructor
or modify the default constructor.

"Rakesh Rajan" wrote:
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

Nov 16 '05 #5
Hi einar,

Then go for the second procedure using reflection. Do let me know if you
need more help.

- Rakesh Rajan

"einar" wrote:
This class that i have is a proxy class so i dont want to add a constructor
or modify the default constructor.

"Rakesh Rajan" wrote:
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

Nov 16 '05 #6
I am new to reflections do ypu have some examples for me?? I am currently
going through some samples on MSDN but that isn´t going all to well.

"Rakesh Rajan" wrote:
Hi einar,

Then go for the second procedure using reflection. Do let me know if you
need more help.

- Rakesh Rajan

"einar" wrote:
This class that i have is a proxy class so i dont want to add a constructor
or modify the default constructor.

"Rakesh Rajan" wrote:
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

Nov 16 '05 #7
Hi einar,

This is a wonderful article on attributes and reflection which will help you.

http://www.ondotnet.com/pub/a/dotnet...ex.html?page=1

Let me know your progress.

HTH,
Rakesh Rajan

"einar" wrote:
I am new to reflections do ypu have some examples for me?? I am currently
going through some samples on MSDN but that isn´t going all to well.

"Rakesh Rajan" wrote:
Hi einar,

Then go for the second procedure using reflection. Do let me know if you
need more help.

- Rakesh Rajan

"einar" wrote:
This class that i have is a proxy class so i dont want to add a constructor
or modify the default constructor.

"Rakesh Rajan" wrote:

> 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

Nov 16 '05 #8
Hi einar, here is an example I use in my code. It works very fine! Perhabs
it may help you!?

//Get the PropertyInfo for a special Property
PropertyInfo pInfo = this.controlType.GetProperty("UserID");
//Get the MethodInfo for the corresponding SetMethod
MethodInfo mInfo = pInfo.GetSetMethod();
object[] attributes = {userID};
//Invoke the SetMethod
mInfo.Invoke(control, attributes);

greetings stephan
I am new to reflections do ypu have some examples for me?? I am currently
going through some samples on MSDN but that isnt going all to well.

"Rakesh Rajan" wrote:


Nov 16 '05 #9

"Stephan Schlicker" <sc******@hotmail.com> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
Hi einar, here is an example I use in my code. It works very fine! Perhabs
it may help you!?

//Get the PropertyInfo for a special Property
PropertyInfo pInfo = this.controlType.GetProperty("UserID");
//Get the MethodInfo for the corresponding SetMethod
MethodInfo mInfo = pInfo.GetSetMethod();
object[] attributes = {userID};
//Invoke the SetMethod
mInfo.Invoke(control, attributes);
I guess that a simpler code would be:

greetings stephan
I am new to reflections do ypu have some examples for me?? I am currently
going through some samples on MSDN but that isnt going all to well.

"Rakesh Rajan" wrote:


Nov 16 '05 #10

"Stephan Schlicker" <sc******@hotmail.com> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
Hi einar, here is an example I use in my code. It works very fine! Perhabs
it may help you!?

//Get the PropertyInfo for a special Property
PropertyInfo pInfo = this.controlType.GetProperty("UserID");
//Get the MethodInfo for the corresponding SetMethod
MethodInfo mInfo = pInfo.GetSetMethod();
object[] attributes = {userID};
//Invoke the SetMethod
mInfo.Invoke(control, attributes);
I guess that a simpler code would be:

//Get the PropertyInfo for a special Property
PropertyInfo pInfo = control.GetType().GetProperty("UserID");
// set the value of the property
pInfo.SetValue(control, userID, null);

greetings stephan
I am new to reflections do ypu have some examples for me?? I am currently
going through some samples on MSDN but that isnt going all to well.

"Rakesh Rajan" wrote:


Nov 16 '05 #11
einar wrote:
This class that i have is a proxy class so i dont want to add a
constructor
or modify the default constructor.


Does that include any modifications to the class? Otherwise, I'd
recommend using the switch/case or if/then method, by in a method in the
class:

class Account
{
public Property(string name, object val)
{
switch "BillingCIty":
BillingCity = val;
break;
// etc
}
}
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.Property(PropName[i], someVal);
}
account.create();
}
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #12
What i want to do is to have some method that can taka a string array of
properties names and manipulata those properties. So if the the class that i
am working with is change then i have to add another statement to the
switch/case.

I want to be abe to send in to this function the name of none or all the
properties for the class and call those properties and set theire value.

So if a property is added to the class just add another value to the string
array with the name of the new property.

This C# code will be called from Axapta.

"James Curran" wrote:
einar wrote:
This class that i have is a proxy class so i dont want to add a
constructor
or modify the default constructor.


Does that include any modifications to the class? Otherwise, I'd
recommend using the switch/case or if/then method, by in a method in the
class:

class Account
{
public Property(string name, object val)
{
switch "BillingCIty":
BillingCity = val;
break;
// etc
}
}
public void bar()
{
string[] propName = {BillingCity ,BillingCountry,BillingState }

for( int i=0; i< propName.length; i++ )
{
account.Property(PropName[i], someVal);
}
account.create();
}
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #13

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

Similar topics

26
by: HackingYodel | last post by:
Hello all! I'm learning to program at home. I can't imagine a better language than Python for this. The ideal situation, for me, would be to study two languages at the same time. Probably...
33
by: Quest Master | last post by:
I am interested in developing an application where the user has an ample amount of power to customize the application to their needs, and I feel this would best be accomplished if a scripting...
1
by: rdsteph | last post by:
I am having a lot of fun using the pyGoogle module ( http://pygoogle.sourceforge.net/ ) that uses the Google API. It is about as easy to use as I can imagine, and it is a lot nicer than using my...
8
by: Metro Sauper | last post by:
Why is the default text representation for booleans in C# 'True' and 'False' whereas in xml it is 'true' and 'false'? It seems like it would make sense to have them both the same. Metro T....
22
by: Matt | last post by:
Some people identify Microsoft C# is Proprietary programming language. What is Proprietary programming language then? How does it differ from other languages such as C++, or Java?? Please...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
14
by: Magius | last post by:
Hello, I have a question about the correctness of the language grammar for the C# 2.0 specification. I am working with the grammar specified in the June 2005 ECMA-334 standard. Basically, a...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
1
AmberJain
by: AmberJain | last post by:
HELLO, First of all, I accept that this is a too simple question but I got different opinions in different books and so I'm posting it here...... The question is simple.....Is C a block...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.