locked
access level of a method in a class changes RRS feed

  • Question

  • I am trying to read in a text file where each line is made of character strings separated by commas. I read in each line from the file, split it by the commas, and then store results in a 2 dimensional array. A sample of the text file would be:

    This is line 1,this is the second part of line 1
    This is line 2,this is the second part of line 2

    with the code, would like to store this such that:

    args[0,0] = "This is line 1"
    args[0,1] = "this is the second part of line 1"
    args[1,0] = "This is line 2"
    args[1,1] = "this is the second part of line 2"

    Ever time i compile the code, i get a warning that "the access level of a method in a class library has changed", but i can't find the mistake... any ideas? Thanks


    using System; 
    using System.IO; 
     
    namespace StrTest 
        class Program 
        { 
            static void Main(string[,] args) 
            { 
                StreamReader SR; 
                string[] S; 
                SR = File.OpenText("i:\\text.txt"); 
                S = SR.ReadLine().Split(','); 
                 
                args[0, 0] = S[0]; 
                args[0, 1] = S[1]; 
     
                Console.WriteLine(args[0,0]); 
                 
                Console.Read(); 
            } 
        } 



    Saturday, May 31, 2008 1:05 AM

Answers

  • You're going to have to declare another variable to hold those strings.

    For the static Main method that is supposed to be the main entry point for an application, you can't use a multidimensional array as its arguments.

    Just declare another array, or similar data structure.

    For instance, to use a List instead:

    List<String[]> values = new List<String[]>();

    String[] item = new String[] {  value1, value2 };
    values.Add(item);

    Even if you were able to declare the argument to Main as a multidimensional array, it would hold a copy of the command line arguments to the program, and would most likely not be of the right size to hold the contents from your file.

    If you want to use the ReadLine method and Split, simply do this:

    List<String[]> values = new List<String[]>();
    values.Add(SR.ReadLine().Split(','));

    this will add an array to the list of values, where the array will be sized to hold the number of elements entered on the line that SR.ReadLine read in from the file.

    O:

    List<String[]> values = new List<String[]>();

    String[] parts = SR.ReadLine().Split(',');
    if (parts.Length == 2)
        values.Add(parts);

    In any case, you *cannot* specify the argument to Main as a multidimensional array, so it doesn't matter how much you want to do that, you won't be able to compile nor run your program in that manner, as you've already discovered.

    http://presentationmode.blogspot.com/
    Saturday, May 31, 2008 7:23 PM
  • One other minor observation; you stated: "Ever time i compile the code". I've just double checked, and this happens when you *run* the code, not when you compile it. Not a biggie, but it meant that I was looking for errors in the compile output, hence my cmoment that it compiled fie for me.

    As has already been pointed out, simply create a separate variable for the multi-dimensional array (or use a different structure).
    Marc
    Saturday, May 31, 2008 7:40 PM

All replies

  • Odd; there is nothing odd in the code, and it compiles without issue here (C# 3 compiler).

    What tools are you using? And could you post the *full* message?
    Marc
    Saturday, May 31, 2008 9:06 AM
  • I'm using MS Visual C# 2008 Express Edition.
    the error message i get is:

    MethodAccessException was unhandled
    Main method for type 'StrTest.Program' has invalid signature
    Saturday, May 31, 2008 10:34 AM
  • That is different.

    The error message doesn't say it changes, just that it is invalid.

    The problem is that the command line arguments is not a two-dimensional array. Remove the comma in the declaration and you should be set.

    In other words:

    static void Main(string[] args)  


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Saturday, May 31, 2008 12:41 PM
  • If i do that, then the string args can't be set in the way stated. Perhaps it's unclear, but i'm trying to make it so that args will store the two parts of the string in a way that i can index each individual straing, so :

    S = SR.ReadLine().Split(','); 
    args[0, 0] = S[0]; 
    args[0, 1] = S[1]; 
     
    S = SR.ReadLine().Split(','); 
    args[1, 0] = S[0]; 
    args[1, 1] = S[1]; 


    Saturday, May 31, 2008 6:29 PM
  • You're going to have to declare another variable to hold those strings.

    For the static Main method that is supposed to be the main entry point for an application, you can't use a multidimensional array as its arguments.

    Just declare another array, or similar data structure.

    For instance, to use a List instead:

    List<String[]> values = new List<String[]>();

    String[] item = new String[] {  value1, value2 };
    values.Add(item);

    Even if you were able to declare the argument to Main as a multidimensional array, it would hold a copy of the command line arguments to the program, and would most likely not be of the right size to hold the contents from your file.

    If you want to use the ReadLine method and Split, simply do this:

    List<String[]> values = new List<String[]>();
    values.Add(SR.ReadLine().Split(','));

    this will add an array to the list of values, where the array will be sized to hold the number of elements entered on the line that SR.ReadLine read in from the file.

    O:

    List<String[]> values = new List<String[]>();

    String[] parts = SR.ReadLine().Split(',');
    if (parts.Length == 2)
        values.Add(parts);

    In any case, you *cannot* specify the argument to Main as a multidimensional array, so it doesn't matter how much you want to do that, you won't be able to compile nor run your program in that manner, as you've already discovered.

    http://presentationmode.blogspot.com/
    Saturday, May 31, 2008 7:23 PM
  • One other minor observation; you stated: "Ever time i compile the code". I've just double checked, and this happens when you *run* the code, not when you compile it. Not a biggie, but it meant that I was looking for errors in the compile output, hence my cmoment that it compiled fie for me.

    As has already been pointed out, simply create a separate variable for the multi-dimensional array (or use a different structure).
    Marc
    Saturday, May 31, 2008 7:40 PM
  • Yeah, sorry, I've never actually encountered that particular exception but of course it will happen at runtime, and not at compile time.

    http://presentationmode.blogspot.com/
    Saturday, May 31, 2008 8:51 PM
  • Thanks guys! Sorry for the confusion on the compile comment, I'll try to be more precise.
    Saturday, May 31, 2008 10:20 PM