What is the 'Subtract Days' equivalent of DateTimePicker.Value.AddDays()? [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a DateTimePicker and two buttons on a form. The buttons are intended to allow a user to cycle backwards and forwards through the dates displayed in the picker.
The code DateTimePicker.Value.AddDays(1); increments the value displayed and DateTimePicker.Value.AddDays(-1); decrements it. It seems a bit clunky to me but this works as expected, is passing in a value of -1 the correct way to decrement the displayed date?
Why isn't there a SubtractDays() method?

As you've seen, you can use AddDays with a negative amount to subtract days, so there's no need for the extra methods (there would need to be one for each of the Add methods). If it really bothers you, you can write extension Subtract methods for all the Add methods.
For example
public static class DateTimeExtensions
{
public static DateTime SubtractDays(this DateTime start, int days)
{
return start.AddDays(-days);
}
}

Related

Naming Convention for Model Properties? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
If you have an int that represents days, what would be the best way for its name:
MinLimitInDays?
MinLimitwith "in days" as xml doc comment?
or the same with a double:
ProductRevenueInPercentage
ProductRevenue with "in percentage" as xml doc comment?
Or would you even create a new class with a specific constructor e.g. MinLimit(int days) that has a property int days?
MSDN states that you should value readability over brevity.
So:
int MinLimitInDays = 4;
Would be better represented as:
int MinimumAmountOfDays = 4;
Better yet, save it as a TimeSpan
TimeSpan MinimumLimit = TimeSpan.FromDays(4);
With everything else, if you're not sure, make it more verbose.
double ProductRevenuePercentage;
is better than:
double ProductRevenue;
which could be the overall revenue. Don't be afraid of making properties too long, and don't abbreviate unless the abbreviation is ubiquitous.

Convert.ToInt32("example") Collisions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Will the C# function Convert.ToInt32(text) convert different words to the same int? Any links or pushes in the right direction are appreciated!
P.S.
What about anagrams?
No, Convert.ToInt32(text) will just try to parse your text to an int, like:
Convert.ToInt32("032") will return 32 as int but
Convert.ToInt32("Brian") will throw an exception.
I assume that you want to have some kind of hashing, when you say "different words to the same int".
Try GetHashCode(). It will return the same value if you call it multiple times with the same value, for example:
"Brian".GetHashCode() will always return 1635321435

Back to the beginning of the code with several events? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm a beginner in C# and I make a quiz. My problem it is that when a question is asked that should make a loop so that the second question can be asked. I had the idea to use goto or return to turn over to the beginning but I have several events of clicks.
Here is an example:
private void Event1
{
//do something..
}
private void Event2
{
//do other things
}
private void Event3
{
//Here I want to return to the event 1 to make a loop
}
Is that possible?
If no are there other solutions?
I assumed that you want to bulid 3 question in series, enable the next options when current question is answered correctly.
If I'm right, first of all you should enable the first button only.
When first event raised and the answer was right , disable first button, then enable next one,so on until all questions were finished.

Should the currecy textbox in winforms gets rounding done all the time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have standard windows textbox that is formatted for currency but there is minor issue, when the user enter a value like 19999.9999999, this get rounded to 20000.00. If the user enters the value as 19999.99 this value will not get rounded. In the big industry, what is the correct way to format this number?
I need this to be rounded without harming the property value!
How about Math.Round.
try like this
decimal dValue = Math.Round(19999.999999M, 3);
Take a look at the msdn for complete reference of Math.Round() method.

What is the diffrence beetween Days and TotalDays? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anybody tell me what is the difference between these two functions in C#? TotalDays and Days because I'm not sure which once I should use in my code? Sorry for the low information on this text, but there is not much I can talk about.
Since i haven't found a duplicate i post my comment here:
Always read the documentation first. TotalDays is a double because it represents whole and fractional days whereas Days is an int which represents only whole days.
That is even mentioned explicitly in the remarks sections of TimeSpan.Days/TotalDays:
The Days property represents whole days, whereas the TotalDays
property represents whole and fractional days.
One thing to note, as opposed to the other properties in TimeSpan like Hours/TotalHours there is no limit on Days. So it doesn't end with 30 or 365(like Hour which ranges from -23 through 23) since there is no larger unit than year. So Days will always be the same number as (int) ts.TotalDays.
A TimeSpan doesn't have a sensible concept of "years" because it
depends on the start and end point. (Months is similar - how many
months are there in 29 days? Well, it depends...) [J. Skeet]

Categories

Resources