0

I have this:

static void Main(string[] arg)

And:

Main("month");

But for some reason, this gives the error:

The best overloaded method match for 'Numbers.Program.Main(string[])' has some invalid arguments

And

Argument 1: cannot convert from 'string' to 'string[]'

How do I fix these?

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
Jon
  • 2,566
  • 6
  • 32
  • 52
  • 1
    `string[] args` expects an array not a string the error message is self explanatory – MethodMan May 09 '13 at 00:16
  • @EvanTrimboli His question was about something a little different, but with the same error. – Jon May 09 '13 at 00:17
  • HA.. I don't think so – MethodMan May 09 '13 at 00:18
  • @DJKRAZE I'm trying to call a function with some info passed along to it, he's trying to move a TextBox's text to an array. – Jon May 09 '13 at 00:19
  • Chipperyman573, what you need to do is if the values that you are trying to pass do not affect the starting or running of the application on startup then don't use the Main entry point to pass values.. you need to use `Events` or other Methods to call / pass values to work against what you are trying to accomplish. perhaps you should refactor the question and state what it is you are truly trying to do.. – MethodMan May 09 '13 at 00:29
  • Right, the questions are different, but the root cause is the same. `string` is not the same as `string[]`. Surely you could have search for that error and got that information? You could argue that 'string[] foo = "bar";` is not the same error as well, still, same cause. – Evan Trimboli May 09 '13 at 00:34

2 Answers2

3

The other answers are correct (the compiler does not let you pass a string as an argument to a method expecting a string array), but an alternative approach is to change the method signature of your Main method like so:

static void Main(params string[] arg)

The params keyword allows arguments to be passed in separately instead of an array. Thus, the following calls would be equivalent:

Main("month");
Main(new string[] {"month"});

Incidentally -- while it is legal, it is not common to call the Main method (your program's entry point) from your own program. Depending on your requirements, you may want to consider a new method that has only a single string as an argument, e.g.:

public static void MyMethod(string s)
{
      // your code
}

// in your Main method
MyMethod("month");
drf
  • 8,461
  • 32
  • 50
0

String is one String. String[] is an array of Strings.

Try this:

String[] parameters = new String[] {"month"};
Main(parameters);

See http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx for more information about arrays and declaring/instantiating them.

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • perhaps because the OP is a beginner you may want to display or advise using more meaningful variable names – MethodMan May 09 '13 at 00:17
  • @DJ KRAZE I don't have enough context to give a meaningful variable name, but I'll try. – Patashu May 09 '13 at 00:18
  • what I am trying to say is advise against using variable names that can get or be come confused with Reserved words.. perhapss arrParams or arrString you know a better naming convention – MethodMan May 09 '13 at 00:19
  • @DJKRAZE I have experience with other (more basic, such as lua variants) code so I know to name my variables with info, etc. Good advice, I didn't make it very clear when I made the post. – Jon May 09 '13 at 00:20
  • I can read it clearly and I am sure other coders can too it's just for a beginner they might have a Typo and put `Array` instead of `array` for example not knocking your answer the answer you provided is Spot On.. I hope I did not offend you .. – MethodMan May 09 '13 at 00:26
  • 1
    @DJ KRAZE No, you are correct in that it's good manners to not use keywords/common words/common class words as variable or method names. – Patashu May 09 '13 at 00:28