C# language question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 = 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 said:
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 = 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.
 
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
 
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
 
This class that i have is a proxy class so i dont want to add a constructor
or modify the default constructor.

Rakesh Rajan said:
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 said:
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 = 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
 
Hi einar,

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

- Rakesh Rajan

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

Rakesh Rajan said:
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 said:
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 = 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
 
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 said:
Hi einar,

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

- Rakesh Rajan

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

Rakesh Rajan said:
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

:

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 = 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
 
Hi einar,

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

http://www.ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html?page=1

Let me know your progress.

HTH,
Rakesh Rajan

einar said:
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 said:
Hi einar,

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

- Rakesh Rajan

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

:

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

:

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 = 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
 
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
 
Stephan Schlicker said:
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:
 
Stephan Schlicker said:
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);
 
einar said:
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, someVal);
}
account.create();
}
 
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 said:
einar said:
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, 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
 
Back
Top