|
This is by design, as shown by this quote from the C# spec: "User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type". (source[^]). I don't really understand why you can't cast from an interface, but there you have it.
Having something that looks like a cast that actually switches to a completely different COM service is probably a bad thing anyway, though. When the conversion is so simple I recommend you just use 'new ExcelWorksheet(...)' and 'worksheet._worksheet' inline (well okay that last one should probably use a read property not the field directly).
|
|
|
|
|
Hello,
I am writing a project which sends SQL query to server, it runs it and returns back to client the results using streaming.
The flow is sending query,clientIP and port the client is going to listen to using WCF. before sending the WCF, the client opens a listener on the chosen port, and waits for a networkStream from the server. this done in a seperate thread. meanwhile the client sends the WCF request (with query,client ip, client port) to server and it returns the results to the waiting client on the right port.
All this done in seperate thread so i can run several queries simultanesly from the same client. After i received the results i am trying to close the TcpListener and TcpClient opened in the client side (in the opened thread). But when the other thread running it throws exception: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCal.
If i don't close the listener and client it works fine.
So how can i close the TcpListener and TcpClient in the thread without causing problem to the second thread .
TcpListener and TcpClient are not static.
Code:
private void OpenListenerTask(int port,AutoResetEvent resetEvent)
{
try
{
NetworkStream getStream = StartListenerGetStream(port, resetEvent);
string localIp = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
var networkReader = new Reader(getStream, localIp, _sendPort.Value, _query);
networkReader.Start();
}
catch (Exception e)
{
Console.WriteLine(e.GetBaseException().Message);
}
//close TcpClient & TcpListener after data/error received
_client.Close();
_listener.Stop();
}
private TcpClient _client;
private TcpListener _listener;
private NetworkStream StartListenerGetStream(int port,EventWaitHandle resetEvent)
{
var tcpListener = new TcpListener(port);
tcpListener.Start();
_listener = tcpListener;
//tcpListener.Server.Blocking = false;
resetEvent.Set();
var tcpClient = tcpListener.AcceptTcpClient();
_client = tcpClient;
return tcpClient.GetStream();
}
|
|
|
|
|
michaelgr1 wrote: i am trying to close the TcpListener and TcpClient opened in the client side
You can't close a TcpListener in the client. The client doesn't own nor have access to that.
|
|
|
|
|
yes,
but in my solution, probably i didn't explain it. The server sending the data and the client receives it. So the listener is opened in the client side
|
|
|
|
|
I take it that your listener has nothing to do with the terminology in socket communication?
Why not use sender and receiver? That takes away the uncertainty of what issue you are really facing.
Cheers, AT
Cogito ergo sum
|
|
|
|
|
The "server" is the one that opens the listener.
The "client" is the one that connects with a client socket to the server (where the listener is.)
I don't care how you label your app(s).
And what I said holds. The client can't close the listener.
The client can close the client socket.
The server can close the client socket (the one it created based on a listener request.)
|
|
|
|
|
This is my first post to discussion so please forgive any mistakes of protocol etc
I am looking to write a 6502 (and possibly later z80) cross assembler to run on the pc using c#. My question is not for code but rather for technique and approach. As I have not done this sort of project before I was wandering what techniques I should use to interpret a line of code. Bearing in mind I need to identify both the instruction and addressing mode.
I can of course actually do this already by either using regular expressions or simply string manipulation but I'd like to know how it's should be done. Other people must have spent years writing assemblers and languages and must know the best approach.
Hope that all makes sense. Discuss .............
Derek
|
|
|
|
|
Let me first admit I'm not a big fan of Regex as they can be powerful, but also cryptic and slow. I do use them in some cases, e.g. I used a few for some of the web page parsing inside CP Vanity (see one of my articles).
I would not even consider using them while writing a compiler or cross-assembler (which I have done dozens of time, mostly in Fortran and later in C). BTW: the really fun part is writing a simulator to go with it.
I'd suggest you write methods (based on string manipulations) that check for and extract a label, a directive or instruction, a first operand, a second operand, etc.
FWIW: everything that can be done with Regex, can also be done without them. I see them as a language add-on, just like LINQ. You can use it if you want, you don't have to.
|
|
|
|
|
Thanks Luc, nice to have the view of someone who has actually done it.
Thanks
|
|
|
|
|
I know one z80 assembler that is written using only string manipulation (not even regex).. it's a nice assembler to use, but the source is a bit of a mess.
Anyway, as usual when parsing some non-trivial, I would suggest using an actual parser. Depending on whether/which parser generator you use, things like "include" may be a little hacky.
But of course string manipulation works fine too.
|
|
|
|
|
I have used Antlr[^] for a not dissimilar task in the past.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thats not something i'd thought about, thats for this.
Derek
|
|
|
|
|
Derek Henderson wrote: how it's should be done
Write a parser.
Don't use a tool since it appears you haven't done this before. The project is simple enough that it can be used as a learning experience for the basics.
Derek Henderson wrote: Other people must have spent years writing assemblers and languages and must
know the best approach.
Buy the Dragon book. That isn't the name but you can find it by looking for that (add 'compiler')
|
|
|
|
|
Im problem is like this.
Im getting these exceptions when running my application.
"A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)"
after this exception
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
In mssql server machine SQl server browser is also enabled and firewall is also disabled.
Please help.
|
|
|
|
|
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;
}
}
|
|
|
|
|