c# string assignment bug?

G

Greg Merideth

While I can't post all of the code I'll post snippets but, it appears that C#
is *not* assigning a string, inside of a class, a value that I am passing to it
using its public set method and I can't for the life of me figure out why.

I have a class, call it Class Test with a private string _a and a public member
string a with a get{ return value; } set{ _a = value; }

I have a method, call it MyMethod() that returns a string

If I create Test mytest = new Test()
and then
mytest.a = MyMethod()

a is *not* assigned the value coming from the method function..now...here's the
kick in the pants.

I've debugged this step by step. The string assigned TO in MyMethod() is set
correctly...I've steped *all* the way to the actual assignment in the Test
class of set { _a = value; } and I've added a watch to the "value" field - it
IS the value of the string returned from the Method call however it does not
*actually* get set in the class.

The real fun is if I do a

string teststring = MyMethod()
mytest.a = teststring;

that works.....what the hell is going on here? I thought I might have passed
in an instance of the class that was different that the one I was using but the
use of "string teststring = MyMethod()" disproves that.

Since the watch on the set { _a = value; } shows the value that I am trying to
set, and it doesn't actually set the value then what could I be looking at here?

The MyMethod() method takes a private instance of the class and loads a set of
data into it from an xml using deserialize. If the values dont match via a crc
check then a string field gets zeroed out. Its that part that works/fails.
 
R

Richard A. Lowe

This:
string a with a get{ return value; } set{ _a = value; }

Should be this:
string a with a get{ return _a; } set{ _a = value; }

HTH,
Richard

Greg Merideth said:
While I can't post all of the code I'll post snippets but, it appears that C#
is *not* assigning a string, inside of a class, a value that I am passing to it
using its public set method and I can't for the life of me figure out why.

I have a class, call it Class Test with a private string _a and a public member
string a with a get{ return value; } set{ _a = value; }

I have a method, call it MyMethod() that returns a string

If I create Test mytest = new Test()
and then
mytest.a = MyMethod()

a is *not* assigned the value coming from the method function..now...here's the
kick in the pants.

I've debugged this step by step. The string assigned TO in MyMethod() is set
correctly...I've steped *all* the way to the actual assignment in the Test
class of set { _a = value; } and I've added a watch to the "value" field - it
IS the value of the string returned from the Method call however it does not
*actually* get set in the class.

The real fun is if I do a

string teststring = MyMethod()
mytest.a = teststring;

that works.....what the hell is going on here? I thought I might have passed
in an instance of the class that was different that the one I was using but the
use of "string teststring = MyMethod()" disproves that.

Since the watch on the set { _a = value; } shows the value that I am trying to
set, and it doesn't actually set the value then what could I be looking at here?

The MyMethod() method takes a private instance of the class and loads a set of
data into it from an xml using deserialize. If the values dont match via a crc
check then a string field gets zeroed out. Its that part that
works/fails.
 
G

Guest

hi
i hope you defined the Variables in the class that you use set and get like
that:
private static string _Configuration;

it has to be static!!!
 
J

Jon Skeet [C# MVP]

guy said:
i hope you defined the Variables in the class that you use set and get like
that:
private static string _Configuration;

it has to be static!!!

Um, no it doesn't. (Unless the property is static, of course.)

Why do you think it does?
 
J

Jon Skeet [C# MVP]

Greg Merideth said:
While I can't post all of the code I'll post snippets but, it appears that C#
is *not* assigning a string, inside of a class, a value that I am passing to it
using its public set method and I can't for the life of me figure out why.

Could you post a short but complete program which demonstrates the
problem? You don't have to post all of your normal code, but take bits
out until you've got a short but complete example.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Greg Merideth

Yea that was a posting screwup. The gets are all correct but other things are
causing mega errors at some point. When I debug watch now the data in class
members just vanish while I am debugging through steps and now when I debug
watch certain methods, clicking "next step" causes visual studio to exit....

I think I've got some major problems in this code so...back to square one.
 
G

Greg Merideth

I took the code thats causing the problem and wrote a short test program that
strangely enough works just fine. Its just in the full version it doesn't.

I've got a series of screen shots that shows just how crazy this is going but
each shot is at 1600x1200 so I'm going to reduce them down a bit. Now I'm
taking the actual classes I'm using, not test classes and making a test program
out of them instead, mabye I can reproduce this.

The only way I got this to work was to use the

string mystring;
mystring = MyMethod();
myclass.mymember = mystring;

simply calling

myclass.mymember = MyMethod(); fails

I'll see if I can reproduce this in a smaller app
 

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

Top