Hi,
In the programme I am creating I am trying to break down the code into functions to make it elegant. However, when doing so one of my functions should return 5 values. As it stand I cannot find anything about returning multiple values using functions. I was wondering if this was possible and if so how? I tried using an array but I may have failed in doing so properly. Anyway below is the code if it makes my question any easier to understand.
Thank you very much
Cris
btw code below is not functioning due to return issue...
static
double output(double fbot, double fmax, double xbot, double xmax)
{
double keff = fbot / xbot;
double x2 = (xmax - xbot) / 2;
double x1 = xmax - x2;
double k1 = 0;
double k2 = 0;
//double[] values = new double[5];
int i = 0;
while (i == 0)
{
k2 = fmax / x2;
k1 = (k2 * keff) / (k2 - keff);
x1 = (k2 / (k1 + k2)) * xbot;
double x2old = x2;
x2 = xmax - x1;
double change = x2 / x2old;
if (0.99999 < change && change < 1.00001)
{
i = 1;
}
//values[1] = x1;
//values[2] = x2;
//values[3] = k1;
//values[4] = k2;
//values[5] = keff;
}
return(x1,x2,k1,k2,keff)
/*
x1box.Text = x1.ToString();
x2box.Text = x2.ToString();
k1box.Text = k1.ToString();
k2box.Text = k2.ToString();
keffbox.Text = keff.ToString();
*/
}
| | cristoball50 Saturday, October 17, 2009 6:07 PM | Ask yourself the question... "What does the data I want to return represent?" It looks to me like it might represent something like a line or something. Create a new class type that would contain the data you want to return, and return the data via that object. public class SomeClass { public double X1 { get; set; } public double X2 { get; set; } public double K1 { get; set; } public double K2 { get; set; } public double Keff { get; set; } } public SomeClass output(double fbot, double fmax, double xbot, double xmax) { // same general things.... return new SomeClass { X1 = x1, X2 = x2, K1 = k1, K2 = k2, Keff = keff }; } Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Proposed As Answer byjgalley Saturday, October 17, 2009 6:57 PM
- Marked As Answer bynobugzMVP, ModeratorSaturday, October 17, 2009 7:18 PM
-
| | David M Morton Saturday, October 17, 2009 6:11 PM | First, you'd have to create a new class to carry the two values. You'd want this class to represent whatever you're trying to return. If you're only creating it for one purpose (the purpose of returning it from the method) consider making two separate calls. Sometimes that makes more sense. In any case, let's call the class you create to return "Point". There's already a Point in the .NET Framework, and it takes an X and Y coordinate, so this would make sense:
public class Point
{
public double X { get; set; }
public double Y { get; set; }
}
Basically, this is a class that contains two properties, X and Y. These two properties are the properties whose values need to be set in an instanceof the class, which you will then return from the method.
Now let's start looking at how to write your method. Note that this section below is the method, not the class. The method will have to reside in a class, because methods in C# cannot reside outside a class. I'm assuming you already have a class. Here's what the signature of the method would look like:
public Point GetPoint(double a, double b)
Note the return value is "Point". This means you're going to be returning an instance of a point from this method. Also, we have two incoming parameters, a and b, and they're both of the type "double". Lastly, notice the access modifieron the method. It's "public". This means that the places you can get to the method from are limited only by the access modifier placed on the class. If the access modifier of a method is missing, it's private by default, but in our example it's public.
Now that we have our method signature, let's create some brackets for the body of the method, and add some code...
public Point GetPoint(double a, double b)
{
Point result = new Point();
result.X = a * b;
result.Y = a / b;
return result;
}
Note what we're doing here. First, we declare a Point. That's the section that reads "Point result". Next, we assign the value of result to a newly instantiated (created) Point (new Point()). If we didn't assign the newly instantiated point to the variable called "result", then result would be null, and we would not be able to assign X and Y.
Next, we calculate a*b and assign it's result to be the value of the X property on our result variable, and we do the same for a/b.
Finally, we return the result back to the caller.
Now, in order to call this method, we have to create a variable to hold the result. This variable would have to be the same kind of variable as the return value. In other words, the variable you store the result in has to be a variable of type Point, which would mean you'd declare the variable as Point.
Point value = GetPoint(1.43, 2.86);
Now, you can access the values like this:
Console.WriteLine(value.X);
Console.WriteLine(value.Y);
Hopefully that makes sense. I've tried to make it as simple as possible to teach the language without starting into some of the more advanced concepts surrounding object oriented code. Learning C# is like learning English. Learning object oriented programming is like learning to write English well. Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer bycristoball50 Sunday, October 18, 2009 10:49 PM
-
| | David M Morton Sunday, October 18, 2009 6:54 PM | That depends on what the code you're calling actually does. If it only interacts with the UI, leave it in the UI. If it is a separate kind of calculation, put it in another method. Try to think about what your application is doing, in real world terms, and think about their interactions, properties, etc, and then create those classes.
I strongly suggest looking up and buying the book "The Object-Oriented Thought Process".
As for style - you can pick that up by reading other people's code typically. Stick around long enough, you'll start to notice patterns. Watch how people like myself, Rudedog2, nobugz, John Grove, Reed Copsey, and a few others around here write their code. You'll start to see some similarities. :) Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer bycristoball50 Sunday, October 18, 2009 11:46 PM
-
| | David M Morton Sunday, October 18, 2009 11:25 PM | Ask yourself the question... "What does the data I want to return represent?" It looks to me like it might represent something like a line or something. Create a new class type that would contain the data you want to return, and return the data via that object. public class SomeClass { public double X1 { get; set; } public double X2 { get; set; } public double K1 { get; set; } public double K2 { get; set; } public double Keff { get; set; } } public SomeClass output(double fbot, double fmax, double xbot, double xmax) { // same general things.... return new SomeClass { X1 = x1, X2 = x2, K1 = k1, K2 = k2, Keff = keff }; } Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Proposed As Answer byjgalley Saturday, October 17, 2009 6:57 PM
- Marked As Answer bynobugzMVP, ModeratorSaturday, October 17, 2009 7:18 PM
-
| | David M Morton Saturday, October 17, 2009 6:11 PM | Hi David, Thank you for the reply. I am unfortunately new to C#, had it for less than 1 week. As such I still have to learn about classes, so am not 100% sure of how this would work and how to implement it into my code. However, I will learn fast and should be able to soon! Thank you for the help! Cris Btw, the x values are random points on a graph. the ks are the slopes and keff is the average slope.
| | cristoball50 Saturday, October 17, 2009 7:07 PM | Btw, the x values are random points on a graph. the ks are the slopes and keff is the average slope.
Having to explain that is a problem. Pick meaningful names for your identifiers. Use "point", "slope", "averageSlope". IntelliSense saves you the hassle of typing it out.
Hans Passant. | | nobugz Saturday, October 17, 2009 7:34 PM | That is a fair point... I have just done the tutorial on Object oriented programming/classes, and am still struggling to quite get what is being said. I have managed to pass one variable into the class and return one say that is double the value. But the multiple value returning has got me. I assume the output line does it but just because I do not know how to put it in, where to put the calculation part... I am confused. So maybe to make it easier can someone guide me through this simpler fictitious example. The user has inputted a value for 'a' and 'b' and we desire to calculate x and y with a class. x = a*b y = a/b This requires the class to return two values, ofcourse 2 operations could be carried out and one could refer to this by class.method1 ... but if I wanted to first carry a and b into the class, then perform both operations and then return the two values, what code would I put in the main body and in the class? If anyone could answer this I would be very grateful ! I am struggling and a simple example should do the trick. Thank you Cris btw: for the sake of this example I am using a, b, x & y. for the real coding I will keep my names elegant!
| | cristoball50 Sunday, October 18, 2009 6:00 PM | First, you'd have to create a new class to carry the two values. You'd want this class to represent whatever you're trying to return. If you're only creating it for one purpose (the purpose of returning it from the method) consider making two separate calls. Sometimes that makes more sense. In any case, let's call the class you create to return "Point". There's already a Point in the .NET Framework, and it takes an X and Y coordinate, so this would make sense:
public class Point
{
public double X { get; set; }
public double Y { get; set; }
}
Basically, this is a class that contains two properties, X and Y. These two properties are the properties whose values need to be set in an instanceof the class, which you will then return from the method.
Now let's start looking at how to write your method. Note that this section below is the method, not the class. The method will have to reside in a class, because methods in C# cannot reside outside a class. I'm assuming you already have a class. Here's what the signature of the method would look like:
public Point GetPoint(double a, double b)
Note the return value is "Point". This means you're going to be returning an instance of a point from this method. Also, we have two incoming parameters, a and b, and they're both of the type "double". Lastly, notice the access modifieron the method. It's "public". This means that the places you can get to the method from are limited only by the access modifier placed on the class. If the access modifier of a method is missing, it's private by default, but in our example it's public.
Now that we have our method signature, let's create some brackets for the body of the method, and add some code...
public Point GetPoint(double a, double b)
{
Point result = new Point();
result.X = a * b;
result.Y = a / b;
return result;
}
Note what we're doing here. First, we declare a Point. That's the section that reads "Point result". Next, we assign the value of result to a newly instantiated (created) Point (new Point()). If we didn't assign the newly instantiated point to the variable called "result", then result would be null, and we would not be able to assign X and Y.
Next, we calculate a*b and assign it's result to be the value of the X property on our result variable, and we do the same for a/b.
Finally, we return the result back to the caller.
Now, in order to call this method, we have to create a variable to hold the result. This variable would have to be the same kind of variable as the return value. In other words, the variable you store the result in has to be a variable of type Point, which would mean you'd declare the variable as Point.
Point value = GetPoint(1.43, 2.86);
Now, you can access the values like this:
Console.WriteLine(value.X);
Console.WriteLine(value.Y);
Hopefully that makes sense. I've tried to make it as simple as possible to teach the language without starting into some of the more advanced concepts surrounding object oriented code. Learning C# is like learning English. Learning object oriented programming is like learning to write English well. Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer bycristoball50 Sunday, October 18, 2009 10:49 PM
-
| | David M Morton Sunday, October 18, 2009 6:54 PM | Thank you David! That was a very helpful reply. I have managed to get that working and I will now create a class for all my data... Unless I understood wrong the method is now in the main code which I have named as form1.cs. Speaking of OOP I was wondering about improving the current techniques I am using. Basically, in form1.cs I have a code that executes when the button is pressed. This code calls on a few methods just like the one you have created above. The methods I have taken outside the private void button1_Click(object sender, EventArgs e) but within the public partial class Form1 : Form I was wondering if there would be a more elegant place to put these methods. So for example create a new method, and store the code away in a different file, that way my main code looks less messy? Thank you again! Cris
| | cristoball50 Sunday, October 18, 2009 10:49 PM | That depends on what the code you're calling actually does. If it only interacts with the UI, leave it in the UI. If it is a separate kind of calculation, put it in another method. Try to think about what your application is doing, in real world terms, and think about their interactions, properties, etc, and then create those classes.
I strongly suggest looking up and buying the book "The Object-Oriented Thought Process".
As for style - you can pick that up by reading other people's code typically. Stick around long enough, you'll start to notice patterns. Watch how people like myself, Rudedog2, nobugz, John Grove, Reed Copsey, and a few others around here write their code. You'll start to see some similarities. :) Coding Light - Illuminated Ideas and Algorithms in SoftwareCoding Light Wiki � LinkedIn � ForumsBrowser- Marked As Answer bycristoball50 Sunday, October 18, 2009 11:46 PM
-
| | David M Morton Sunday, October 18, 2009 11:25 PM |
|