473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Call a function

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
Nov 16 '05 #1
7 1189
Steven <counterball_20122@_hotmail.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steven <counterball_20122@_hotmail.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
>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.

Nov 16 '05 #4
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" <br*******@canada.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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.

Nov 16 '05 #5
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.

---------
Ajay Kalra
aj*******@yahoo.com

Nov 16 '05 #6
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.

Nov 16 '05 #7
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 anobject and read from that?

- -Praveen
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@l41g2000cwc.googleg roups.com

....
>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.

.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
0
by: Mike S | last post by:
I've seen examples of using the CallWindowProc Windows API function to call functions through their addresses in VB6 -- a clever workaround to emulate C-like function pointer semantics. A...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.