Edit Method WPF | Dynamic Parameters

Situation

I'm trying to make an edit member method for my WPF application.

Basically, I have a list of members in my main class, I iterate through that list of members and find the member with the matching Username parameter, that works.

Then, once that username is found within the system (which it will be since the member needs to login with a valid one) I want to set the member."Whatever" paramater to whatever parameter the user chose to edit on the gui, with the new content the user has entered for that parameter.

public void editMember(string Username, string parameter, string newEntry) { foreach (BaseMember bm in members) { if (Username == bm.username) { bm.[parameter] = newEntry; } }

Problem

I don't want to do:

"member.club" and "member.firstname", or "member.street", since there are far too many parameters that can be edited by the user, and it's long winded "bad" code.

how can I do this in ONE line of code? since bm.[parameter] = newEntry; won't work?

More info

This method works if I use a static parameter, for instance, bm.memclub = newEntry; but I want the parameter to be dynamic.

-------------Problems Reply------------

You need to use Reflection:

foreach (BaseMember bm in members)
{

if (Username == bm.username)
{
Type type = bm.GetType();

PropertyInfo prop = type.GetProperty(parameter);

prop.SetValue (bm, newValue, null);
}
}

Reflection provides objects (of type Type) that describe your current object.You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

Category:c# Views:2 Time:2018-05-05
Tags: wpf parameters

Related post

  • MVC Partial Methods with dynamic parameters 2012-02-17

    ASP.NET MVC 2 I'm trying to write a view whose generic parameter is dynamic, and then pass this dynamic model to a partial view <%@ Page Title="" ..... Inherits="System.Web.Mvc.ViewPage<dynamic>" %> and then <% Html.RenderPartial("Menu

  • ObjectDataSource Update method with dynamic parameters 2008-09-29

    I have this DataTable that has a varying set of columns except for a sequence number. | Sequence | Value | Tax | Duty | Total | Any number of columns should be accepted with unique column names. To display that table, I need to use an ObjectDataSourc

  • Calling dynamic function with dynamic parameters in Javascript 2009-03-24

    I'm looking for a trick about this. I know how to call a dynamic, arbitrary function in Javascript, passing specific parameters, something like this: function mainfunc (func, par1, par2){ window[func](par1, par2); } function calledfunc(par1, par2){ /

  • How to Write Method to Verify Reflection Method Names and Parameters 2011-10-14

    We were writing a Selenium test core by using Python webdriver. The main idea is to read from a CSV file with the format: method_name,parameter 1,parameter 2, parameter 3, ..., parameter n And then by using reflection, the test core will call the met

  • Edit method is creating new records, instead of just updating existing ones 2009-08-29

    My question is: How do I make the edit/update methods for my profiles controller stop creating new records when I edit a profile page? I have a user model and profile model. user has_one :profile. profile belongs_to :user. My routes.rb looks like thi

  • Using one delegate to for several methods with different parameters 2010-02-21

    Is it possible to use one delegate for several methods with different parameters somehow? I use reflection to get all the methods in a class, and I want to assign each of them a delegate and save that delegate in a dictionary with an enum as the key.

  • Creating child nodes for a DynamicNode in MvcSiteMapProvider that have dynamic parameters 2010-11-09

    I am using MvcSiteMapProvider 2.2.1 (http://mvcsitemap.codeplex.com), and am having a problem with creating children under a dynamic node (using a dynamicNodeProvider) when those children have a dynamic parameter (id). I am losing breadcrumbs for the

  • Pickle a dynamically parameterized sub-class 2011-01-10

    I have a system which commonly stores pickled class types. I want to be able to save dynamically-parameterized classes in the same way, but I can't because I get a PicklingError on trying to pickle a class which is not globally found (not defined in

  • Dynamic parameters to stored procedure 2011-08-03

    I have a problem in creating dynamic parameters to stored procedure. I have two nvarchar parameters in a stored procedure that are combination of words separated with comma. For example: 'Login, check, take, return' and 'admin, yes, no, yes' I have t

  • Django model with dynamic parameters : how to query? 2011-08-23

    I need to build a generic django object having dynamic parameters. that is parameters that can be different from one object to another. I cannot create different models because parameters can change at any time and I do not want to use a PickledField

  • Django model with dynamic parameters : how to order? 2011-08-24

    I want to have dynamic parameters, so I made these models : class DynamicObject(models.Model): uuid = UUIDField(primary_key=True, editable=False) synopsis = models.CharField(max_length=64) created = models.DateTimeField(auto_now_add=True) modified =

  • Pass dynamic parameters in the keyvalue (message key) to package.properties 2011-12-23

    I have tried to pass dynamic parameters to keyvalue(message to display) which I will get from package.properties to the Action class through the getText() method. To get the message, we can use getText(String keyvalue) method. What should I do to pas

  • Is it possible to write a stored procedure with dynamic parameters like sp_executesql? 2008-10-22

    Is it possible to write a stored procedure (in tsql or other variants) with dynamic parameters like sp_executesql? i.e. the number and type of parameters in not known in advance and should be built dynamically on client side. just the way you can pas

  • What's the best method to pass parameters to SQLCommand? 2008-11-16

    What's the best method to pass parameters to SQLCommand? You can do: cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob"; or cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob"; or cmd.Parameters.Add("@Name").Value = "Bob"; It s

  • Using reflection to get method name and parameters 2009-01-23

    I am trying to workout a way to programatically create a key for Memcached, based on the method name and parameters. So if I have a method, string GetName(int param1, int param2); it would return: string key = "GetName(1,2)"; I know you can get the M

  • Convince my coworker that methods with 26 parameters are bad practice 2009-07-03

    How do I convince my co-worker that methods with 26 parameters are bad practice? We've debated this without any progress. Closest related questions I could find: How many parameters are too many? Worst Java practice found in your experience? What “ba

  • WPF Dynamic Binding X and Y Co-ordinates 2009-11-10

    I have a question on WPF dynamic positioning. I want to place Elipses on the screen based on X and Y co-ordinates that i have stored in a collection in C#. I have been made aware of the drawing ability in WPF which you do from C# using the Windows.Me

  • Using dynamic parameters in email publisher subjectSettings block with CruiseControl.Net 2010-06-10

    I am trying to get dynamic parameters to be used in the email publisher's subjectSettings block. For example, <project> ... <parameters> <textParameter> <name>version</name> <display>Version to install</display

  • Is the adapter pattern usable in cases where the different interface methods have varying parameters 2010-07-15

    I am creating a client side swing app that will have data provided by/from one of many data providers(brokers). The data providers however, have varying ways of perfoming same things e.g. broker1's login method public boolean doLogin(String username,

  • LinqDataSource dynamic parameters - forcing an OR with WhereParameters 2010-07-22

    In my "selecting" statement I need to add two dynamic parameters when using the LinqDataSource's WhereParameters collection: e.WhereParameters.Add(param, True) However, the system adds these parameters as AND, but I want to perform an OR where either

  • Spring lookup method injection with parameters 2010-12-02

    Is there a way to use Spring lookup method inject with parameters? For example, I want to be able to instantiate prototype-scoped beans while passing them arbitrary parameters via constructor. --------------Solutions------------- You can inject them

  • How to create a delegate a method with 3 parameters in C#2? 2010-12-09

    I have the following method : private void WriteTrace(object sender, EventArgs e, EventElement eventElement) { /* ... */ } When I want to create Delegate like this : Type controlType = control.GetType(); MethodInfo method = typeof(Trace).GetMethod("W

  • Crystal Reports dynamic parameters values 2011-02-08

    I have crystal report with dynamic parameter (for example, linked on table COUNTRIES in some datebase). How can i programmatically get allowed values for this parameter (list of countries)? For static i can get default values using : ReportDocument r

  • Mock static method with no parameters in Groovy 2011-02-22

    I need to mock a static method. I'm using the EMC approach described at Mocking static methods using groovy. Like this TestDaemon.metaClass.'static'.newDownloadManager = {downloadManager} The method newDownloadManager has no parameters and for some r

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.226 (s). 11 q(s)