468,790 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,790 developers. It's quick & easy.

Strange Behavior

I have a static struct :

public static LocationPaging stLocationPaging = new LocationPaging();

Which is made up from :

public struct LocationPaging

{

public int Page;

public int MaxPage;

}

Which is used to control the paging of some records. BUT , when I call :

Scanning.stLocationPaging.Page = Scanning.stLocationPaging.Page + 1;

The increment doesn't occur, im quite new to C# , but im sure I have done
this in VB.Net with no issues. What am I over looking ?


Jul 27 '06 #1
3 2002
Ignore the above , I was resetting it back to 1 in my code :(
How embarassing !

"Jon Vaughan" <jo**********@hotmail.comwrote in message
news:1S*********************@fe12.news.easynews.co m...
>I have a static struct :

public static LocationPaging stLocationPaging = new LocationPaging();

Which is made up from :

public struct LocationPaging

{

public int Page;

public int MaxPage;

}

Which is used to control the paging of some records. BUT , when I call :

Scanning.stLocationPaging.Page = Scanning.stLocationPaging.Page + 1;

The increment doesn't occur, im quite new to C# , but im sure I have done
this in VB.Net with no issues. What am I over looking ?


Jul 27 '06 #2
Jon Vaughan wrote:
I have a static struct :

public static LocationPaging stLocationPaging = new LocationPaging();

Which is made up from :

public struct LocationPaging
{
public int Page;
public int MaxPage;
}
Which is used to control the paging of some records. BUT , when I call :

Scanning.stLocationPaging.Page = Scanning.stLocationPaging.Page + 1;
The increment doesn't occur, im quite new to C# , but im sure I have done
this in VB.Net with no issues. What am I over looking ?
Could you post a short but complete example which demonstrates the
problem? Here's a short but complete example which shows it working:

using System;

public struct LocationPaging
{
public int Page;
public int MaxPage;
}

class Test
{
static LocationPaging Foo;

static void Main()
{
Test.Foo.Page = Test.Foo.Page+1;
Console.WriteLine (Test.Foo.Page);
Test.Foo.Page = Test.Foo.Page+1;
Console.WriteLine (Test.Foo.Page);
}
}

Jon

Jul 27 '06 #3
Well, as presented it seems to just about work for me - i.e. the value
increments, but I suspect you are hitting one of the following:

1: value-type handling: if the struct is accessed e.g. through a
property, then the value will be cloned, with you updating the separate
copy
2: volatility: another thread looking at this field directly in a tight
look may not notice the change i.e. the value might be in a register

My first question would be: why is this a struct? It feels like it
should be a class, since you seem to want to share it between callers
(which structs are not geared up to do).

Also - (advisory only) - a: accessing fields directly (rather than
through properties) isn't all that recommended; b: since it is static
you may need to make this thread safe (synchronised access) if you are
ever going to do anything complex with threading, otherwise various
gotchas apply. Sounds like this may not be an issue.

But try making it a class; I suspect this will fix everything.

Marc

Jul 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Dmitriy Iassenev | last post: by
1 post views Thread by Alexander Inochkin | last post: by
reply views Thread by ivb | last post: by
1 post views Thread by Nicholas Palmer | last post: by
19 posts views Thread by david | last post: by
20 posts views Thread by Pilcrow | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.