C # Functions inside a button to calculate distance

(I recently became acquainted with the world of C # coding.) I am currently trying to work using functions in C #. The goal of the program is to try to calculate the average using total distance traveled and total hours traveled , which will be multiplied by the time to get from New York City to MIAMI to get the distance from New York City to MIAMI.

How can I correctly perform functions (content-wise) to give me the result (distance from the city of New York to MIAMI)? Should I make the double void function and declare them public or private?

4 Text Files:

 Starting Mileage Ending Mileage Total Driving Time Time from NY city to MIAMI 

I put my functions to be performed at the click of a button. A brief idea on how I plan to use functions:

  private void button1_Click(object sender, EventArgs e) { double v = Calculate_Velocity(); double t = Get_Time(); double d = Calculate_Distance (v,t); Display_Results(v, t, d); Clear_TextBoxes(); } 
+4
source share
4 answers

You want the functions to be closed or protected. The difference between private and protected is that private can only be accessed by code in a class or structure, and protected can only be used in the current class and any class derived from it.

This is a link to a good explanation of access modifiers in CSharp.

Your code should look something like this:

 private double CalculateVelocity() { double distance = 0; double time = 0; if(double.TryParse(tbDistance.Text, out distance) && double.TryParse(tbTime.Text, out time)) { return distance/time; } return 0.0; } private double GetTime() { //Get Time } private double CalculateDistance(double velocity, double time) { //Calculate Distance } private double DisplayResults(double velocity, double time, double distance) { //Display Results } private double ClearTextboxes() { //Clear textboxes } private void button1_Click(object sender, EventArgs e) { //You get the idea } 

Since you're new to csharp, it's probably time to try using properties. For example, GetTime can be written as follows:

 // Property to GetTime public double GetTime { get { // variable to hold time double time = double.MinValue; // Safely parse the text into a double if(double.TryParse(tbTime.Text, out time)) { return time; } // Could just as easily return time here return double.MinValue; } set { // Set tbTime tbTime.Text = value.ToString(); } } 

And you can use the property as follows:

 double time = GetTime; 

or

 GetTime = time; 

You can also use TimeSpan instead of double for GetTime.

+3
source

How can I correctly perform functions (content-wise) to give me the result (distance from New York city to MIAMI)?

Firstly, there is no absolutely correct answer, and everyone will have their own opinion. In my opinion, I would suggest adding a new class to your project.

 class Formulas { public static double CalculateVelocityFromMileage(double startMileage, double endMileage, double time) { return (endMileage - startMileage) / time; } } 

This isolates the logic for computing speed, distance, and average time to a particular place and makes no assumptions about what you call TextBox controls or how the user should enter information in these text fields.

You must make these functions static. You should think of a static function as one that requires only the input provided in the function definition. A static function is good, it does not depend on elements that are hidden from the kind of person calling your function. Of course, for static functions you have no choice but to return to double.

And you must make the function public, so the code in the click event can call it like this:

 Formulas.CalculateVelocityFromMileage(start, end, time); 

Remember the following:
1. The function should do one thing and do one thing well 2. The function should not rely on hidden values, its output should be based only on its arguments.

You may be able to write a little more code, but you will thank yourself when you start writing more code as your application grows.

But trust your own instincts and find your own style and find out what works for you.

+2
source

since I don’t know the formulas, I’ll just give you an idea and you can create an application accordingly

just define the function inside the class as

 double i=0; public double Calculate_Velocity() { i=double.parse(Starting Mileage.Text)*double.parse(TotalDrivingTime.Text ); return i; //Formula may be wrong but you will get idea abt the things... } 

Define all functions in the same way ...

0
source

Choosing double is good if you need fractional miles. If you want to get a result, for example, 1500 or 1837 miles, then int is the way to go. By declaring it double , you will get the result back as 1500.33 miles (this will be 1500 miles and 1/3 miles). Thus, the choice is up to you if you want fractional miles (or rather) or a whole mile number.

For your program, I would make the functions private . Creating private means that functions cannot be called by instantiating an instance of your class. In other words, the end user only wants the end result. The end user does not have any business functions calling your "helper" functions.

Just like FYI, you did not ask, but there is a third option for visibility called protected . protected works the same as private . The difference is that if your class is inherited by another class, this class has access to your protected functions and variables.

0
source

Source: https://habr.com/ru/post/1436052/


All Articles