By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,335 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,335 IT Pros & Developers. It's quick & easy.

Calling methods from constructor?

P: n/a
Hi,

I have a class whose constructor accepts a Socket as an argument. The
constructor then makes a call to Socket.BeginReceive(). The callback
delegate passed to BeginReceive() is a method of my class. There is a slight
chance that the callback delegate will be called from the I/O thread pool
BEFORE my constructor completes. Is calling this delegate before the
constructor finishes a problem?

In C++, this type of arrangement was a problem as I recall. In C#, it
doesn't seem like it SHOULD be a problem. After all, I can call private
methods from my constructor so why shouldn't some external party be able to
call them in the midst of construction as well? In any case, I just wanted
to double-check!

Granny
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
To side-step this issue and possible others, you could (of course) create
..Send() public method or .Connect() and use that after construction. You
can still set the socket var in your constructor and/or property of the
class.
--wjs
"Grandma Wilkerson" <tu**@bottleneck.scalability.crash.dum> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a class whose constructor accepts a Socket as an argument. The
constructor then makes a call to Socket.BeginReceive(). The callback
delegate passed to BeginReceive() is a method of my class. There is a slight chance that the callback delegate will be called from the I/O thread pool
BEFORE my constructor completes. Is calling this delegate before the
constructor finishes a problem?

In C++, this type of arrangement was a problem as I recall. In C#, it
doesn't seem like it SHOULD be a problem. After all, I can call private
methods from my constructor so why shouldn't some external party be able to call them in the midst of construction as well? In any case, I just wanted
to double-check!

Granny

Nov 13 '05 #2

P: n/a
Grandma Wilkerson <tu**@bottleneck.scalability.crash.dum> wrote:
I have a class whose constructor accepts a Socket as an argument. The
constructor then makes a call to Socket.BeginReceive(). The callback
delegate passed to BeginReceive() is a method of my class. There is a slight
chance that the callback delegate will be called from the I/O thread pool
BEFORE my constructor completes. Is calling this delegate before the
constructor finishes a problem?


Well, it'll be a potential problem if the delegate relies on
information which is set up after Socket.BeginReceive() is called, but
I don't think it's fundamentally a problem. Probably worth documenting
very clearly though, for maintenance purposes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.