|
I have the following class structure - as you can see the nested class has an additional Font that needs disposing.
I'm not too hot on Disposal techniques (the laziness of the managed world!), is the method I've used sufficient and 'safe'?
public class MyClass : IDisposable
{
private MyNestedForm myNestedForm;
public MyClass()
{
myNestedForm = new MyNestedForm();
}
public Font XYZFont
{
set { myNestedForm.XYZFont = value; }
}
public void Dispose()
{
myNestedForm.Dispose();
}
internal class MyNestedForm : Form
{
private Font xyzFont;
public Font XYZFont
{
get
{
if(xyzFont==null)
return Control.DefaultFont;
else
return xyzFont;
}
set { xyzFont = value; }
}
protected override void Dispose(bool disposing)
{
if (xyzFont != null)
xyzFont.Dispose();
base.Dispose(disposing);
}
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Looks OK to me. BTW, why are you exposing the nested class to outside of MyClass ?
|
|
|
|
|
Cool - thanks
N a v a n e e t h wrote: exposing the nested class to outside of MyClass
I haven't totally decided whether to keep the class nested or not. If I keep it nested I'll change it to private, but if I move it back outside (where it was originally) then it will need to stay internal.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Usually, when you override the Dispose(bool disposing) method, you do it like the following:
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (xyzFont != null)
xyzFont.Dispose();
}
base.Dispose(disposing);
}
A better explanation here[^].
DaveyM69 wrote: I'm not too hot on Disposal techniques (the laziness of the managed world!),
Don't do that, this is bad programming. In fact, I am tired to fix the code written by lazy programmers. If you are holding expensive resources, you give your clients a change to do cleanup when appropriate by calling your Dispose() method.
|
|
|
|
|
Le Centriste wrote: Don't do that
I know, that's why I was wanting to check before getting in to this too deep.
"disposing: true to release both managed and unmanaged resources; false to release only unmanaged resources."
I should have noticed that and put the managed stuff inside a disposing check - thanks for pointing it out.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I'm having a bit of a problem getting the Unicode version of this:
((char)((int)'a' + new Random().Next(26))).ToString();
The above snippet returns a random lowercase character. I'm trying to do the same thing for Unicode Characters in the range of 0621 - 064A
Suggestions?
If the post was helpful, please vote!
Why won't the worm just leave me be?
|
|
|
|
|
((char)((new Random()).Next(0x621, 0x64A + 1))).ToString();
|
|
|
|
|
Same problem. The problem being char apparently doesn't cover the letters I want to it simply returns a "?"
Thanks though
If the post was helpful, please vote!
Why won't the worm just leave me be?
|
|
|
|
|
A wild guess: Maybe the Unicode char is in a font you don't have installed.
|
|
|
|
|
Nope, Arabic would be a staple of my system being as about 80% of my work is in Arabic (front end)
The problem is that whenever I try to cast the numeric unicode value of the char seems to "truncate" it which is very strange considering char is a unicode character.
If the post was helpful, please vote!
Why won't the worm just leave me be?
|
|
|
|
|
Maybe changing the encoding might give a different result. UTF-8 uses one byte for the range 0-127, but more bytes for other character sets. UTF-16 uses two bytes for all characters, but is limited to the Basic Multilingual Plane (BMP). The BMP covers most modern languages, including Arabic I'm sure.
|
|
|
|
|
Snippet please?
If the post was helpful, please vote!
Why won't the worm just leave me be?
|
|
|
|
|
|
Hi,
I am doing a web application. In this I have check boxes for the child nodes. I want to check all the check boxes at a time by default. Is there any property to check all the check boxes in a tree view control. have to do this by default in a page. Please if anyone have work please reply me.
Thank you.
|
|
|
|
|
Wrong forum, try this: ASP.NET forum[^]
If the post was helpful, please vote!
Why won't the worm just leave me be?
|
|
|
|
|
Hi All,
Image I have a collection of items that is populated when a load function is called. This load function can be called multiple times based on user interaction.
My questions are...
Should I clear the current instance of the collection at the start of each load or should I simply create a new instance each time?
Are there any benifits or disadvantages to using one method over the other?
I cant image its going to make much difference either way... but you never know what you dont know
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
AFAIK using the same collection instance each time is a better way. While creating a new instance will force to use new heap space, clearing the existing instance will avoid this. I read about this topic when a new object was created in a for-each-loop, just to do a calculation. The author of the article mentioned to use the same instance but call a reset()-method within the loop.
Regards
Sebastian
|
|
|
|
|
cool, that makes sense
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Try both and see. I usually re-use.
|
|
|
|
|
A hi to all programming gurus
i am an amature in network programming i have written an simple program for making a client and server in c#
the cleint sends a msg and the server displays it in the listbox(lbconnections)
The Problem is that----
this application runs perfectly after being deployed and afterward when i again use it it crashes (port already bieng used error)
plz help me with this plzzzzzzzzzzzzzzzzzzzzzzzz..............
********************************coding for server*******************************************
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void recieve()
{
try
{
UdpClient server = new UdpClient(8080);
while (true)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
byte[] recieved = server.Receive(ref ip);
string returndata = Encoding.ASCII.GetString(recieved);
listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString());
}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread server = new Thread(new ThreadStart(recieve));
server.Start();
}
}
}
***************************************CODING FOR CLIENT*********************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace ser_cli
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient client = new UdpClient();
client.Connect(tbHost.Text, 8080);
byte[] send = Encoding.ASCII.GetBytes("HELLO WORLD?");
client.Send(send, send.Length);
}
}
}
|
|
|
|
|
Well, you forget to close the server port when you app shuts down.
|
|
|
|
|
hi SeMartens first of all thx for replyin
but frankly speakin i checked out ur article but cudnt catch a word can u explain me in simpler words
plzzzz....
..
help me with the solution
|
|
|
|
|
Okay, let me try.
Your server opens a socket at port 8080 with
byte[] recieved = server.Receive(ref ip);
This socket will be open all the time your program is running. You have to close this socket when your application exits.
So you have to use following instruction:
server.Close()
Regards
Sebastian
|
|
|
|
|
hey sebastian thx for ur reply
.
i tried that it still says only one usage of each socket is allowed
************************the new code for server************************************************
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace server
{
public partial class Form1 : Form
{
UdpClient server;
Thread thdserver;
public Form1()
{
InitializeComponent();
}
public void recieve()
{
try
{
server = new UdpClient(8080);
while (true)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
byte[] recieved = server.Receive(ref ip);
string returndata = Encoding.ASCII.GetString(recieved);
listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString());
}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//recieve();
thdserver = new Thread(new ThreadStart(recieve));
thdserver.Start();
}
private void button2_Click(object sender, EventArgs e)
{
server.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
server.Close();
}
}
}
*********************the new code for client****************************************
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace ser_cli
{
public partial class Form1 : Form
{
UdpClient client = new UdpClient();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(hear));
th.Start();
}
private void hear()
{
client.Connect(tbHost.Text, 8080);
byte[] send = Encoding.ASCII.GetBytes(textBox1.Text);
client.Send(send, send.Length);
}
private void btnStop_Click(object sender, EventArgs e)
{
client.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
client.Close();
}
}
}
|
|
|
|
|
Check that every instance of the app is closed before you start a new try.
By the way, does something else is running at port 8080? Maybe Apache Tomcat or something similar? Could be that your app is in conflict with another one.
Regards
Sebastian
|
|
|
|