array of string


N

nicol

Hi
This is a program that have 3 error that I could not solve it

Error 1 The name 'i' does not exist in the current context
((( same error in 3 line )))
using System;
namespace ConsoleApplication109
{
class Program
{
static void Main(string[] args)
{
information name=new information();
name.x="reza"; // ********error
i++; //*** error*******
name.x="ali"; // ********error
}
class information
{
public int i = 1;
string []name1=new string[100];
public string x
{
get
{
return name1;
}
set
{
name1 = value;
}
}
}
}
}

Thanks . . .
 
Ad

Advertisements

A

Arne Vajhøj

This is a program that have 3 error that I could not solve it

Error 1 The name 'i' does not exist in the current context
((( same error in 3 line )))
static void Main(string[] args)
{
information name=new information();
name.x="reza"; // ********error
i++; //*** error*******
name.x="ali"; // ********error
}


The error message say that i does not exist in that
context.

I would agree with the compiler. :)

Arne
 
K

Konrad Neitzel

Hi Nicol!

I will just try and find out, what you did and what you wanted to do...
(It is mostly a guess, but I try to explain a lot. Hopefully I am able to
explain something that is usefull for you.)
class information
{
public int i = 1;
string []name1=new string[100];
public string x
{
get
{
return name1;
}
set
{
name1 = value;
}
}
}


Ok, you define a new class called information.

This function has am integer field called i and that field is initialized to
1.

Next comes a private field called name1 which is an array of 100 strings.
(If you miss the public, the member will be private. That means, that you
cannot use it from outside.)

And then you create a property called x. It either takes or gives a string.
And when you set it, the i-ths element will be stored. With a set it is
given back.

static void Main(string[] args)
{
information name=new information();
name.x="reza"; // ********error
i++; //*** error*******
name.x="ali"; // ********error
}


Ok. Lets look at this code.

static void Main (string[] args)
{
}
defines the main routine that is called when the program is started.

information name=new information();
defines a new variable called name of type information and it directly
instantiates it with a new instance.

name.x="reza";
- name is not an array, so you cannt use [] here.
- maybe you mean name.x = "reza", because the .x is the property and it will
store the "reza" internaly in name1. Here is important, that i is just
part of the variable name.

i++;
You want to increase the i inside name. So maybe you meant:
name.i++;

name.x="ali";
This is the same as above. So maybe you meant:
name.x = "ali";

So maybe it helps a little to compare it a little with cities. English is
not my main language and that makes everything a little harder. Please do
not be confused. If it confuses you: Just ignore it completly.

The class is a map of a city. The instance is the real city. (In this crazy
worlds, there will be a lot of cities that are equal to each other :) ).

So first you create a map of a city. It only exists on some paper.
- You create a street called i with a small space that just hold a number.
(Together with a comment: When you build this city: Please put a 1 here!)
- You create a street called name1 with slots for names. (Together with the
comment: if you build such a city: Make sure there are 100 shields to put
names on! And create some security so that no foreigner can access these
shields.)
- You create a job. Someone must be there who is doing something if someone
asks him for a name or tells him a name. This person should look for the
number on street "i" and simply look for the ith name and put it / give it.

Once you have this kind of map, you can easily go and say: I want exactly
such a city here with the name "name". Then the computer is doing everything
for you. It builds the streets inside and so on.

So when you get the "street" i inside the city "name", you have to say
exactly that! So inside your "Main" city is no street "i" and when you tell
the cab driver (your computer) get me to "i", then it will simply tell you:
Sorry, no such street here.

Did that help? Or was ist just confusing?

With kind regards,

Konrad
 
Ad

Advertisements

P

PvdG42

nicol said:
Hi
This is a program that have 3 error that I could not solve it

Error 1 The name 'i' does not exist in the current context
((( same error in 3 line )))
using System;
namespace ConsoleApplication109
{
class Program
{
static void Main(string[] args)
{
information name=new information();
name.x="reza"; // ********error
i++; //*** error*******
name.x="ali"; // ********error
}
class information
{
public int i = 1;
string []name1=new string[100];
public string x
{
get
{
return name1;
}
set
{
name1 = value;
}
}
}
}
}

Thanks . . .


As Arne said in a slightly different way, the identifier i is not declared
or initialized in Main(). Your unqualified references to it in Main() are
causing the syntax errors.

You have declared i as a public field in your class, information, so you can
either leave it as an instance field (considering how you use it in Main(),
I don't recommend this) and refer to it using <object name>.i syntax, or you
can define it as a static field and refer to it using <class name>.i syntax.

Another thing to consider. As you initialize the field i to 1, what are you
planning to do with the first element of your array?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

file [?] 7
Overriding virtual function Equals affecting static Equals... 7
error :( 4
? 7
Shouldn't this compile? 2
inheritance problem 2
what is base() in inheritance 2
Looping through Object Properties 2

Top