0

I tried to assign a value in aspx.cs file.

static String cnn;
cnn = "hi";

I get an error:

cnn is a field but used as a type

I have added the code above all functions in the class (not inside page load or any other event handler).

Why do I get this error and how can I fix it??

1 Answer 1

3

You can't refer to cnn outside of a function, except when declaring the field.

If you want to declare and initialize the field, this can be done in one line:

static string cnn = "hi";

Note on style - in C#, use string, not String.

Sign up to request clarification or add additional context in comments.

7 Comments

It is still not working and I get the same error I mentioned ago
@SriniVas - Is this happening on this line? Are you certain? It could very well be happening somewhere else where cnn is referenced. Where are you trying to use it?
That is only place where I used cnn.
@SriniVas - since this is essentially identical to what Adil posted in this answer, how come that answer worked and this one didn't?
I am not sure why it didn't.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.