Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the following code
C#
private readonly string _cstrvalueMember;    //Holds value
private readonly string _cstrdisplayMember;

object obj;
LookupDTO lkpDTOo =(LookupDTO) obj;
if (lkpDTOo.strValueMember == "3")
{
    lkpDTOo.strDisplayMember = "--";
}

But lkpDTOo.strDisplayMember = "--"; here I am getting error like
VB
Error   50  Property or indexer 'XXX.DTO.LookupDTO.strDisplayMember' cannot be assigned to -- it is read only 


Even though I removed readonly and made it public it is not working
Posted
Updated 16-Jul-14 6:38am
v4
Comments
johannesnestler 14-Jul-14 9:07am    
Yes so what's the question? You should know variable scope and access rules, otherwise asking QA makes not much sense. You didn't show your actual code, so I see no reason to guess what could be your problem. Show actual, relevant code and ask again(?) - Btw. Access such information with Properties (get/set) and not through variables (I think that is OOP lesson1)
chandra sekhar 14-Jul-14 9:09am    
But how can i access that member? I have shown on which i am working :|
johannesnestler 14-Jul-14 9:28am    
Others answered it in between. But I'm puzzled: You make a variable "readonly", next you provide a thing called "get". Then you aks "why can't I set it", You didn't mention any properies first, just that you made the variable public (this can't be true because it would have worked). So even if you are a beginner you should have asked what "readonly and get" means - C# is a language you can read like spoken langauage. So realy spend ONE day (should be enough) to go through the c# basics (again)..
Kornfeld Eliyahu Peter 14-Jul-14 9:39am    
See his post on one of the solutions...
He thinks CP is a compiler service, so feed us line-by-line :-)
Sergey Alexandrovich Kryukov 14-Jul-14 11:36am    
The whole idea to assign hard-coded immediate constant "--" in code is quite bad. You did not explain to us the goal of it. Anyway, here is how it looks: you specifically created a read-only member yourself and trying to assign a value to it. Your intention is hard to understand. Your already got technical advice in other solution, but I'm not sure they will solve your problem, which is just understanding of programming. You should understand every line you write.

—SA

You have not defined the setter for strDisplayMember in your class.

Try
C#
public string strDisplayMember
{
        get { return _cstrdisplayMember; }
        set { this._cstrdisplayMember = value; }

}
You have a similar problem with strValueMember.

Here is a reference to brush up on Properties[^]
 
Share this answer
 
You have to learn what scope and access of a variable are!
You have here a read only property (and it's read only because it have only property getter, so you can only get its value):
C#
public string strDisplayMember
{
  get { return _cstrdisplayMember; }
}

To make it writable you have to add a property setter (which enables to set its value), something like this:
C#
public string strDisplayMember
{
  get { return _cstrdisplayMember; }
  set { _cstrdisplayMember = value; }
}

http://msdn.microsoft.com/en-us/library/w86s7x04.aspx[^]
---
Do yourself a favor and learn these basic elements of C# - do not write any code until you understand them by heart...
 
Share this answer
 
v2
You need a property setter

e.g.

C#
public string strDisplayMember
   {
       get { return _cstrdisplayMember; }
       set { cstrdisplayMember = value;}
   }


Otherwise the property is read-only
 
Share this answer
 
Comments
chandra sekhar 14-Jul-14 9:33am    
Now i am getting an error like LookupDTO lkpDTOo =(LookupDTO) obj; use of unassigned variable obj
johannesnestler 14-Jul-14 9:53am    
back to basics for one day! You see, you don't know what to do with the solutions - exactly what I said in my first comment to you your question.
Duncan Edwards Jones 14-Jul-14 11:33am    
object obj;

This object never gets assigned to anything. It is like you have got a fresh sheet of paper from the notepad and handed it to someone to read - but they can't because you have not written anything on it.
The whole idea to assign hard-coded immediate constant "--" in code is quite bad. You did not explain to us the goal of it. Anyway, here is how it looks: you specifically created a read-only member yourself and trying to assign a value to it. Your intention is hard to understand. Your already got technical advice in other solution, but I'm not sure they will solve your problem, which is just understanding of programming. You should understand every line you write.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900