C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing though a function Posted by Rykon on 1 May 2003 at 11:14 AM
How can I make a new function and pass a var. through it?
Report
Re: Passing though a function Posted by Brutes on 1 May 2003 at 11:24 AM
Hi Rykon

They called Methods Rykon.

I am not sure how you mean make a function?
Can you tell me what it is you need to do?
You need a basic structure of a method and how to call it?
Report
Re: Passing though a function Posted by Rykon on 2 May 2003 at 8:06 AM
My question is for this type of example in C++

Int myfunction(int hi)
{
int j;
j = hi + 2;

Return hi;
}
then calling it
int h = 4;
myfunction(h)

I can not get the "myfunction(h)" to pass into "myfunction9int (hi)"




Report
Re: Passing though a function Posted by lyubo on 2 May 2003 at 9:03 AM
This message was edited by lyubo at 2003-5-2 9:7:4

: My question is for this type of example in C++
:
: Int myfunction(int hi)
: {
: int j;
: j = hi + 2;
:
: Return hi;
: }
: then calling it
: int h = 4;
: myfunction(h)
:
: I can not get the "myfunction(h)" to pass into "myfunction9int (hi)"
keke, in the example you provided, you return the same variable you've put as parameter, unmodifyed.
Anyway I've, a little bit, forgotten c++, try calling myfunction from other function. Is myfunction in the global(is global function) namespace?

Furthermore, you should try addressing your question in the C++ Forum

Report
Re: Passing though a function Posted by Rykon on 2 May 2003 at 9:16 AM
: keke, in the example you provided, you return the same variable you've put as parameter, unmodifyed.
: Anyway I've, a little bit, forgotten c++, try calling myfunction from other function. Is myfunction in the global(is global function) namespace?
:
: Furthermore, you should try addressing your question in the C++ Forum
:

First I was doing a compare to c++ and the code was psuedo. You where no help to my question, read the first post.

Report
Re: Passing though a function Posted by lyubo on 2 May 2003 at 9:17 AM
: : keke, in the example you provided, you return the same variable you've put as parameter, unmodifyed.
: : Anyway I've, a little bit, forgotten c++, try calling myfunction from other function. Is myfunction in the global(is global function) namespace?
: :
: : Furthermore, you should try addressing your question in the C++ Forum
: :
:
: First I was doing a compare to c++ and the code was psuedo. You where no help to my question, read the first post.
:
:
Sorry, thought, I didn't see the comparement.
Report
Re: Passing though a function Posted by Rykon on 2 May 2003 at 9:50 AM
This message was edited by Rykon at 2003-5-2 9:51:26

That is ok, I was giving an example in c++ and I was wonder the C# equiv. Might I add I need it to work with a static and an object.


Report
Re: Passing though a function Posted by lyubo on 2 May 2003 at 9:57 AM
This message was edited by lyubo at 2003-5-2 10:1:34

: This message was edited by Rykon at 2003-5-2 9:51:26

: That is ok, I was giving an example in c++ and I was wonder the C# equiv. Might I add I need it to work with a static and an object.
:
:
:
I'll try to give an example of what I think you are trying to express (hopefully, if we are both lucky, i will not be wrong ;))
using System;
namespace MyTestSpace
// namespace
{
// simple SHOWOFF class
public class WhatACalculator
// class
{
// simple static method
public static int Add(int fNum,int lNum)
// method
{
// add the two values and return the result;
return fNum+lNum;
} // end method
static void Main()
{ // method
int myAge=18; // yes i'm 18
int myGirlFriendsAge=16; // no, my girlfriend's is not 16
int myMomsAge= // because my mom's age is 35
// calling the static add method, we don't need an object instance forIt
WhatACalculator.Add(myAge,myGirlFriendsAge);
// display the result from the Add method usage;
Console.WriteLine(myMomsAge.ToString());
} // end method
} // end class
} // end namespace


Report
Re: Passing though a function Posted by Rykon on 2 May 2003 at 5:30 PM
There is one problem with this code, it is not that same (to my knowlege) as one needed for a Windows app, not a console app. The main diff that I can find is the passing through an object(i.e. no static). Could you be any help on that?
Report
Re: Passing though a function Posted by lyubo on 3 May 2003 at 5:10 AM
: There is one problem with this code, it is not that same (to my knowlege) as one needed for a Windows app, not a console app. The main diff that I can find is the passing through an object(i.e. no static). Could you be any help on that?
:
First - yes this is console demo app.
Second what help exactly do you need, i'm confused now :)
Report
Re: Passing though a function Posted by Rykon on 3 May 2003 at 1:15 PM
Ok, I can make a console app and get methods to work, but when I try the same tactic on a windows app it will not work. The only difference that I can see it that I am trying to pass from a object(i.e. a method for a button) to a static method. Any idea of what I am talking about now?
Report
Re: Passing though a function Posted by Brutes on 4 May 2003 at 2:42 AM
: How can I make a new function and pass a var. through it?
:
Ok.

Look every thing is a class right and when you instantiate a class it becomes an object.
When you make a method static you use the class name and the dot notation to indicate witch static method to use like so:

public class MyClass
{
static string MakeComent()
{
return Hi;
}
}

public class MyDriver
{
public MyDriver()
{

}

public static void Main(string[] args)
{
Console.Out(MyClass.MakeComment());
}
}

PS. I am using a console app for now.
Report
Re: Passing though a function Posted by Brutes on 4 May 2003 at 2:49 AM
Now when you make the class an object things change.
Your first class needs a constructor first of all.

public class MyClass
{
public MyClass()
{
}

public string MakeComent()
{
return Hi;
}
}

The Driver class Main Instantiates the Class and Calls the method through the Object:

public static void Main(string[] args)
{
MyClass myNewObject = new MyClass();
Console.Out(myNewObject.MakeComment());
}


Report
Re: Passing though a function Posted by Brutes on 4 May 2003 at 3:47 AM
Now when you use a Windows app, one has to be careful not to get mixed up between the driver and your class. The code window presented to you at start-up is the driver and I suggest you add your custom class to the project You should follow the same approach in the console app by the way

You will then have a choice between using a global instance and a local instance in the event handler methods.

In the former you would instantiate the object in the Main method while you would need a global reference together with the other form object references

Using MyClass;
public class form1
{
private System.ComponentModel.Container components = null;
private MyClass myNewObject = null;

public static void Main()
{
myNewObject = new MyClass();
}
}

In the Later one would just instantiate a local instance inside the EventHandler method:

private void button1_Click(object sender, System.EventArgs e)
{
MyClass myNewObject = new MyClass();
MessageBox.show(MyNewObject.MakeComment());
}


Report
Re: Passing though a function Posted by Brutes on 4 May 2003 at 3:57 AM
When you want to pass parameters:

private string MyMethod(int times)
{
return Its the + times.ToString() + th Time;
}

public static void Main()
{
for(int I; I < 9; I++)
{
Console.Out(MyNewObject.MyMethod(I));
}
}

PS non of my code has been tested, but you will be able to see what to do.
Ask if you still dont understand.


Report
Re: Passing though a function Posted by joekske on 6 May 2003 at 2:06 AM
: How can I make a new function and pass a var. through it?
:
testje



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.