Windows Phone Developers

Friday, December 5, 2008

Array – X is a 'variable' but is used like a 'method' Error in .NET (C#) - CS0118

The compiler detected a situation in which a construct was used in some erroneous way or a disallowed operation was tried on a construct. Some common examples include the following:

  • A try to instantiate a namespace (instead of a class)
  • A try to call a field (instead of a method)
  • A try to use a type as a variable
  • A try to use an extern alias as a type.

One possible cause is use of parenthesis ‘()’ in Arrays

static void ArrayExample()

{

int[] Marks = new int[5];

string[] Students = new string[5];

for (int i=0;i

{

Console.WriteLine("Student Name: {0} - Marks {1})" , Students(i), Marks(i));

}

}

To resolve this error, replace parenthesis with square brackets

Console.WriteLine("Student Name: {0} - Marks {1}" , Students[i], Marks[i]);


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment