1

OK, let me preface this by saying I'm not coding in visual studio. I'm doing this in down time at work, and don't have that option. I'm coding in Notepad ++ portable and compiling with the windows built in c# compiler.

That said, I have this class in useful.cs

using System;

public class Useful

{   
    public void GetInt()
        {
            string s = Console.ReadLine();
            int i = Convert.ToInt32(s);
            return i;
        }
}

Now I also have a main project file, let's call it project.cs. How do I call useful.cs in that file so I can just type like int a = Useful.GetInt(); and have it work?

3
  • 3
    This is how: msdn.microsoft.com/en-us/library/78f4aasd.aspx Commented Jan 19, 2016 at 4:32
  • Check the MSDN docs. Commented Jan 19, 2016 at 4:34
  • you should use a Namespace to encapsulate your Useful class and then import this Namespace in your project.cs to access the Useful class and its functions. Commented Jan 19, 2016 at 6:39

1 Answer 1

2

You should set GetInt as a static method.

public static int GetInt()
{
    string s = Console.ReadLine();
    int i = Convert.ToInt32(s);
    return i;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Although this does match their example, it does not answer their question.
This is wrong. You cannot return a value from a method that has void as the return type in its signature. Don't just copy from question, ensure that your answer actually works ;)
Thanks, I actually just copied the class from another program I did where it was in the only .cs file, but I forgot to change the void to int.
Now, that's much better!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.