|
Hi folks,
OK, what I'm interested in are the technical pros and cons of data initialisation in a classes.
I.e. which is better?
class Test<br />
{<br />
private int mInt = -1;<br />
Test()<br />
{<br />
}<br />
}
or
class Test<br />
{<br />
private int mInt;<br />
Test()<br />
{<br />
mInt = -1;<br />
}<br />
}
The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way.
But are there technical reasons not to do this?
Dr Herbie
Remember, half the people out there have below average IQs.
|
|
|
|
|
I think that the first method is much better too, because it is possible that you have problems to initialize the variable at a later time.
(perhaps in a try-catch chapter -> than you are not able to use the new instanced variable out of this chapter)
With the first method a variable is certainly instanced which is easier to handle.
So it´s also easier and safer to check if a variable is set to an invalid value than to check if this variable is instanced.
So if I´m wrong please rectify me...
Norman-Timo
|
|
|
|
|
I don't believe there is any technical reason to prefer the second method (Unless you're a VB programmer;P). As you point out the first method gaurantees the variable will be initialed to a useful value no matter which constructor you use.
|
|
|
|
|
|
|
It compiles down to the same thing. If you don't declare a default constructor, one is defined for you anyway and instance fields are initialized in it.
The only difference is that instance fields initialized outside the default constructor - when compiled - are initialized before base.ctor() is called. Any fields you initialize explicitly in your default constructor are initialized after the call to base.ctor() (compiled into your default constructor automatically).
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Thanks for the replies, it has put my mind at rest
Dr Herbie
Remember, half the people out there have below average IQs.
|
|
|
|
|
Hi All,
I hava developed a Addin which has a Custom toolbar. and also have a VBA Application embeded in the same Excel Object. in this scenario my custom toolbar flickers continusly.
Any please help me.....
Adv Thnaks
Praveen Kumar Kella
|
|
|
|
|
Hi All,
I had some problem with Information Bridge Framework. The problem is I have developed a COM Addin for Office Excel 2003. this addin working fine until unless I install Information Bridge Framework in my system. It is giving some strange error while loading the Addin in to Excel.
Eror Details:
1) Exception Information
*********************************************
Exception Type: System.InvalidOperationException
Message: There is an error in the XML document.
TargetSite: System.Object Deserialize(System.Xml.XmlReader, System.String)
HelpLink: NULL
Source: System.Xml
StackTrace Information
*********************************************
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at MS.IT.ReportingServices.Addin.AddinConfiguration.Init()
at MS.IT.ReportingServices.Addin.AddinConfiguration.get_Instance()
at MS.IT.ReportingServices.Addin.Connect.OnConnection(Object application, ext_ConnectMode connectMode, Object addInInst, Array& custom)
Some one help me on the same
Adv Thnaks
Praveen Kumar Kella
|
|
|
|
|
|
Hi. How can I show a message when I get an exception in a web service? In other apps I use the Message property of the exception object, but with the web service I don't know how to do that.
How do you show exception information if the error comes from a web service?
Regards,
Diego F.
|
|
|
|
|
Since the nature of the Web Service is like a 'server' process which runs in the background and caters users requests silently, it is best suited to
a. Log the error in local text/xml files, windows event log which can be monitored by the IIS Admin
b. If a particular WebMethod gives errors/exceptions you should inform the user about the failure using some <errorcode> tag being set in the returned xml.
|
|
|
|
|
If you are invoking the web Service with C++ Code like
pWSClient = new COleDispatchDriverWrapper;
if(pWSClient->CreateDispatch("MSSOAP.SoapClient30"))
pWSClient->mssoapinit((LPCTSTR)pWebServiceParams->csWebServiceLink, (LPCTSTR)pWebServiceParams->csServiceName, (LPCTSTR)pWebServiceParams->csServicePort, NULL);
You can catch exception such as (in the given order)
catch(COleException *eCOleException)
catch(COleDispatchException *eCOleDispatchException)
catch(CException *eCException)
catch(_com_error &ecom_error)
catch(...)
|
|
|
|
|
|
Hello everybody!
I want to have a permanent data transfer to another machine via TCP/IP Socket Communication. I actually coded a solution which will work fine. But one thing does not work:
In my code the SocketServer does not recognize that the client has disconnected. How can I recognize this?
In the case that the client disconnected, my code is waiting for Data and there is logically no dataflow anymore.
Here´s the important extract from my code:
[STAThread]
public static void Main()
{
Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint myEndPoint = new IPEndPoint(IPAddress.Any, 65000);
mySocket.Bind(myEndPoint);
try
{
mySocket.Listen(20);
Socket sa = mySocket.Accept();
byte[] buffer = new byte[5];
string tmpString = "";
NetworkStream myStream = new NetworkStream(sa, true);
while (sa != null)
{
if (myStream.DataAvailable)
{
int read = 0;
read = myStream.Read(buffer, 0, buffer.Length);
tmpString = Encoding.ASCII.GetString(buffer, 0, read);
Console.WriteLine(tmpString);
}
}
}
catch (SocketException sockErr)
{
Console.WriteLine("Socket Exception: ", sockErr.Message);
}
catch (Exception err)
{
Console.WriteLine("Exception: ", err.Message);
}
mySocket.Close();
}
Thanks for any help
Norman-Timo
|
|
|
|
|
Norman-Timo wrote:
In the case that the client disconnected, my code is waiting for Data and there is logically no dataflow anymore.
Ideally this is what a server should do! Other options:
- poll the client by sending an "empty" packet, a socket exception will occur if the connection is dead.
- ask the client to send some info that it has disconnected, before disconnecting.
top secret xacc-ide 0.0.1
|
|
|
|
|
Yes the server should do so
But I tested this scenario:
I made a connection to the server.
Then I send some data and disconnected from server.
The server was waiting then.
I tried to make another connection from same client, which worked.
But the sended data did´nt reached the server anymore, while he is waiting and waiting...
I think I have to recognize that the connection was disconnected, so that the socket could set to Listen-Mode again and catches the connection witch an Accept() Method.
But while the server is waiting no data transfer is possible anymore.
Yeah polling is maybe a solution, but there should be a property to check if TCP/IP-Partner is available (such like that one on client side with: 'mySocket.Connected' or so...)
So please help me more
Norman-Timo
|
|
|
|
|
Hehe.
There IS a "Socket.Connected" property for Sockets.
I suspect that you are actually WAITING on that socket.
Do not do that. As far as I understand your problem, you have ONE Socket. That is, to say the least, not what anyone expects of a server.
Try understanding the concept of either synchronous sockets OR asynchronous sockets. If you choose the latter, you wont have to do any "manual multi-threading".
My last project at work was also a server waiting on TCP connections.
There is no "worst-case-handling" in there yet, but even if (due to network error or other problems) I lost a few clients, it would not block the whole thing down.
I am using asynchronous sockets, which means at runtime the necessary threads are auto-magically created in a threadpool.
The whole thing is based on the MSDN "non-blockingserverexample".
What you can do to check if your client still is connected: check socket.connection and socket == null (with proper capitalization, of course) or try sending something to your client. You will get an exception if your client is not connected.
I do have one advantage:
Once a client has connected, he will stream his requests one at a time (receive, process, answer - thats what I do) and each request has a length field in the leading five bytes.
So, if a Client has connected, I will immediately receive 13 byte from him, process his request, send my response, then receive the next 13.
I KNOW that a client who did not send me the proper shutdown package and has not responded for, lets say, thirty seconds, must be dead. So I can run over my (yet to be implemented) hashtable every thirty seconds and check for timeouts. That way, it will take less than sixty seconds for any dead sockets to be removed.
|
|
|
|
|
Hey thanx!
But in my case (I forgot to write this!) I only want to communicate with a static partner. And in my case the communication should never be disconnected, and there is only one case if connection fails: socket error.
Only if I start my application I bind the socket to my IPEndpoint and set the Socket to Listen and wait for this client response.
After client is responsing the connection is hold and should never end.
But there are many reasons why the connection breaks: perhaps the cable was disconnected, or the comm. partner has to reboot.....
But nothing helps, so I understand, I have to test the communication and send a "null" package after every certain timespan.
Ok thanx to all Your
Norman-Timo
|
|
|
|
|
In any of the languages like C++, Java, C# there are provisions to cast an object to Type A to an object to Type B. To complicate the matter further, there are various types of casting options provided. It is simple to say and perhaps to imagine the internals when: I (down)cast an integer to float but what happens when I (up)cast? What magic the compiler performs when we cast complex objects to each other.
|
|
|
|
|
No magic! I suggest you read up on polymorphism, inheritance, and numeric conversions, all part of the OO paradigm.
top secret xacc-ide 0.0.1
|
|
|
|
|
The compiler knows how he stores every variable.
The programmer of that compiler just parses for an explicit cast (like (int)myvar) and then says: well, I have to shove these first bits into a new integer-style, then turn them around and here we go....
Casting is all about conversion between different bit arrangements.
Everything is stored as bits in your memory. By declaring variables or designing your class, you also did a description of how these objects or whatever are stored in memory.
Of course, for SOME types, there is no logical conversion. It DOES NOT MAKE SENSE to cast a string into an integer or the other way round.
In these cases, you need to PARSE your stuff, which takes much longer (microcode-wise) than simply rearraning your bits in a given pattern.
Really, just think about it. Everything is bits. You have an signed integer and want to cast it into an unsigned long. What do you have to do?
basically, substract one, then xor with all ones (like 01010011 ^ 11111111)
you then have the "normal" representation of your number, now just remove the sign and...
And so on.
Computers work by rules. If you know these rules, you just have to write them down into a program.
Still, writing a compiler is something I wish I never have to do.
Cheers
Sebs
|
|
|
|
|
Thanks Sebs, this gives some insight. Pretty helpful.
|
|
|
|
|
Thx!
We just need a dynamic array but not a ArrayList
|
|
|
|
|