|
You're using an inaccurate clock. The Windows tick timer (Environment.TickCount ) ticks at a frequency of around 15-16ms, so you will normally see durations measured with this timer as being some multiple of 16ms. Try using the Stopwatch class instead, if using .NET 2.0 or later.
That said, UDP is always susceptible to network delays. Routers can delay or drop packets if a link is saturated. LANs will drop packets and have to retransmit if two stations try to send at the same time (this is referred to as a collision). You would need a dedicated link to avoid the possibility of a collision.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
nope, i'm using the multimedia timer, which is just as accurate as the Stopwatch.
|
|
|
|
|
Hello everyone,
Here is my code to make a duplicate value copy of all the Values in a dictionary instance -- not just reference. So, I use CopyTo method.
My current issue is, I think my code is stupid but do not know how to solve it. My specific concern is,
1.
the code Foo[] FooArray = new Foo[3] creates 3 new instances of Foo, the code dic.Values.CopyTo(FooArray, 0) will overwrite the 3 new instances of Foo? So, seems it is stupid to create 3 new instances of Foo in Foo[] FooArray = new Foo[3], but never use them and overwrite them? Any ideas? Is it an issue?
2.
Maybe I am wrong, the code Foo[] FooArray = new Foo[3] creates only references variable of Foo class? Not class instances variable themselves?
class Foo
{
public int abc;
public Foo (int i)
{
abc = i;
}
}
public static void Main()
{
Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
dic.Add(1, new Foo(10));
dic.Add(1, new Foo(20));
dic.Add(3, new Foo(30));
Foo[] FooArray = new Foo[3];
dic.Values.CopyTo(FooArray, 0);
return;
}
thanks in advance,
George
|
|
|
|
|
George_George wrote: the code Foo[] FooArray = new Foo[3] creates 3 new instances of Foo
Buzz! Wrong!
It allocates an array with 3 spaces for Foo. It does not instantiate any Foo instances, only an instance of Array . Dont believe me? Try accessing one of them:
Foo[] FooArray = new Foo[3];
Console.WriteLine(FooArray[0].abc.ToString());
|
|
|
|
|
Thanks J4amieC!
So this code Foo[] FooArray = new Foo[3] will only create 3 references and make them refer to NULL? Then code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class, and assign the 3 NULL references variables in FooArray to point to the instances?
regards,
George
|
|
|
|
|
George_George wrote: So this code Foo[] FooArray = new Foo[3] will only create 3 references and make them refer to NULL
Exactly
George_George wrote: hen code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class
No, you're doing that above with lines like
dict.Add(1,new Foo(10))
You're then copying references to these into the array. Each Foo is only created once.
|
|
|
|
|
Thanks J4amieC!
1.
I am confused why do you think the code "dic.Values.CopyTo(FooArray, 0)" is the same as the code "dict.Add(1,new Foo(10))"? I think code "dic.Values.CopyTo(FooArray, 0)" will fill array FooArray, and "dict.Add(1,new Foo(10))" will fill dic dictionary instance. They are of different types.
Why do you think they are of the same effect?
2.
"code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class" -- what is wrong? I think not the reference, but the real new copy is created?
regards,
George
|
|
|
|
|
George_George wrote: I am confused why do you think the code "dic.Values.CopyTo(FooArray, 0)" is the same as the code "dict.Add(1,new Foo(10))"
I never said that. Lets go back a bit. You said:
"Then code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class"
and I said that this was not the case, that the code in the lines above are what actuall instantiated the Foo instance, not the act of using the CopyTo method.
George_George wrote: I think not the reference, but the real new copy is created?
Im 99% sure you're copying the references not making a new instance.
|
|
|
|
|
Thanks J4amieC!
1.
"that the code in the lines above are what actuall instantiated the Foo instance" -- sorry my English is not good. And I am confused about, "the code in the lines above" -- which code do you mean? Could you list here please?
2.
J4amieC wrote: Im 99% sure you're copying the references not making a new instance.
I think you are correct. I have written code to verify. Could you help to review whether my code is correct to test only reference, not instance value themselves are copied please?
class Foo
{
public int abc;
public Foo (int i)
{
abc = i;
}
}
public static void Main()
{
Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
dic.Add(1, new Foo(10));
dic.Add(2, new Foo(20));
dic.Add(3, new Foo(30));
Foo[] FooArray = new Foo[3];
dic.Values.CopyTo(FooArray, 0);
FooArray[0].abc = 40;
Console.WriteLine(dic[1].abc);
return;
}
thanks in advance,
George
|
|
|
|
|
Yeah, the code is fine for the test.
|
|
|
|
|
|
A new object is only instantiated when it is contructed with 'new' else they are references.
|
|
|
|
|
Thanks leppie,
I think CopyTo will create new instances, other than just make a copy of the reference, right?
regards,
George
|
|
|
|
|
George_George wrote: I think CopyTo will create new instances, other than just make a copy of the reference, right?
Buzz! Wrong Again!
Here's a little test app to convince you:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace ConsoleApplication2
{
class Foo
{
public int abc;
public Foo(int abc)
{
this.abc = abc;
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<int, foo=""> d = new Dictionary<int, foo="">();
d.Add(1, new Foo(10));
d.Add(2, new Foo(20));
d.Add(3, new Foo(30));
Foo[] fooArray = new Foo[3];
d.Values.CopyTo(fooArray, 0);
Console.WriteLine(d[1].abc);
Console.WriteLine(fooArray[0].abc);
Console.WriteLine("Same Instance? {0}", (d[1] == fooArray[0]));
Console.ReadLine();
}
}
}
</int,></int,>
Output:
10
10
Same Instance? True
This implies that I have picked the Foo instances with the same abc value (one from the dictionary one from the array), and when I check the for equality (which checks the reference not the value of abc it comes out true.
|
|
|
|
|
|
It will make copies of the references. Not new objects, hence they will be ReferenceEquals.
|
|
|
|
|
Cool leppie!
When dealing with "==" and Equals, how could we judge whether it compares with address equality (i.e. pointing to the same object instance) or compare for the content/value (e.g. Equals from IEquatable interface)?
regards,
George
|
|
|
|
|
Hi All,
I am trying to read a XML file which contains "=" as value of one of the attribute. The compiler throws following error when i am loading this XML using XMLDocument.Load().
'=' is an unexpected token. The expected token is ';'.
Following is the position where i get the error :
<tab name="Blogs" key="http://www.ournhs.nhs.uk/?cat=3&feed=rss2" />
key attribute contains "=" which throws the error.
Can anyone pls guide me in reading this xml.
Cheers
Mani.
First have the WILL then u WILL
modified on Monday, May 19, 2008 7:50 AM
|
|
|
|
|
Its the ampersand thats the problem. & must be encoded as & when writing the document. If the document has been created with an actual & on its own then its not valid Xml.
modified on Monday, May 19, 2008 8:57 AM
|
|
|
|
|
Jamie has the right answer but didn't double up the escaping! & must be encoded as & when placing a URL with query parameters in an XML file (or anywhere else an & character appears).
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
Hi,
I tried to display an image in an image control in VS 2005, every other thing works except the displaying of the image. However, it works with VS 2003. Here's my code:
protected void uploadButton_Click(object sender, EventArgs e)
{
string fileName = FileUpload1.PostedFile.FileName;
fileName = Path.GetFileName(fileName);
string fileFolder = Server.MapPath("~/images/");
string filePath;
bool fileOk = false;
if (FileUpload1.PostedFile.ContentLength > 0)
{
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToString();
string[] allowedExtensions = { ".gif", ".jpeg", ".jpg", ".png" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOk = true;
if (fileOk == true)
{
if (!Directory.Exists(fileFolder))
{
Directory.CreateDirectory(fileFolder);
}
filePath = fileFolder + fileName;
if (File.Exists(filePath))
{
msgBox.Text = fileName + " " + "already exists on the Web server";
return;
}
FileUpload1.PostedFile.SaveAs(filePath);
Image1.ImageUrl = filePath;
I need help on how to display the image.
Thanks.
Obinna from Nigeria.
//obinnaaj Eof
|
|
|
|
|
|
We are just about to start a new project that is going to get very big!!
In the past i have always used a different assembly for Business objects (with the DAL inside) and presentaion.
We have been thinking about how to structure the new app, and thought about having the DAL and BO in seperate assemblies (to spread the code out), but they need to refernce each other which you cant do.
How do you manage your code files? do you use assemiles or namespaces?
Any good articals that you have seen?
Thanks
|
|
|
|
|
mark_w_ wrote: do you use assemiles or namespaces?
Yes. DAL dll is imported into BLL dlls. BLL dlls are imported into presentation layer.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for your VERY quick reply ,
but
if i have a singleton in my BLL, and to create it i need to connect to the database (via the DAL), how would i do that?
Edit, the reason i need to do this is to read in what application level permissions the user has once so i dont have to keep going back to the database
modified on Monday, May 19, 2008 6:59 AM
|
|
|
|