Hello, I have my main program.cs with static void main and i want to call a method I have created in my misc_methods.cs.

The method is string_split(string inputString)

misc_methods.cs is in the same folder and looks like this:

namespace my_project
{
    class string_split
    {
        public string_split(string inputString)
        {
              ...code for method
        }

    }
}

I've only just started learning C# so I don't really know what the problem was but I thought the main part was having the same namespace.

Oh, i get the type or namespace could not be found error.
I'd be most grateful if someone could help me.

Thanks

Define your class as public: public class string_split . Also do both classes reside in the same namespace? namespace my_project ? If not you will need to add a using my_project; directive at the top of your class to see the other definition

Define your class as public: public class string_split . Also do both classes reside in the same namespace? namespace my_project ? If not you will need to add a using my_project; directive at the top of your class to see the other definition

I've added public, they both have the same namespace but i'm still getting an error.

"The name " string_split" does not exist in the current context "


This is my main method:

namespace my_project
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new myInputForm());
            string_split(myInputForm.strValue);
      }
    }
}

In addition to snake reply, you need to instantiate the class in the file where you want to use the method like this string_split split=new string_split(inputstring) because this method happen to be the constructor.

Two more remarks.
1) I would not call methods in the program class ad leave it as it is. Call methods in the Form class.
2) The string class already has a split method, so I would not invent my own, unless you find it fun to do so. But I know, as a beginner just as I am in fact, you want some functionality and you think it is not there in .NET, untill you find out it IS there :-O

commented: good practices +10

Thanks ema005 and ddanbe. i know theres a sting split method but mine is different to the default method. And i will bear in mind calling it in the form.

I tried replacing:

string_split(myInputForm.strValue);

with:

string_split split=new string_split(myInputForm.strValue);

and i get "The type or namespace name 'string_split ' could not be found (are you missing a using directive or an assembly reference?)"

What is the exact name of the class your string_split method is in?

What is the exact name of the class your string_split method is in?

its defined here:

namespace my_project
{
    class string_split
    {
        public string_split(string inputString)
        {
              ...code for method
        }

    }
}

OK do it this way:

class StringSplitter
    {
        public StringSplitter()
        {
            // default constructor
        }

        public string string_split(string inputString)
        {
              //...code for method
            string splittedString=string.Empty;
            // more code
            return splittedString;
        }
    }

And then do something like this in your form:

StringSplitter split = new StringSplitter();         
            string Fstr = "My formstring";
            //call your method
            string Splstr = split.string_split(Fstr);
commented: God bless u greatly in Jesus name! +0

2) The string class already has a split method, so I would not invent my own, unless you find it fun to do so. But I know, as a beginner just as I am in fact, you want some functionality and you think it is not there in .NET, untill you find out it IS there :-O

And it never stops! I'm constantly finding implementations in the .NET framework that I didn't think existed so I rolled up my own code.

I tried this and i still get:

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

It just doesn't seem to be finding it at all.

OK do it this way:

class StringSplitter
    {
        public StringSplitter()
        {
            // default constructor
        }

        public string string_split(string inputString)
        {
              //...code for method
            string splittedString=string.Empty;
            // more code
            return splittedString;
        }
    }

And then do something like this in your form:

StringSplitter split = new StringSplitter();         
            string Fstr = "My formstring";
            //call your method
            string Splstr = split.string_split(Fstr);

Is there something other than namespace required for it to be found?

Did you follow the guidelines of sknake?
Did you actually add the clas to your my_project project?

How about you upload your project and we take a look at it?

I got it working, i'd added it to the wrong project.
I wouldn't have known to add it to a project in the first place though if it wasn't for your advice so thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.