what is wrong with this code? [Resolved]

Posted by Balajikcp8 under C# on 8/17/2012 | Points: 10 | Views : 1174 | Status : [Member] | Replies : 6
class demo
{
string str="";
str="balaji"; //here i'm not able to assign value.why.?

}




Responses

Posted by: Gopesh9 on: 8/17/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
No you cannot assign the value of the str twice inside the class. Since when you write string str = ""; it will become a field of the class so we cannot use it as a type where we can assign the value of its more than once.

G. S.
.Net Developer

Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 8/17/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
You cannot re-assign the value of a variable with in the class. It becomes a field and we cannot use it as a type. You may re-assign it in the method or with in one method you can re-assign a variable more than once by

method() {
string x= "5";
{ x = "asdf"; }
}

or directly re-assign
x="5";
x="asfd"; the value of x gets re-assigned.

Regards,

Awesome Coding !! :)

Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: prashant12-7256 on: 8/17/2012 [Member] Starter | Points: 25

Up
0
Down
Error is .str' is a 'field' but is used like a type

You need to assign value to str inside methog like this:


class demo
{
string str = "";
public void StringValue()
{
str = "balaji";
}
}


Or try this :

class demo

{
string str = "balaji";
}




Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirajkumar on: 8/18/2012 [Member] Starter | Points: 25

Up
0
Down
class demo
{
string str="";
str="balaji"; //here i'm not able to assign value.why.?

}


u can not assign values to the variable more then once in a class because variable can declare only once.

Dhiru

Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Annamacharyakadambi on: 8/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi This is Annamacharya

I can't get why u r unable to assign the value there. If I see you full code page I think I can answer your question.
I think prashanth@12 is correct.
you are trying to assign value to type variable. Any way I didn't come across such a situation.

Thanks & Regards,


Annamacharya K

Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Balajikcp8 on: 8/18/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks for all

Balajikcp8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response