Object member as a parameter of a function in C# [duplicate]












0
















This question already has an answer here:




  • Update a property on an object knowing only its name

    3 answers



  • How to set property value using Expressions? [duplicate]

    1 answer



  • How set value a property selector Expression<Func<T,TResult>>

    6 answers




I want to know how to put object member as a parameter of a function



 class Student
{
public int id;
public string name;
public string year;
public Student(string i,string n,string y){
id=i;
name=n;
year=y;
}
}
class main{
Student s=new Student(1,"NAME","YEAR");
private static void Main(string args)
{

change();
}
void change(){
s.name="CHANGED";
}
}


This code may not have any point since this is just an example. All I want to know is how to put object member as a parameter. On void change(), I want to put a parameter to replace name, so that I can us this function to change both name and year. Example: Instead of void change() above:



  void change(string m)//using string is not working
{
s.m="CHANGED";
}


On main function:



     private static void Main(string args)
{
change(name);//s.name is now equal to CHANGED
change(year);//s.year is now equal to CHANGED
}


is it possible to do this?
THANK YOU










share|improve this question













marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 9:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

    – CodeCaster
    Nov 20 '18 at 9:47













  • I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

    – Teng Vanndavid BLiZzaRD
    Nov 20 '18 at 10:21
















0
















This question already has an answer here:




  • Update a property on an object knowing only its name

    3 answers



  • How to set property value using Expressions? [duplicate]

    1 answer



  • How set value a property selector Expression<Func<T,TResult>>

    6 answers




I want to know how to put object member as a parameter of a function



 class Student
{
public int id;
public string name;
public string year;
public Student(string i,string n,string y){
id=i;
name=n;
year=y;
}
}
class main{
Student s=new Student(1,"NAME","YEAR");
private static void Main(string args)
{

change();
}
void change(){
s.name="CHANGED";
}
}


This code may not have any point since this is just an example. All I want to know is how to put object member as a parameter. On void change(), I want to put a parameter to replace name, so that I can us this function to change both name and year. Example: Instead of void change() above:



  void change(string m)//using string is not working
{
s.m="CHANGED";
}


On main function:



     private static void Main(string args)
{
change(name);//s.name is now equal to CHANGED
change(year);//s.year is now equal to CHANGED
}


is it possible to do this?
THANK YOU










share|improve this question













marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 9:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

    – CodeCaster
    Nov 20 '18 at 9:47













  • I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

    – Teng Vanndavid BLiZzaRD
    Nov 20 '18 at 10:21














0












0








0









This question already has an answer here:




  • Update a property on an object knowing only its name

    3 answers



  • How to set property value using Expressions? [duplicate]

    1 answer



  • How set value a property selector Expression<Func<T,TResult>>

    6 answers




I want to know how to put object member as a parameter of a function



 class Student
{
public int id;
public string name;
public string year;
public Student(string i,string n,string y){
id=i;
name=n;
year=y;
}
}
class main{
Student s=new Student(1,"NAME","YEAR");
private static void Main(string args)
{

change();
}
void change(){
s.name="CHANGED";
}
}


This code may not have any point since this is just an example. All I want to know is how to put object member as a parameter. On void change(), I want to put a parameter to replace name, so that I can us this function to change both name and year. Example: Instead of void change() above:



  void change(string m)//using string is not working
{
s.m="CHANGED";
}


On main function:



     private static void Main(string args)
{
change(name);//s.name is now equal to CHANGED
change(year);//s.year is now equal to CHANGED
}


is it possible to do this?
THANK YOU










share|improve this question















This question already has an answer here:




  • Update a property on an object knowing only its name

    3 answers



  • How to set property value using Expressions? [duplicate]

    1 answer



  • How set value a property selector Expression<Func<T,TResult>>

    6 answers




I want to know how to put object member as a parameter of a function



 class Student
{
public int id;
public string name;
public string year;
public Student(string i,string n,string y){
id=i;
name=n;
year=y;
}
}
class main{
Student s=new Student(1,"NAME","YEAR");
private static void Main(string args)
{

change();
}
void change(){
s.name="CHANGED";
}
}


This code may not have any point since this is just an example. All I want to know is how to put object member as a parameter. On void change(), I want to put a parameter to replace name, so that I can us this function to change both name and year. Example: Instead of void change() above:



  void change(string m)//using string is not working
{
s.m="CHANGED";
}


On main function:



     private static void Main(string args)
{
change(name);//s.name is now equal to CHANGED
change(year);//s.year is now equal to CHANGED
}


is it possible to do this?
THANK YOU





This question already has an answer here:




  • Update a property on an object knowing only its name

    3 answers



  • How to set property value using Expressions? [duplicate]

    1 answer



  • How set value a property selector Expression<Func<T,TResult>>

    6 answers








c# function object parameters






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 9:45









Teng Vanndavid BLiZzaRDTeng Vanndavid BLiZzaRD

1




1




marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 9:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by CodeCaster c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 9:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

    – CodeCaster
    Nov 20 '18 at 9:47













  • I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

    – Teng Vanndavid BLiZzaRD
    Nov 20 '18 at 10:21














  • 1





    Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

    – CodeCaster
    Nov 20 '18 at 9:47













  • I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

    – Teng Vanndavid BLiZzaRD
    Nov 20 '18 at 10:21








1




1





Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

– CodeCaster
Nov 20 '18 at 9:47







Using reflection, or using a property expression. But why would you want this? What problem are you trying to solve?

– CodeCaster
Nov 20 '18 at 9:47















I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

– Teng Vanndavid BLiZzaRD
Nov 20 '18 at 10:21





I am try to sort out an object list(List in a class). Since the object list I want to sort has many variable and I want to sort by all of it, I want to create a function that can sort by every member of that object.

– Teng Vanndavid BLiZzaRD
Nov 20 '18 at 10:21












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory