|
1 bogey at 11'!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
throw new ProgrammingQuestionInLoungeException(); But I'll give you a hint - write a loop and test each method 1000 times. Time it with a Stopwatch . In a managed language all your assumptions about performance are irrelevant, the JIT compiler may be optimising stuff all over the place. Make sure your running your test in release mode.
Simon
|
|
|
|
|
Ouch!!
I seem to have trodden on someones toes
It was meant to be rhetorical and to see what users preffered and why, not a technical (try timing it!) question.
Oh well, I thouhjt it was interesting...
---
Regards,
Martin.
|
|
|
|
|
'as' is used when the instance you are checking may not of the expected type. E.g Your expecting an 'object' but want it to be a specific type (or maybe one of many types) for a function parameter but the caller may pass in a type you dont want or expect, so you can treat is as null by casting it with the as keyword.
An example of this may be when overriding the Equals method on System.Object
The () cast method is more effecient becuase as compiles down to a set of IL instructions that include checking the type before attempting the cast, or returning null, eg:
Customer customer = null;
if(e.SomeObject is Customer)
customer = (Customer)e.SomeObject;
else
customer = null;
So, I would say 'as' has its place, but its not a replacement for a () cast. Its probably used by certain people becuase they consider it easier to read, rather than actually need to use it.
James
James Simpson
Web Developer
imebgo@hotmail.com
P S - This is what part of the alphabet would look like if Q and R were eliminated Mitch Hedberg
|
|
|
|
|
James Simpson wrote: The () cast method is more effecient becuase as compiles down to a set of IL instructions that include checking the type before attempting the cast, or returning null, eg:
A cast doesn't perform checks or return null. A cast will throw an InvalidCastException if the types aren't castable.
Simon
|
|
|
|
|
the 'as' cast compiles down to the 'is' check
James Simpson
Web Developer
imebgo@hotmail.com
P S - This is what part of the alphabet would look like if Q and R were eliminated Mitch Hedberg
|
|
|
|
|
Ahh...It's confusing talking about keywords like 'as' that appear naturally in sentences.
Simon
|
|
|
|
|
Sorry if I read it wrong, it just seemed like a I can't be bothered to figure it out myself kind of question. (We get a lot of them around here)
No offence intended.
I cast if I know that the type is right, but us 'as' if I need to check. At a guess, I would say the casting is faster if it works, but slower if it throws an exception because it fails. But like I said before, with managed languages all guesses are off really. The only way that you can be certain about it is to test it.
Simon
|
|
|
|
|
Simon:
Certainly, no offence taken
What's more I liked the answer you gave, very informative: Thanks.
---
Regards,
Martin.
|
|
|
|
|
I'm going to move this to the C# forum, but first an answer:
Do
MyObject thing = new MyObject();
MyOtherObject otherThing = thing as MyOtherObject;
MyOtherObject otherThing = (MyOtherObject) thing;
1 will have otherThing == null. 2 will throw an exception. 1 allows you (in conjunction with is to write safe, polite code that handles bad casts intelligently. 2 just barfs on the carpet like a mangy cat.
cheers,
Chris Maunder
CodeProject.com : C++ MVP
|
|
|
|
|
Help meeeeeeeeeeee
|
|
|
|
|
No! Never.
What is so hard to understand with No?
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Cross posted homework question with crap subject. Why should anyone help you. Do your own work.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Keep your responses in the thread that you already have created, instead of creating a new thread.
People have raised questions about how you expect someone to answer to your question. Your only chance of getting some answers at all is to respond to these questions and explain what your question really is. Ignoring those replies and just asking louder will not get you any answers.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi.
I'd like to write a program, which would delete first 55 bytes from a file. Files size varies from 1MB to 250MB, that means, the files are big...
I started by opening a file as a text file... but that doesnt work for big files.
Could you please help me with some sample code? Or somethin similar?
Thanks in advance!
Regards,
Matjaž
|
|
|
|
|
You can't delete the first n bytes. You need to read the whole thing, then write from n to the end, in a new file ( use the same file path to overwrite ). File.ReadAllBytes and File.WriteAllBytes are probably what you need.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Ok... thanks And where can i edit those readed bytes?
byte[] x= File.ReadAllBytes("C:\\test.pdf");
I didnt find how to remove or edit the array of bytes... or i just forgot how to
File.WriteAllBytes("C:\\test.pdf", x);
Thanks for your fast answer!
Regards,
Matjaž
|
|
|
|
|
max00slo wrote: I didnt find how to remove or edit the array of bytes... or i just forgot how to
You can't remove part of an array. You would have to create a new array and copy the data that you want to keep. That would use twice as much memory, so that's not a very good idea.
Use a FileStream to write part of the array, instead of the WriteAllBytes method.
An alternative, if you don't want to keep the entire file in memory, is to read smaller blocks from the file and write to a temporary file.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Ok, lets say we forget about the filesizes...
Shouldnt it work this way?
byte[] x= File.ReadAllBytes("C:\\test.pdf");
byte[] temp = new byte[x.Length-55];
long tempx=1;
for (long i = 56; i <= x.LongLength; i++)
{
temp[tempx] = x[i];
tempx++;
}
File.WriteAllBytes("C:\\test.pdf", temp);
by my logic and the newbie knowing of c#, this should work? but it doesnt. it goes outside the new array bounds. tried creating the new array without predefined size but its a no-go.
any ideas?
Regards,
Matjaž
|
|
|
|
|
1. Indexes in c# start from 0, not 1.
2. If you don't need first 55 bytes use BinaryReader class. Read first 55 bytes and discard them. Then read next portion of bytes and write them to new file until you get to the end of the file.
|
|
|
|
|
i know that just tried with the number 1 if it makes any difference.
still... why is it out of bounds?
Regards,
Matjaž
|
|
|
|
|
If you knew it, you would know that x[56] refers to 57th element and x[x.LongLength] will be out of bound. Why are you ignoring my suggestion to use BinaryReader/BinaryWriter ?
|
|
|
|
|
Ignore this post... as now i get it.. i trully was ignoring your post went to fast through the text u wrote
Thanks for the answers all of you.
And thank you Giorgi, as you have, once again, solved my problem.
Have a nice day
Regards,
Matjaž
modified on Monday, November 24, 2008 5:49 AM
|
|
|
|
|
I'm not insisting but if I were you I would use BinaryReader/BinaryWriter. If you need to replace first 55 bytes do something like this:
1. Write 55 bytes with BinaryWriter
2. Read 55 bytes by BinaryReader. Don't do anything with them, just read.
3. Read other bytes from file by BinaryReader and write them with BinaryWriter.
|
|
|
|
|
Thats it... i'm almost on giving up on programming, as im going nowhere.
Could you, please, for my better understaning, write me this piece of code for this problem... i just dont get it.
You also said that write first 55 bytes. Ok, but there shouldnt be those first 55 bytes.
This program is ment for this: i got back around 20 PDF files and all of them have 55 bytes at the beggining which makes them fail on load in a viewer. Yes, i would already fix them by hand, but if the problem appears again it would be better to have an app. to do that for me.
Once again... sample would be the best for me. A working one and tested ... which would delete first 55 bytes or rewrite the file without those 55 bytes...
Bah... im desperate :/
Regards,
Matjaž
|
|
|
|