Click here to Skip to main content
15,888,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can we able to give it as a Optional variable in the below class. Because I don't want Obj1, obj2, obj3 for request type but needed for another request. Kindly suggest me.

public Obj1 IsForecasted { get; set; }
     public obj2 ? IsGenerated { get; set; }
     public obj3 ? IsPosted { get; set; }
     public long? startDate { get; set; }
     public long? endDate { get; set; }
     public int?[] s1= new int?[6] { 0, 0, 0, 0, 0, 0 };
     public int?[] s2= new int?[6] { 0, 0, 0, 0, 0, 0 };
     public int?[] s3= new int?[6] { 0, 0, 0, 0, 0, 0 };
     public int?[] s4= new int?[6] { 0, 0, 0, 0, 0, 0 };


What I have tried:

I have tried to add [Optional] as a data annotation but its not allowed because it only applicable for parameters.
Posted
Updated 7-Oct-17 4:15am

You can't have optional data in a class - a class instance is a fixed size and cannot be altered. Think about it: you have a optional value which is not there for "mode == normal" - so you create an instance of the class as a "normal" instance.
Later you change the value of mode to "advanced" and try to use the optional value. Where does the extra space come from? It can't be created because the instance is a fixed size and the space allocated did not include your optional field.
Result: your app crashes!

No, there are no optional fields in C#.
 
Share this answer
 
For what do you need such "optional Variables" ?
If your class should have this Variables for the usage 1 and needs not some Variables for usage 2 I don't understand the problem.
If you don't need the Variables then don't use them.
If the Variables (in your code-snippet I see them as Properties) should have default Values then use an internal Variable for the Property which has a Start-Value :
C#
public int BorderSize {
	get { return my_BorderSize; }
	set { my_BorderSize = value; }
}
private int my_BorderSize = 1;
 
Share this answer
 
Comments
Member 11725507 9-Oct-17 6:29am    
{ "weeks": [ {
"s1": [ 0, 0,0,0,0, 0],
"s2": [0,0,0,0,0,0],
"s3": [0,0,0,0,0,0],
"s4": [0,0,0,0,0,0],
"isForecasted": null,
"isGenerated": null,
"isPosted": null,
"startDate": null,
"endDate": null
}
]}
I have posted the response generated JSON, and Instead of showing NUll in reponse, I don't want "isForecasted","isGenerated","isPosted","startDate","endDate" object. But in another response i will pass value for the above values so it will be needed for the another scenario. I hope it would be helpful for the solution.
Ralf Meier 9-Oct-17 6:46am    
I'm sorry but your issue is unclear for me.
You have the Solution from UG which tells you what is possible and what is impossible.
If you have different scenarios you could do it with the same class and don't use some properties ... or ... you have/use different classes.
You can't use optional variables or optional properties. If you refer to a method you should know that those optional parameters are allways used - but sometimes with default values (as specified in the method-definition) - this is the difference ...
Ralf Meier 9-Oct-17 6:50am    
Question :
why don't you work with the properties as described in my Solution (use the Property with an internal Varible which holds a/the default value) ?
Member 11725507 9-Oct-17 6:56am    
Yes I can use it with a default value but I dont want to display the Property with default values also I just want to ignore it. For this we can able to achieve by using separate class but I was thinking to reuse the same class by keeping few property as optional. So now I can conclude that we cant do it right?
Ralf Meier 9-Oct-17 10:31am    
It depends ...
How or in which context do you use your class ? Is it part of the UI (is it a Control) ?

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