Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2008

Tip: Looking for answers? Try searching our database.

pass by reference ,value doesnt change

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anil TG - 09 Feb 2008 11:31 GMT
Hello,
Im dealing with a Client Server application with multiple Client support.
In that i'm using an integer for storing the number of clients..

On each Client connect i'm incrementing the value in a thread Safe manner like

  Interlocked.Increment(ref m_ClientCount);  
  //Now its value is say 1 ,after increment

In this i have a class

Class ClientConnect
{
    public int  _clientConnect ;
    public ClientConnect(ref int _clientconnect_F)
    {
        _clientConnect = _clientconnect_F;
    }
    public void change()
    {
          //Decrementing the value of clients..

          interlocked.Decrement(ref  _clientconnect_F);

          //the value should have reduced from 1 to 0 rite???
    }
}

im calling a thread  like this to invoke the function change()

    ClientConnect obj=new ClientConnect(ref m_ClientCount);
    Thread CxnManager= new Thread(new ThreadStart(obj.change));
    CxnManager.Start();

Now come to my problem..When i check the value of  m_ClientCount after the
above operation it seem to have the same value 1.it never changes.i tried
with
 _clientconnect_F--;     also
instead of   interlocked.Decrement(ref  _clientconnect_F); inside the
change();
But no change.Since i'v passed the integer via reference ,if i make some
changes to the formal parameter it should be reflected in the actual
parameter also..rite?..
But here its not happening..Any idea?...
Anil TG - 09 Feb 2008 11:44 GMT
Sorry i'v made a mistake in the question..
Inside the Change function iv written like
Interlocked.Decrement(red  _clientConnect_F);
But that was a mistake,Actually im trying to decrement the variable  
_clientConnect instead of  _clientConnect_F;

i must have written like
Interlocked.Decrement(red  _clientConnect);

Please note that..

> Hello,
> Im dealing with a Client Server application with multiple Client support.
[quoted text clipped - 40 lines]
> parameter also..rite?..
> But here its not happening..Any idea?...
Jon Skeet [C# MVP] - 09 Feb 2008 12:56 GMT
> Sorry i'v made a mistake in the question..
> Inside the Change function iv written like
[quoted text clipped - 6 lines]
>
> Please note that..

it will change the value of _clientConnect, but not of the caller's
_clientConnect_F. That's not how pass by reference works - all it means
is that *for the duration of that member* (the constructor, in this
case) changes to the parameter will be visible to the caller.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Anil TG - 12 Feb 2008 03:55 GMT
> > it will change the value of _clientConnect, but not of the caller's
> > _clientConnect_F. That's not how pass by reference works - all it means
> > is that *for the duration of that member* (the constructor, in this
> > case) changes to the parameter will be visible to the caller.

Is there any other way to change the value of m_ClientCount by passing it to
a function like this...?.

>"Jon Skeet [C# MVP]" wrote:

> > Sorry i'v made a mistake in the question..
> > Inside the Change function iv written like
[quoted text clipped - 11 lines]
> is that *for the duration of that member* (the constructor, in this
> case) changes to the parameter will be visible to the caller.
Jon Skeet [C# MVP] - 12 Feb 2008 07:36 GMT
> > > it will change the value of _clientConnect, but not of the caller's
> > > _clientConnect_F. That's not how pass by reference works - all it means
[quoted text clipped - 3 lines]
> Is there any other way to change the value of m_ClientCount by passing it to
> a function like this...?.

No. Well, not without creating an extra class which basically wraps an
integer.

Frankly, I'd find that behaviour confusing.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.