Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to update abstract class property value at run time by derived class in c#

I have following abstract class

public abstract class BaseResponse
{
 public string TestId{ get; set; }
}


I have two concrete class(TestRequest and TestResponse) derived from above abstract class

public class TestResponse: BaseResponse
    {
        
    }


public class TestRequest: BaseResponse
    {
        //some properties

    }

I have passed some value for TestId in the request parameters. As TestId is used for TestRequest and TestResponse both. when i tried to update the TestId got exception "
object reference not set to an instance of an object 
" .Following are the code

private async Task<IEnumerable<testresponse>> GetTestResponse(){
TestResponse testObject = new TestResponse();
testObject.TestId = "some data here";

}

I want to update TestId using TestResponse class, how to achieve it?

Thanks in advance!

What I have tried:

<pre>TestResponse testObject = new TestResponse();
testObject.TestId = "some data here";
Posted
Updated 15-May-17 18:47pm
v5

1 solution

The same way you update any property in an object.
C#
TestResponse testObject = new TestResponse();
testObject.TestId = "some data here";
 
Share this answer
 
Comments
ManojRGEC 15-May-17 11:01am    
Thanks for your response. I have tried same but getting error "object reference not set to an instance of an object"
Richard MacCutchan 15-May-17 12:01pm    
So are you expecting us to guess what your code is doing? Please edit your question, show the code where the error occurs and explain exactly what happens.
ManojRGEC 15-May-17 13:39pm    
Thanks for your suggestion :)
Richard MacCutchan 15-May-17 13:47pm    
There is not enough context there, you must be doing something wrong. I have tested my suggestion and it works successfully.
ManojRGEC 15-May-17 22:05pm    
but unfortunately it is not working for me. I am trying to update property value in "async await" method. so may be this is the main cause that i am not able to update it. Thanks for your help!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900