The error is on the line that starts with "test":
namespace DM
{
class Armour
{
string[] strArray = new string[] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
string test;
test = strArray[0];
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello. I can initialize the array just fine, but when I try to access it, I get an error: "the name does not exist in the current context" I copy/paste code from the internet and it is still doing this. I tried changing all the different combinations of public and private, and it still happens. Thanks in advance!
The error is on the line that starts with "test":
namespace DM
{
class Armour
{
string[] strArray = new string[] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
string test;
test = strArray[0];
Hi WilliamOGorm,
The "test" should be placed in a function, not directly inside a class. You could update your code like this:
namespace DM
{
#include <cstring>
#include <iostream>
class Armour
{
public:
string strArray[5] = { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
string test;
string test() {
test = strArray[0];
return test;
}
};
};
Best Regards,
Dylan
In appears you are coding in C#. So consider the following:
namespace DM
{
class Armour
{
string[] strArray = new string[] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
string test;
static string[] staticArray = new string[] { "Tom", "Dick", "Harry" };
string test2 = staticArray[0]; // initialize from static array
public string Test1()
{
test = strArray[0];
return test;
}