General

Reply

Level 3

Level 3
niwrA
Posts: 152
Registered: ‎31-07-2008
Message 1 of 3 (156 Views)

Stupid C# property question

Hi All, 

 

Here's probably a really stupid question, but I'm stumped and Google doesn't help either.

 

In a class, I have a public string property called Value. If I simply define it as public string Value; my application works great. If I change it to use get and set, with a private container called _value, I get a bad image format exception. I really don't understand ... am I missing something?

 

So when I comment out public string Value below, and comment in public string Value { get ... etc.} I get an error, but vice versa no problem:

 

string _value = "";

// public string Value;

public string Value
{
get
{
Console.WriteLine("Reading value from element: " + _value);
return this._value;
}
set
{
Console.WriteLine("Setting element value to: " + value);
this._value = value;
}
}

Please use plain text.

Member

Member
HeffJoker
Posts: 2
Registered: ‎30-11-2011
Message 2 of 3 (121 Views)

Re: Stupid C# property question

Unfortunately, I'm not sure as to why you're getting that error. But you might "fix" it by using a different name other than "value". As a general programming rule, I tend to stay away from using variable names close to reserved words as much as possible. Not only does this help make the code readable, but it also prevents unexplained bugs. If I can try to run your code later today, I'll update you on if I find the exact problem. Hope that helps.
Please use plain text.

Level 3

Level 3
niwrA
Posts: 152
Registered: ‎31-07-2008
Message 3 of 3 (106 Views)

Re: Stupid C# property question

Yes, I think I tried Value2 just for testing, and then it seemed to work, so you're probably right.

 

I wonder if I can use [Value] ? I'll try that.

Please use plain text.
This widget could not be displayed.
Announcements

Welcome to the PlayStation Mobile Developer Forums


This is a community for the discussion of technical topics with other developers and SCE engineers. Posting ideas/requests are also appreciated. Join the discussion!

PlayStation®Mobile開発者フォーラムでは世界中の開発者の皆様と一緒に、議論や情報交換が可能です。SCEも議論に参加し、皆様の開発をサポートします。アイデアやリクエストも大歓迎です。ぜひご参加ください。

PSM Developer Registration (for free) on PSM DevPortal is required to post on the forum.
Please sign out then sign in again to the forum and PSM DevPortal after you have completed the registration.

フォーラムへ投稿をするにはPSM DevPortalへの登録(無料)が必要です。
登録後はフォーラムと PSM DevPortalを一度ログアウトし、再度ログインしてください。






Recent News