• Content count

  • Joined

  • Last visited

Community Reputation

234 Neutral

About JamesLewis

  • Rank
  1. [.net] Test Record and Playback

    We use Rhino Mocks at work, really good, can be really simple but gets complicated with more complex unit testing. Depends what you're doing. James
  2. [.net] Transparancy in C#

    Setting the background colour of an Image to Transparent shouldn't make the image transparent. It will make the Image place-holder control's background colour equal the background colour of the control underneath it. Quote: I'm currently writing a program that involves a pretty big background image and multiple images on top of them. Some of the top images have mouse over events (as well as click events) but need to be 'invisible' to the user until the cursor is over them. Right now when I set the top image to transparent it actually shows the form background. In other words, it's ignoring the middle image that's between the form background and the top-most image. I'm not 100% sure I get what you mean. Am I right in thinking this: You have a background image (always visible) with a layer of selectable images over the top (the middle layer). Then you have an invisible top layer of images that only display themselves when the relevant middle layer image is selected. The selection is done by the user passing the mouse over the top of one of the middle layer images. If so then in this situation you should definately use the Visible property. Set all the top layer images Visible properties to False by default. Then when the mouse rolls over the middle image, set the top layer image Visbile to True. If this isn't what you meant then sorry. I still think that using the visible property would work though. Just because you can't see the image doesn't make it unusable by the user - all the events will still fire. Cheers, James
  3. Perhaps this: http://dnchannel.blogspot.com/2007/09/getting-line-numbers-in-exception-stack.html and this: http://forums.msdn.microsoft.com/en-US/csharplanguage/thread/a58dc2a0-0612-407b-8cbe-10f1784ba85a/ will help! Hope you get it working, James
  4. about an error message ...

    [quote]Original post by myriac foreach (maze ball in listarrows) { foreach (maze ball2 in listarrows) { if (ball != ball2) { if (ball.Position == ball2.Position) { this.Exit(); Console.WriteLine("hello"); listarrows.Remove(balls); } } } } [/quote] Again, you are trying to remove an item from a list that you are currently iterating through. As described in the above posts, you cannot do this for various reasons. You should replace the above code with the code example that c43892 supplied. This is not great performance wise but will do exactly what you want it to. You could also do something like this: // Create a temp list to loop through ArrayList tempListarrows = listarrows; foreach (maze ball in tempListarrows ) { foreach (maze ball2 in tempListarrows ) { if (ball != ball2) { if (ball.Position == ball2.Position) { this.Exit(); Console.WriteLine("hello"); listarrows.Remove(balls); } } } } Hope it helps, James EDIT: I've just looked at your code and just out of interest why do you have this.Exit in the collision code? Also, I think you'll find that the chances of ball.position and ball2.position being equal are quite low. You may want to look into the BoundingBox objects in XNA - I think you would find these very usfull. Lastly, inside the collision code you have .Remove(balls) - what's "balls"? If you want to remove both balls then do 2 remove operations (.Remove(ball1); .Remove(ball2);).
  5. Working with Files and Directories

    Quote: The problem that I have is that I have to tie to binary files together. The first file will setup some some thing like a Category. Then the 2nd Binary File will hold the Category Subs will all the information needed to access the directories. If you get my meaning. I'm not sure if I understand you here - you have to tie 2 binary files together? Can you explain why it is that you require 2 binary files - I'm being slow and can't follow your post! Anyway, I personally like implementing ISerializable to quickly read / write files from / to disk in binary (I also think this is the standard for this type of operation?). So the following example is an object that implements ISerializable: [Serializable()] public class MySerializableObject : ISerializable { #region Some Properties To Be Serialized // A string property public string SomeString{ get; set; } // A List property public List<object> SomeList{ get; set; } #endregion #region Constructors // Default constructor public MySerializableObject { } // Consrtuctor used when deserializing the object public MySerializableObject(SerializationInfo info, StreamingContext context) { // Read the properties in from the serialization info list this.SomeString = (string)info.GetValue(string.Format("SomeString"), typeof(string)); this.SomeList = (List<object>)info.GetValue(string.Format("SomeList"), typeof(List<object>)); } #endregion #region ISerializable Members // Method used when serializing the object public void GetObjectData(SerializationInfo info, StreamingContext context) { // Add the values of the properties to the serialization info list info.AddValue(string.Format("SomeString"), this.SomeString); info.AddValue(string.Format("SomeList"), this.SomeList); } #endregion } The two important parts of that class are the GetObjectData method and the special constructor that takes a SerializationInfo and StreamingContext as arguments - these are used when serializing / deserializing the objects data. Now you need some methods that read / write the data from / to binary files. The following code is for writting the data to disk using a Binary Serializer: /// <summary> /// Serialises an object to a binary file. /// </summary> /// <param name="value">Object to be serialized.</param> /// <param name="fullFileName">File name of the file ro serialise to.</param> public static void SerializeBinary(object value, string fullFileName) { // Create a new filestream and initialise to null. Stream fileStream = null; // Try to serialize the data try { // Try to create the directory if its not there Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullFileName)); // Create a filestream and name the file the object will go into fileStream = File.Open(fullFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Delete); // Create binary formatter to serialise the data BinaryFormatter binaryFormatter = new BinaryFormatter(); // Serialise the data binaryFormatter.Serialize(fileStream, value); } // Always clean up the stream after finishing the method finally { // Check for null on the filestream if (fileStream != null) { // Flush and close the filestream fileStream.Flush(); fileStream.Close(); } } } That above method is a generic one I use sometimes and should work for any object that implements ISerializable correctly. The following method is for deserializing the data into the example object I provided in the first code snippet: /// <summary> /// Deserialises a MySerializableObject object from a binary file. /// </summary> /// <param name="fullFileName">The file name of the file to deserialise from.</param> /// <returns>A new MySerializableObject object populated from the file.</returns> public static MySerializableObject DeserializeBinary(string fullFileName) { // Create an object to return MySerializableObject newReadData = null; // Create a filestream and a binary formatter Stream fileStream = null; BinaryFormatter binaryFormatter = new BinaryFormatter(); // Try to deserialize the data try { // Open the file exclusively fileStream = File.Open(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None); // Deserialize into a MySerializableObject object newReadData = (MySerializableObject)binaryFormatter.Deserialize(fileStream); } // Always close the streams when finished finally { // Close the streams if(fileStream != null) fileStream.Close(); } // Return the object - returns null if something went wrong return newReadData; } Hope that helps, James EDIT: I really would strongly advise you to take a look at the MSDN documentation - there are loads of examples on manipulating files (XML / Binary etc) on there.
  6. Working with Files and Directories

    Hi, If you want to Check / Create a directory then you could do the following: Check if the directory exists and if not, create it like this: if(!Directory.Exists(Path.Default.FilesPath)) Directory.CreateDirectory(Path.Default.FilesPath); However, I think if you read the MSDN documentation, CreateDirectory can be used on its own - if the directory is there it does nothing, if its not it'll create it. If you want tutorials on working with Binary files and file IO, the MSDN has loads of documentation. Hope that helps, James
  7. VB.NET: Properly terminating socket connection help

    Quote: Whether I close the client and reopen it or I try to disconnect using clientsocket.close, when I try to reconnet I get the error "Cannot access disposed object. Object name: System.Net.Sockets.TcpClient" When you call the Close() method you are calling the Dispose() method too (Close usually calls dispose - see the dispose pattern for more information on this). So you are getting this exception because you have created an object, disposed of it and then tried to use it again after its been disposed / probably garbage collected. However, I would say you are correct in calling the Socket.Close() method in your Disconnect method - your problem lies within your Open or Connect method code. In the Open or Connect method of your application make sure you create a new Socket and then open it. Then in the close method, dispose of the socket like you have been doing. Your code may look something like this (NOTE: this is in C# but should be pretty similar): private TcpClient clientSocket; public Connect() { clientSocket = new TcpClient(); clientSocket.Open(); } public Disconnect() { clientSocket.Close(); clientSocket = null; } Hope that helps, James EDIT: I've just read through your first post properly and in answer to it, the above code is probably the best practice for opening / closing a socket connection with a server. However, if there is any method such as Disconnect() on the clientSocket then that should be called before the Close() method.
  8. The only thing I can think of as a quick solution is exactly what you said - Make another Vector3 class that is actually serializable. Or as a quick fix you could just add 3 double properties to that scene mesh class called X, Y and Z and then each one would return your_vector3.X / Y or Z. Then just add the attribbutes to those properties and not the Vector3 itself. Like this: [Serializable] public class SceneMesh { private Vector3 _pos; [XmlAttribute("name", typeof(string))] public string name{ set; get; } public Vector3 position { set{_pos = value;} get{return _pos; } } [XmlAttribute("x", typeof(double))] public double posX { get{ return _pos.X; } } // ... etc etc } Hope it helps, James
  9. [.net] Random slowness

    Use perfmon to check the memory usage - this is really good to see what happens at the beggining of your application and throughout its lifetime. Quote: Quote:If you run a program the first time and it's slow, if you run it again is it faster? Or is it just completely random? I can't recall a time when I've run it again and it was still slow, but it's hard to draw any conclusions from that. From what you've described I'd guess that something is being loaded on startup - hard to say what right now as there's no code or description of the applications. I would have guessed it was something GUI intensive or a large data set but you've mentioned that some are just console applications. As an example, I have a mapping application that runs a little slow the first time you run it due to the huge data sets it has to load then after that its fine. Definately worth running perfmon on it (you'll have this on your PC already) just to check what happens in memory during the running of your application. Then I'd suggest something like the CLR Profiler (not sure if that link is the right one!) to get a more indepth look. Post your findings, I'd be interested to know what it is. James
  10. VB.NET: Create/Send/Interpret Array

    Quote:"How do I create an array with no defined size (or would I just reDim the array to 1 more each time), and then how do I add data into that array?" Use a List or some other similar container to store your data in. have a look on the MSDN (here) to find out about lists. If you want to add data to it you can use List.Add(), however, from what you've described you may be better off clearing the list completely (List.Clear()) then using List.AddRange() to add the data returned by the server. Now, I know this is your first Networked program but the application you have described would fit perfectly (and reletively easily) into the WCF The WCF is very simple to use if you want it to be and there's tons of documentation on the internet on how to use it. You can pass List objects between the server and client and you can also use events to signal all the clients when a new user has logged onto the server. Hope this helps, James
  11. C# equivalent of RECT in C++

    When you're drawing the System.Drawing.Rectangle do you not have to draw it onto a GDI+ Graphics Object? - Perhaps this is why it doesn't seem to work? I could be wrong as I don't work with the GDI so I'm partly guessing. Good luck! James
  12. [.net] Random slowness

    Quote:Original post by ibebrett is it possible the garbage collector is kicking in ? Again, if the OP uses perfmon (or something similar) then the graph they get should indicate this nicely - you see little spikes every few mins or so indicating when the garbage collector kicks in. However, I've never had it affect any of my applications in such a way that it noticably slows them down. James
  13. [.net] Random slowness

    Have you used perfmon to see how much memory your applications are using? If you get a graph with a line that goes up then thats bad times!
  14. [.net] Random slowness

    Without seeing an example of your code I couldn't comment on that side of things but it is well known that running an application through the debugger is much slower than running the final release .exe. If this problem only shows itself when running through the debugger then I wouldn't worry at all, however, 1/50th its normal speed does sound a bit odd. What type of application are you seeing this decrease in performance with? James
  15. Another way to do this is to use TableLayoutPanel controls to setup the layout of your User Control, then place all your Buttons / Panels etc inside the TableLayoutPanels and dock everything to Fill. This way is very quick and easy in the Visual Designer but can be extremely taxing on the processor at runtime as resizing embedded TableLayoutPanels is a heavy operation. I would also suggest the Anchor approach but just thought you might like another take on Form layouts. Also, look at the WPF (you'll need VS2005 extensions for .Net 3.0) - if it's what you need you'll love it, I think its awsome. Cheers, James