Click here to Skip to main content
16,016,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a class named Test

C#
 public class Test
    {
        public string type { get; set; }
        public string self_url { get; set; }
        public string summary_url { get; set; }
        public string description { get; set; }
        public string location_description { get; set; }
        public List<object> images { get; set; }
        public string integration_id { get; set; }
        public bool realtime_server_url_is_public { get; set; }
        public string row_id { get; set; }
        public string auction_type { get; set; }
        public string last_updated { get; set; }
        public string time_start { get; set; }
}



And I want to remove the row_id field if it is empty.
I have done the below code and its only removing the contents. what I want is that the row_id field should not seen in the class if it is empty.

Here id is the code


C#
Test lot=new Test();
if (lot.row_id  == "")
{
lot.row_id .Remove(0); 
}
Posted
Updated 18-Sep-15 6:52am
v3
Comments
Afzaal Ahmad Zeeshan 18-Sep-15 8:49am    
Your question is unclear, you say you have a class named "Test" whereas in your code it is "Response". Also, in the code you are not at all testing the row_id. You are testing against coin_grade, what is that?

Please edit the post and make it more clear. Thank you. :-)
Richard Deeming 18-Sep-15 13:53pm    
C# is a static language. You can't add or remove properties to a class at runtime in a static language.

Perhaps if you explain what you're actually trying to do, we might be able to suggest a different approach.
Sergey Alexandrovich Kryukov 18-Sep-15 18:52pm    
This is not how .NET works.
—SA
Ralf Meier 20-Sep-15 10:53am    
You can't remove Properties from a class. But what you can do is removing the Property from the PropertyGrid. But this also only makes sense or is realizeable by the value from another Property - not by it's own value.
So ... what exactly do you want to do ... (and why do you want to do it) ?

1 solution

To test if the field isn't populated you can use

C#
if (!string.IsNullOrWhiteSpace(yourclass.row_id))


or change the type to "string?", but that will probably involve other changes in your code too.
 
Share this answer
 
Comments
Richard Deeming 18-Sep-15 13:48pm    
You can't use reference types with Nullable<>, so string? isn't a valid type.

(There's a proposal related to this[^] for C# 7, but there's no guarantee that it will make the cut.)

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