Browsing 7239 questions and answers with Jon Skeet

Attempting to acquire a lock on an object when the calling method already has a lock on the same object

I have some code that I have been going over to learn the system and I ran across some code that to me is a code smell and I wouldn't think it would work at all, but it does. We...
Jon Skeet
people
quotationmark

You seem to be under the impression that a class owns a lock (aka monitor). That's not the case - a thread owns a monitor. Monitors in .NET are re-entrant - if a thread already has the monitor, it can acquire it again. That will increase... more 6/18/2014 12:40:10 PM

people

Union of two linq statement

Hi i have two linq statements: from d in dtDummy.AsEnumerable() where d["Role_id"].ToString() == "0" select d;` from d in dtDummy.AsEnumerable() join t in...
Jon Skeet
people
quotationmark

Well you can use Union: var union = (from d in dtDummy...).Union (from d in dtDummy...); Personally I'd separate the two out for clarity though: var query1 = ...; var query2 = ...; var union = query1.Union(query2); I'd... more 6/18/2014 12:04:27 PM

people

Flags attribute seems to have no effect

Please see the code below: Public Class Form1 <Flags> _ Public Enum Days Monday = 0 Tuesday = 1 Wednesday = 2 End Enum Private Sub...
Jon Skeet
people
quotationmark

What am I doing wrong? You're giving Monday a value of 0, which means it's irrelevant when you perform a bitwise-OR. The only sensible semantic value of 0 in a Flags-based enum is None. You should have: <Flags> _ Public Enum... more 6/18/2014 10:26:06 AM

people

LINQ to Xml Select Element

I'm having some real issue's getting an element value from my XML. I've always seemed to have problems with XML. Code that works perfectly for others wont for mine, I must be...
Jon Skeet
people
quotationmark

Both of your sample code snippets will fail because they're looking for elements in no namespace. Your second sample will also fail because you're looking for a license element under the root element, when in fact it is the root... more 6/18/2014 10:18:40 AM

people

Creating lists from XML with optional elements

I am working on some XML documents and trying to parse them into Lists. I have read the following answers and articles but, I couldn't find a way to parse optional "Values" tag...
Jon Skeet
people
quotationmark

Instead of using .Value, use the explicit conversions in XElement. So for example: FieldName = (string) _field.Element("FieldName"), StartIndex = (short?) _field.Element("StartIndex") You'll end up with null values for absent elements,... more 6/18/2014 8:55:20 AM

people

c# Add some values at List

I write some code which do: List<DataRow> rows=new <DataRow>(); foreach (DataRow dtRow in sqlRows) { foreach (DataRow dtRowId in dttRows1) { if...
Jon Skeet
people
quotationmark

You could just use LINQ with a join, which would be more efficient than the nested approach: var query = from dtRow in sqlRows join dtRowId in dttRows1 on Convert.ToInt32(dtRow[1]) equals... more 6/18/2014 6:17:35 AM

people

How to render a byte array (Blob) as image using TagLib

How to render an Image file (Blob) stored in the database using a custom tag, I used to render a logo but the image is being crashed and the output is the byte array. This is...
Jon Skeet
people
quotationmark

It sounds like you want to provide a data URI, basically. For that you'll want something like: src="data:img/png;base64,xxx" where "xxx" is the data in base64 format. So for example, using this public domain base64 library: out... more 6/18/2014 6:04:32 AM

people

Data structure with O(1) performance for get(int index) and ability to avoid duplication

Set is an obvious choice, if I do not want to have duplication on my list of data. However, Set doesn't have get(int index) method : Why doesn't java.util.Set have get(int...
Jon Skeet
people
quotationmark

The simplest approach is to have a composite collection which contains a HashSet and an ArrayList. Your add operation would try to add it to the set, and only add it to the list if that has actually added a new item. The get operation... more 6/18/2014 5:55:37 AM

people

How to encrypt a string using public key cryptography

I am trying to implement my own RSA encryption engine. Given these RSA algorithm values: p = 61. // A prime number. q = 53. // Also a prime number. n = 3233. // p * q. totient =...
Jon Skeet
people
quotationmark

Your basic code for encrypting and decrypting each byte - the call to ModPow - is working, but you're going about the "splitting the message up and encrypting each piece" inappropriately. To show that the ModPow part - i.e. the maths - is... more 6/17/2014 4:52:27 PM

people

Insertion of BLOB fails with the file being NULL

In Java, with JDBC I am trying to insert a file in a BLOB column in a table of an Oracle database. Here is how I proceed: private Statement getStatement(File f, String fid, Long...
Jon Skeet
people
quotationmark

You're closing the FileInputStream before you execute the statement, so there's no way for the statement to get the data when it actually needs it. It would better to pass an InputStream into your method, so you can close it externally... more 6/17/2014 4:27:59 PM

people