Passing though a function

How can I make a new function and pass a var. through it?

Comments

  • 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?
  • 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)"




  • [b][red]This message was edited by lyubo at 2003-5-2 9:7:4[/red][/b][hr]
    : 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 ;-)

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

  • : : 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.
  • [b][red]This message was edited by Rykon at 2003-5-2 9:51:26[/red][/b][hr]
    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.


  • [b][red]This message was edited by lyubo at 2003-5-2 10:1:34[/red][/b][hr]
    : [b][red]This message was edited by Rykon at 2003-5-2 9:51:26[/red][/b][hr]
    : 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


  • 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?
  • : 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 :)
  • 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?
  • : 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.
  • 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());
    }


  • 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());
    }


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


  • : How can I make a new function and pass a var. through it?
    :
    testje
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories