Call a function

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

I'm novice in C#. Here is a basic question --
I have 4 variables a, b, c, d in class main(). I want to perform all the
mathematical operations on these variables in a function called maths() and
get the values back to main. How can I achieve this?


Thanks
Steven
 
Steven said:
I'm novice in C#. Here is a basic question --
I have 4 variables a, b, c, d in class main().

Do you mean in the Main method?
I want to perform all the mathematical operations on these variables
in a function called maths() and get the values back to main. How can
I achieve this?

I'd suggest encapsulating the variables in a class, then pass an
instance of the class to the Maths() method (or even put the Maths
method in the encapsulating class).
 
Heres a very quick example if I understand your question. This is done with
a console application.

using System;

namespace test

{

class Class1

{

public string myItem;

static void Main(string[] args)

{

string a1, b1, c1, d1;

a1 = "a";

b1 = "b";

c1 = "c";

d1 = "d";

Class1 myClass = new Class1();

myClass.Math(a1,b1,c1,d1);

}

public string Math(string a1,string b1,string c1,string d1)

{

myItem = a1 + b1 + c1 +d1;

Console.WriteLine(myItem);

return myItem;

}

}

}

Hope it helps

MikeY
 
From the tone of your question, I think that you want to pass four
variables into the maths() method, potentially change all four
variables, and have them be changed when you return to Main(). Here is
a sample of that:

public class MyClass
{
public static void Main(string argv[])
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
maths(ref a, ref b, ref c, ref d);
// At this point, a, b, c, and d will have new values
}

private static void maths(ref int w, ref int x, ref int y, ref int
z)
{
// Whatever you do here to w, x, y, and z
// will appear in Main as having been done
// to a, b, c, and d.
}
}

That said, this isn't very good form in .NET... it's more procedural
programming style. As Jon Skeet pointed out, the proper thing to do is
group related variables together in a class, and pass an object of that
class into maths(), or have maths() return a result of that class type.

Of course, you haven't told us what a, b, c, and d represent, so it's
hard to say whether they belong together in a class or not.
 
a,b,c,d are just variables of int type. So How can I pass all these into an
object and read from that?

- -Praveen


Bruce Wood said:
From the tone of your question, I think that you want to pass four
variables into the maths() method, potentially change all four
variables, and have them be changed when you return to Main(). Here is
a sample of that:

public class MyClass
{
public static void Main(string argv[])
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
maths(ref a, ref b, ref c, ref d);
// At this point, a, b, c, and d will have new values
}

private static void maths(ref int w, ref int x, ref int y, ref int
z)
{
// Whatever you do here to w, x, y, and z
// will appear in Main as having been done
// to a, b, c, and d.
}
}

That said, this isn't very good form in .NET... it's more procedural
programming style. As Jon Skeet pointed out, the proper thing to do is
group related variables together in a class, and pass an object of that
class into maths(), or have maths() return a result of that class type.

Of course, you haven't told us what a, b, c, and d represent, so it's
hard to say whether they belong together in a class or not.
 
You can encapsulate a,b,c etc in a class. Something like this:

internal class CMyClass
{
private:
int a;
int b;
int c;
int d;

// You can use properties
public int ValA
{ return this.a;}
....
// You can use a method
public void DoSomeOperation()
{
this.a = this.b + this.c + this.d;
}
}

You can make an object of this class as follows:

CMyClass obj = new CMyClass();
obj.DoSomeOperation();

int val = obj.ValA;

etc.
 
Yes, but what do they represent? Annual salary, net salary, etc? Or
widgets produced per hour? It's the meaning of the variables that
decides whether they belong together in a class.
 
You need simply pass the values by reference using the ref
keyword on both the calling and called side. Your maths()
function would end up looking something like:

void maths(ref int a, ref int b, ref int c, ref int d)
{
//Your math here
}

While your calling of maths() would look like:

maths(ref a, ref b, ref c, ref d);

Ordinarily when you call a function, copies of each of the
arguments are made (for numerical types at least) and any
changes made to the copies do not affect the originals,
however using the ref keyword causes the called function
to use the actual variables used in the calling and any
changes made to them will remain once the function has
returned.
-----Original Message-----
a,b,c,d are just variables of int type. So How can I pass all these into an
object and read from that?

- -Praveen


From the tone of your question, I think that you want
to pass four
variables into the maths() method, potentially change all four
variables, and have them be changed when you return to Main(). Here is
a sample of that:

public class MyClass
{
public static void Main(string argv[])
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
maths(ref a, ref b, ref c, ref d);
// At this point, a, b, c, and d will have new values
}

private static void maths(ref int w, ref int x, ref int y, ref int
z)
{
// Whatever you do here to w, x, y, and z
// will appear in Main as having been done
// to a, b, c, and d.
}
}

That said, this isn't very good form in .NET... it's more procedural
programming style. As Jon Skeet pointed out, the proper thing to do is
group related variables together in a class, and pass an object of that
class into maths(), or have maths() return a result of that class type.

Of course, you haven't told us what a, b, c, and d represent, so it's
hard to say whether they belong together in a class or not.


.
 
Back
Top