locked
how to include/call funcions from another .cs file RRS feed

  • Question

  • i am starting to learn c#.

    i have multiple .cs files that hold different functions.

    i now want to create a new .cs (called Main) that can talks to all the other .cs files in the same directory and able to use those functions.

     

    overall, my question is very similar to this example below:

    example:

    i would like to have one file that has a funtion to take in X value

    i would like to have have another file that has a function to take in y value

    i would like to have have another file that has a function that take in an operation character

    now i would like to create a third file that can calls these already available files which has the X and Y and operation (+-*/) so that I can do an operation and display to screen.

    please show me a code for this

     

    thanks

     

    • Moved by Rudedog2 Monday, September 13, 2010 3:51 PM : ASP.NET is off-topic (From:Visual C# General)
    Thursday, September 9, 2010 5:36 PM

Answers

  • There is no mystery to it, you call it the same way you would call a Net class. For example let us say that you have:

    namespace MyNameSpace

    {

    class A

    {

     public void Test1(){}

    public static void Test2(){}

    }

    }

    Calling would look like

    MyNameSpace.A.Test2();//As is static there is no need to create an instance of class A

    MyNameSpace.A test=new MyNameSpace.A();

    test.Test1();//As in not static you need to create an instance of class A

    if you use:

    using MyNameSpace;// You don't need to type MyNameSpace as long as there is no other class named A the compiler can get confused with

    A test=new A();

    Regards

    • Marked as answer by Rudedog2 Friday, September 10, 2010 7:20 PM
    Thursday, September 9, 2010 6:11 PM
  • Did you add the file(s) that had this classes to the blank asp project?

    If not please do so, VS can't find them on his own

    Regards

    • Marked as answer by Rudedog2 Monday, September 13, 2010 3:51 PM
    Monday, September 13, 2010 3:47 PM

All replies

  • Why?

    I mean you can do it but...

    The cs files need to belong to the same project

    In order for the variables to hold the value you either use a single class partially divided unto the files or use static classes or properties.

    What are you trying to do?

    Regards

    Thursday, September 9, 2010 5:47 PM
  • you are right. i really mean *from the same project*

    can you show me a sample code for those?

    i am trying to utilize the already available functions from some files in the project.

    thanks

    Thursday, September 9, 2010 5:54 PM
  • There is no mystery to it, you call it the same way you would call a Net class. For example let us say that you have:

    namespace MyNameSpace

    {

    class A

    {

     public void Test1(){}

    public static void Test2(){}

    }

    }

    Calling would look like

    MyNameSpace.A.Test2();//As is static there is no need to create an instance of class A

    MyNameSpace.A test=new MyNameSpace.A();

    test.Test1();//As in not static you need to create an instance of class A

    if you use:

    using MyNameSpace;// You don't need to type MyNameSpace as long as there is no other class named A the compiler can get confused with

    A test=new A();

    Regards

    • Marked as answer by Rudedog2 Friday, September 10, 2010 7:20 PM
    Thursday, September 9, 2010 6:11 PM
  • i followed your code as above, but my code doesn't seem to work, can you help point to what i did wrong,

    its like this,

     

    i have a project. in this project i have a folder. in this folder i have three .cs files and they are TakeX.cs, TakeY.cs and Execute.cs. the code inside these three files is as follow, respectively

    1.

    using System;
    /// Take in X value to be use for calculation
    namespace TakeX
    {
        class XValue
        {
            public static int XNum()
            {
                int X = 10;
            }
           
        }
    }

    2.

    using System;
    /// Take in Y value to be use for calculation
    namespace TakeY
    {
        class YValue
        {
            public static int YNum()
            {
               int Y = 5;
            }
        }

    }

     

    3.

    using System;
    //using TakeX.XValue;
    //using TakeY.YValue;
    /// <summary>
    ///  Add the two numbers
    /// </summary>
    public class DoMath
    {
        public void Main()
        {
            XNum x = new TakeX.XValue.XNum();
            YNum y = new TakeY.YValue.YNum();
            Console.WriteLine("x + y is : " , x+y);
            Console.Read;
        }
    }

     

     

    Thursday, September 9, 2010 7:25 PM
  • First a static member do not need a instance of an object so you can actually loose the new.

    TakeX.XValue.XNum();//The order is namespace, class and then member

    Also you need to return the value on XNum and YNum so

              public static int XNum()
            {
                return 10;
            }
    And the using statement only takes namespaces not classes names so

    //using TakeX;
    //using TakeY;

    Console.WriteLine("x + y is : " + (XValue.XNum()+YValue.YNum()));//This will give you what you want

    Thursday, September 9, 2010 8:00 PM
  • Error    1    Expected class, delegate, enum, interface, or struct    C:\abc\Prac.Skill.C#\CallFuntion\TestCallFunion\TestCallFunion\Folder1\Execute.cs    9    15    TestCallFunion

     

    i followed your comments, but I still get the above error. whatelse i am doing wrong?

    After the fix, it now looks like the following:

     

    1.

    using System;
    /// Take in X value to be use for calculation
    namespace TakeX
    {
        class XValue
        {
            public static int XNum()
            {
               return 10;
            }
           
        }
    }


    2.

    using System;
    /// Take in Y value to be use for calculation
    namespace TakeY
    {
        class YValue
        {
            public static int YNum()
            {
               return 5;
            }
        }

    }

     

    3.

    using System;
    using TakeX;
    using TakeY;
    /// <summary>
    ///  Add the two numbers
    /// </summary>


    public static void Main()
    {
        TakeX.XValue.XNum();
        TakeY.YValue.YNum();
        Console.WriteLine("x + y is : " + (XValue.XNum() + YValue.YNum()));
    }

    Thursday, September 9, 2010 8:34 PM
  • TakeX.XValue.XNum();
        TakeY.YValue.YNum();

    One, this two lines, you don't need

    Two, in which line is the error occurring, and what is the code in that line

    Regards

    Friday, September 10, 2010 12:27 PM
  • using System;
    using TakeX;
    using TakeY;
    /// <summary>
    ///  Add the two numbers
    /// </summary>

    class Calculate
    {
       static Main()                                           // the error is at this line. it says "Class, struct, or interface method must have a return type"
        {
            //TakeX.XValue.XNum();
            //TakeY.YValue.YNum();
            Console.WriteLine("x + y is : " + (XValue.XNum() + YValue.YNum()));
            return;
        }
    }
    Friday, September 10, 2010 12:45 PM
  • You see methods need a return type, in the case of Main that is either int or void so:

    static int Main(){} or static void Main(){}

    Regards

    Friday, September 10, 2010 12:49 PM
  • i figured that using "static void Main()" will work. however, i don't know what is the reason behind it. but, if i put like "static int Main()" it won't work.
    Friday, September 10, 2010 12:52 PM
  • Because you are not returning an int I bet, if you put void you are saying that you  don't need anything from Main.  However if you put int then you are demanding an int as a return.

    To work you'll need to:

    return 0; instead of return;

    btw if you use void in the context of your Main method there is no need for a return line.

    Regards

    Friday, September 10, 2010 1:00 PM
  • thank Serguey123. i am really appreciate your help.

    now, i would like to display the result to a web page in the form of .aspx - how can i make this happen?

    Friday, September 10, 2010 3:57 PM
  • Just create a new WebApplication and Add a Label Control (for showing the returned text). In Page_Load of the aspx.cs write the code what is written you main method. also set Label1.Text . 
    Friday, September 10, 2010 4:02 PM
  • You do?, how kind of you.

    Now,

    "x + y is : " + (XValue.XNum() + YValue.YNum());

    return a string so any control that can display a string would do it for you

    Now the simpler way I can come up to would be a blank aspx project, add a label and a button, double click the button and it will create on the cs file a stub for the OnClick event, in there put this line of code

    label1.Text="x + y is : " + (XValue.XNum() + YValue.YNum());

    That should do it

    Also remember to include your classes

    Regards

    PS: study hard

    Friday, September 10, 2010 4:07 PM
  •  

    in VS 2005 when i create new project .. and under visual C# tree how come i can't see the "asp.net web application" anywhere?

    i believe that is how i would create aspx project?

    Friday, September 10, 2010 6:07 PM
  • Yes, that is how you do it, do you have that feature installed?, check please.

    Also check in the Create new Website option, is near the create new project option

    Regards

    Friday, September 10, 2010 6:21 PM
  • ok so i found the New Website..

    now, i have the default.aspx.cs and default.aspx - what do i do now? what file should i change?

    Friday, September 10, 2010 6:48 PM
  • Friend I leave in 20 minutes so this might be my last post until monday,

    default.aspx is for asp.net code

    default.aspx.cs is for c# code so your code goes here

    Regards

    Friday, September 10, 2010 6:52 PM
  • you have a great personality
    Friday, September 10, 2010 6:57 PM
  • last question for the day -- how do i create lable and button?
    Friday, September 10, 2010 7:04 PM
  • Thanks for the compliment, some people think otherwise.

    Is there something else you need or are you all set for the weekend.

    Have a nice one if not and good luck on your future enterprises

    Friday, September 10, 2010 7:05 PM
  • On the designer view, there is a toolbox window, mine is to the left but it can be on other place, in there you can find the controls you need, the only thing you need to do is drag them to the designer.

    Regards

    Friday, September 10, 2010 7:07 PM
  • i want to know how to create label and button so that i can publish the file :(
    Friday, September 10, 2010 7:08 PM
  • Start a new thread on the ASP.NET forum for questions about web applications.  Thanks.

    For questions related to ASP.NET use the ASP.NET forum .

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Friday, September 10, 2010 7:20 PM
  • You do?, how kind of you.

    Now,

    "x + y is : " + (XValue.XNum() + YValue.YNum());

    return a string so any control that can display a string would do it for you

    Now the simpler way I can come up to would be a blank aspx project, add a label and a button, double click the button and it will create on the cs file a stub for the OnClick event, in there put this line of code

    label1.Text="x + y is : " + (XValue.XNum() + YValue.YNum());

    That should do it

    Also remember to include your classes

    Regards

    PS: study hard

     

    a qucick on,


    when i am adding classes (ie. using TakeX, using TakeY, using Execute), it throw errors as this in the x.aspx.cs

    The type or namespace name 'TakeX' could not be found (are you missing a using directive or an assembly reference?) 

    The type or namespace name 'TakeY' could not be found (are you missing a using directive or an assembly reference?) 

    The type or namespace name 'Execute' could not be found (are you missing a using directive or an assembly reference?) 

    help me fix this last one

    Monday, September 13, 2010 3:43 PM
  • Did you add the file(s) that had this classes to the blank asp project?

    If not please do so, VS can't find them on his own

    Regards

    • Marked as answer by Rudedog2 Monday, September 13, 2010 3:51 PM
    Monday, September 13, 2010 3:47 PM
  • You do not add references to to classes within your source file.  You add references to the assembly that defines them.

    using AssemblyThatDefinesTakeX;

    class UseTakeX

    {

       AssemblyThatDefinesTakeX.TakeX take;

    }

    For this to work properly, the assembly must have previously added to your Project's assembly References.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Monday, September 13, 2010 3:50 PM
  • ya

    i created a new website

    then i add a label and a buttion

    double click the button and add something like this

    protected

     

    void Button1_Click(object sender, EventArgs e)

    {

    label1.Text =

    "x + y is : " + (XValue.XNum() + YValue.YNum());

    }

    i then right-click on the website and choose add existing project; whihc import the project which has those files

    but still that didn't work. error is

    The name 'XValue' does not exist in the current context C:\Documents and Settings\x\Desktop\web\Default.aspx.cs 19 40 C:\...\web\

    Monday, September 13, 2010 4:11 PM
  • http://social.msdn.microsoft.com/Search/en-US?query=references%20visual%20studio&ac=1

    That last error message is stating that a variable is out of scope. 

    Please use the other forum that I indicated above for your web site projects.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Monday, September 13, 2010 4:59 PM

  • hi, I have a problem please i need help. my question is simple i have a request on access that countains information about students and note of many subjet . So i need to print on each page information of each student and his notes , I th ought to use group in the cristal report to display information of a student on each page but i have to display the informations in an array i don't know how . Thank you for helping me

    Loubnas
    Tuesday, December 7, 2010 10:34 AM