|
Don't post your question on multiple locations. Already I have answered here[^]
Always give us some time for the answers.
|
|
|
|
|
Hi.
There is my problem.
I've created a wpf 4.0 app. When I debug and run it from vs 2010, there is no problem.
At the same pc, when I try to run the .exe, the application crashes.
The error is the following
EventType : clr20r3 P1 : application.exe P2 : 1.0.0.0 P3 : 4ee325f2 P4 : mscorlib P5 : 4.0.0.0 P6 : 4e181ae3 P7 : 41ed P8 : 460
P9 : system.windows.markup.xamlparse
Why is this happening? Any ideas?
|
|
|
|
|
Over 43000 results for the google search phrase "wpf clr20r3" [^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hi,
I am using OpenXML to open an existing Word document and modify it. After modifications I want to save it to a database. When I am using a stream in the WordprocessingDocument.MainDocumentPart.Document.Save method, only the main document part is saved and not the whole docx file.
Is there a method available which saves the whole file to a stream or converts it to a byte array?
Thx,
Danny
|
|
|
|
|
if you want to store an existing file in a database, you don't care about the file content, it is just bytes to you.
This implies you don't want to know what application did create the file, and hence you don't involve Word in doing the job. Use Word to create, alter, and save the document to disk; then use file operations and database operations to store it all in a BLOB, as if it were an image or anything else that fits in a byte array.
|
|
|
|
|
dennieku wrote: When I am using a stream in the WordprocessingDocument.MainDocumentPart.Document.Save method, only the main document part is saved and not the whole docx file.
That's because the <a href="http://msdn.microsoft.com/en-us/library/cc840441.aspx">MainDocumentPart</a>[<a href="http://msdn.microsoft.com/en-us/library/cc840441.aspx" target="_blank" title="New Window">^</a>] is merely a part of the document; a typical Word-file also contains attachments like pictures, and links to those embedded blobs.
dennieku wrote: Is there a method available which saves the whole file to a stream or converts it to a byte array?
Yes, the <a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpackage.close.aspx">Close</a>[<a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpackage.close.aspx" target="_blank" title="New Window">^</a>] -method, according to MSDN;
Saves and closes the OpenXml package plus all underlying part streams.
Once closed, read all the bytes from the file and delete the original.
Bastard Programmer from Hell
|
|
|
|
|
... moving on from my excursion into the mysteries of Tuple, and bearing down on a thorough review of struct ...
... edit #1 ...
The reason I originally titled this post with a qualification stating I thought use of Reference Types in a struct with self-initialization were illegal was because of this statement in the MS doc, "Using Structs: C# Programming Guide:"
"When a struct contains a reference type as a member, the default constructor of the member must be invoked explicitly, otherwise the member remains unassigned and the struct cannot be used. (This results in compiler error CS0171.)"
As mentioned below, this view was modified, as I discovered that internal members of a struct declared 'static can be self-initilizing.
... end edit #1 ...
I am aware that 'struct' in .NET is primarily intended to hold value Types, but I wanted to explore the use of reference Types inside a struct in various ways. I would be very interested in knowing, in terms of allocation, what happens "under the hood" in .NET if a reference Type is included in a struct.
This "quest" led to this particular (final) experiment, where not only did I use a reference Type, but made it static. That a static Type could be self-initializing inside a struct interested me:
public struct TestStruct
{
public static ListInnerList = new List<TestStruct>();
public int testInt;
public string testString;
} And, creating instances updates the static List 'InnerList' as expected:
for (int i = 0; i < 10; i++)
{
TestStruct.InnerList.Add(new TestStruct {testInt = i, testString = "string #" + i.ToString()});
}
Where it becomes more interesting to this well-trodden blade of grass is: if you want a TestStruct public field modified, using the 'InnerList: got to do something like this:
TestStruct selectedStruct = TestStruct.InnerList[5];
selectedStruct.testString += " modified !";
TestStruct.InnerList[5] = selectedStruct; As MS says: "When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy."
I assume there's no work-around possible for this, where you could directly modify an existing instance of a struct inside a List of instances of said struct ... without creating a new variable, and then re-assigning.
The closest idea I could come up with to simplify modification was something like this to be included in the definition of TestStruct:
public static void modifyString(int index, string stringToAdd)
{
InnerList[index] = new TestStruct {testInt = index, testString = InnerList[index].testString + stringToAdd};
} Which would be called like this:
TestStruct.modifyString(5, " ... modified !"); I don't regard that as "pretty" code.
My guess is that if you reach the point where you need the kind of facilities created (awkwardly) in this example, you've reached the point where you should use a Class.
Aside: interesting you cannot use the "==" operator on two instances of a struct, but, after all, why should something built to be "mean and lean," and live on the Heap, implement IEqual??? or other fancy Interfaces ?
Appreciate any comments, and if this code reflects "bad practices," I certainly am eager to know, since I hear LOPEARS, League Of Programmers Extrajudicial Authority Respecting Structs, has been authorized to shoot to kill.
thanks, Bill?
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
modified 11-Dec-11 23:18pm.
|
|
|
|
|
BillWoodruff wrote: "bad practices"
Absolutely.
I see only two situations that really justify using a struct:
1.
a group of data items that need exact relative positioning in memory, e.g. when one needs to map to a native struct (as in P/Invoking user32.dll and kernel.dll ).
2.
a really small amount of data that doesn't justify the overhead of a class instance. The best examples are System.Drawing.Point/Size/Rectangle, and maybe a Complex number, and the like. All of them are below 32 bytes of data (note: 32 bytes is about the cost of an object).
For everything else, I use classes. Which gives better performance, and less hassle: cheaper sorting, no boxing/unboxing, no equality problems, ...
|
|
|
|
|
+5 Thanks, Luc, I asked to "tell it like it is,"[^] and, you did
best, Bill
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
|
|
|
|
|
You're welcome.
|
|
|
|
|
BillWoodruff wrote: interesting you cannot use the "==" operator on two instances of a struct
Yes and no...
A class can be automatically tested for equality by comparing the reference (pointer) to it, obviously a struct has no reference so has no always defined value.
The reference comparison for classes is pretty much useless without some kind of inlining in most cases so if equality needs to be tested then overloading the == and != operators, and bool Equals(object) and int GetHashCode() methods need to be overriden, something like:
public class TestClass
{
private int value;
public TestClass(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
}
public static bool operator ==(TestClass first, TestClass second)
{
bool result = false;
if (object.ReferenceEquals(first, second))
result = true;
else
if (!(object.ReferenceEquals(null, first) || object.ReferenceEquals(null, second)))
result = first.value == second.value;
return result;
}
public static bool operator !=(TestClass first, TestClass second)
{
return !(first == second);
}
public override bool Equals(object obj)
{
bool result = false;
TestClass other = obj as TestClass;
if (!object.ReferenceEquals(null, other))
result = value == other.value;
return result;
}
public override int GetHashCode()
{
return value;
}
}
If you implement the same operators and override the same methods on a struct you can use == etc, in fact the implementation is easier as there is no need for null checking on value types! :
public struct TestStruct
{
private int value;
public TestStruct(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
}
public static bool operator ==(TestStruct first, TestStruct second)
{
return first.value == second.value;
}
public static bool operator !=(TestStruct first, TestStruct second)
{
return !(first == second);
}
public override bool Equals(object obj)
{
bool result = false;
if (obj is TestStruct)
result = value == ((TestStruct)obj).value;
return result;
}
public override int GetHashCode()
{
return value;
}
}
|
|
|
|
|
+5 Thanks, Dave, for this thoughtful answer, and the sample code, which I will study diligently.
best, Bill
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
|
|
|
|
|
Good answer.
A quick question on this: I have been missing out on object.ReferenceEquals, so I have typically written my == operator along the lines of
public static operator==(MyType a, MyType b){
return ((object)a == null) ? (object)b == null : a.Equals(b);
}
public override bool Equals(object o){
MyType b = o as MyType;
if(b == null) return false;
else if((object)b == this) return true;
return (field1 == b.field1) &&
}
Is it better to use ReferenceEquals than the cast to object?
|
|
|
|
|
I don't know that it makes any difference to be honest... Does boxing* MyType to object have any overhead worth considering?
Using object.ReferenceEquals appears cleaner IMO as we are obviously comparing the references of each instance (or null ), rather than a cast to object when, apart from the null equality check, we never actually want it as an object.
*Edit: If MyType is a reference type then it's not boxing, but you get what I mean
Edit 2: See this SO thread[^]
modified 14-Dec-11 8:02am.
|
|
|
|
|
Nice link. And I think 'cast' is the word you want; if it's a value type then you don't need to bother with the null check anyway since a null can never be passed.
So basically it makes no difference as long as they've improved the compiler since .Net 2, which didn't inline ReferenceEquals calls. And yeah, any overhead is trivial and of academic interest only, but that's fun to think about sometimes.
|
|
|
|
|
hello guys... First of all, if this is wrong platform for this question, plz guide me to the right one. Second, I want to play with communication protocols like TCP/IP Model. I know basics for Network programming or Socket Programming but I dont know where to start for programming basic internet protocols. Any pointers to such topics will be appreciated. thnx
|
|
|
|
|
Not sure if this would be the wrong place but you at least kept your question wide open.
I think that you will need to be a bit more specific about what you want to accomplish. Are you just interrested in builing a chat app(tons of sources which i seriously don't need to point out). If you are aiming to controll a device of sorts you'll need to think about your own message protocol (which at least negotiates a version!). Either way let us know your aim and we'll give you a lot of hints, tips and/or links.
Cheers, AT
Cogito ergo sum
|
|
|
|
|
I guess im interested in Internet Security, if that is an appropriate term to use. I want to make sure that when I send some data over internet, its as much secure as possible. I guess I will need to play with .... Network Layer or something, I dont know. Let me know if I was able to help you.
|
|
|
|
|
Protecting data isn't so much a protocol issue as it is a cryptography issue
You should start learning basic cryptography primitives such as what a symmetric cipher vs asymmetric, in addition to key establishment protocols like diffie-hellman.
Keep in mind this is a very large field of study
Thanks,
Matthew.
|
|
|
|
|
There is more to security than just data encryption.
Although researching data encryption, understanding it and implementing it for non-trivial size messages will likely take some time so it is a start.
|
|
|
|
|
mmm, you could make your data secure by just making it un-understandable by having a really idiotic protocol or you can do what everybody else does... encrypt the data. There are lots of good examples which you can follow.
If it is just about file transfer you can take the easy(chicken??) way out and write yourself an app that creates a temp spanned-encrypted zip, transfer that, glue it back to gether and decrypt it.
I've done a lot on communication and protocols but hardly ever bother with security. This ball is for someone else I guess.
Cheers, AT
Cogito ergo sum
|
|
|
|
|
The most secure approach if you can't have offline key transfer is to first share a key using public key cryptography (realistically at the moment that means RSA), and then use that key for the data transfer. You can refresh the key periodically if you think key re-use is compromising your security (using the public key exchange protocol again, of course).
The easiest way to do that is use SSL. My sockets library also offers an RSA protected key exchange and you're welcome to have a look at that code to get some inspiration if you want to do it yourself.
|
|
|
|
|
overloaded Name wrote: I know basics for Network programming or Socket Programming but I dont know where to start for programming basic internet protocols.
What, you wanna learn the Network Time Protocol[^]?
Most protocols are described on the wikipedia. Is there any specific protocol that you are interested in?
Bastard Programmer from Hell
modified 12-Dec-11 12:02pm.
|
|
|
|
|
Eddy Vluggen claims to be: Bastard Programmer from Hell Holland
ftfy [see your wikilink]
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Whehe, aight - just corrected the link
|
|
|
|
|