Wednesday, March 16, 2011

What is the deal with the unicode character 首(U+9996) and how java/mysql deal with it and its friends?

Man, this character encoding hole just keeps on getting deeper. Sigh. Ok. Check this out: I have a java String that contains the unicode character U+9996 (that's what I get if I do codePointAt()). If I look at it in the debugger expressions panel (in eclipse) then all is well and it looks like "首". However if I print it out to the console I get simply "?". It doesn't seem to be the font that's the problem as I've tried setting that differently.

My real problem is that I'm trying to put the string into a MySQL database (with utf8 encoding). Lots of other wide characters show up fine in the db but, again, this one and some others like it show up as "?". All of which leads me to believe that the problem is on the java side.

In chasing down this bug I've learnt a little about Unicode Normalization and java.text.Normalizer which looks like it might be relevant in this case. I've learnt that U+9996 is the canonical version of U+2FB8. U+2FB8 has exactly the same problems above though as regards display and anyway why would I want to transform to a non-canonical representation (even if I could, which I don't think I can)?

Anyway, there's one potential clue I've found which I've been unable to comprehend. This page contains the words "U+9996 is not a valid unicode character" with no further explanation. It then proceeds to show how to encode this supposedly non-valid unicode character in various unicode encodings. So my question is this basically: WTF?


UPDATES

  • I'm on a Mac.
  • I'm talking about the Eclipse console.
    • I set the console encoding to UTF-8 under Run > Common
    • I added -Dfile.encoding=UTF-8 to the JVM arguments (the default was MacRoman)
    • The console (Eclipse and Terminal.app) now show the right chars. Hooray!
  • I'm mostly interested in the data getting into the database correctly though of course I'd like to get a total understanding of what's going on here.
  • I think I've fixed the database problem. I forgot to set the encoding on the connection. Now I don't understand why some asian characters were getting through and not others.
  • Phew, stackoverflow moves fast. It's hard to keep up. Thanks people.
From stackoverflow
  • I don't know about the problems, but it's definitely a valid Unicode character (and has been since Unicode 1.1).

    1. What O/S is this running on?
    2. What console application is ie (xterm, cmd.exe, etc?)
    3. Is the console application set for UTF-8 output?

    Regarding 3 above, which is probably the important one, I've seen similar issues using e.g. PuTTY to talk to a Linux box, where the Linux box thought I was on UTF-8, but the PuTTY session itself was set to ISO-Latin-1 (8859-1)

    Yoni : In Eclipse you can set the enciding for the console, check out the preferences.
  • Have you verified that the value that gets stored in the database is actually U+003f (question mark)? There are all sorts of conventions for how to display characters that don't exist in the chosen font, and displaying them as ?' is fairly common.

    So most likely, the character gets stored correctly, and for whatever reasons, simply gets displayed as '?'. Basically, ignore how it gets rendered, and look at what codepoint gets stored in the database. Is it U+9996 or U+003f (or something else entirely)? Don't blindly assume that just because it gets rendered as a question mark, it is actually a question mark that is stored in the database.

    Rowan : How do I verify the value in the database is correct? I don't see a SQL function to show codepoints.
    Darryl Braaten : Read it back out with a java function and verify it at that point.

.net 2.0 security configuration

Are there some help resources, or can anyone give me a brief Idea how I would configure the .net 2 runtime security policies for the following scenario:

I have a windows forms control hosted in IE. The control tries to read from a serial port and write to the event log. Both of these operations fail due to security restrictions in the browser:

Request for the permission of type 'System.Security.Permissions.SecurityPermission,mscorlib, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089 failed.

Request for the permission of type 'System.Diagnostics.EventLogPermission,System, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089 failed.

I've set my site to be fully trusted by adding it to the list of fully trusted sites in IE, but I still have the problem. I am pretty sure the answer isin the runtime security policies in the .net 2.0 configuration but I just don't know what to change.

From stackoverflow
  • Since (I assume) you're running under IIS, you need to make sure that your I_USRmachinename account is in a security group that has the permissions you need, or that it is not in a group like "Guests," which would have restricted access to things like the system Event Log. Look at the permissions/groups for your I_USR, I_WAM, and ASP.NET accounts on the system you're trying to run on. I think that's where your problem lies.

    AJ : I appreciate the down-vote. Any feedback as to why I got it?
    Jeremy : PITADeveloper, Not sure abut the down vote, I didn't do it, but these permissions we're talking about would be ones on the client machine, what you're talking about are permissions on the server, which I don't think would solve my problem becase the code being restricted is running on the client.
    AJ : Ah, didn't realize that. Thank you for your insight. I just like to learn, that's all, and a downvote without comment doesn't teach me anything.
  • Try the following:

    Control Panel -> Administrative Tools -> Microsoft .NET Framework 2.0 Configuration

    Expand out and select:

    .Net Framework 2.0 Configuration -> MyComputer -> Runtime Security Policy -> Machine -> Code Groups -> All_Code

    Under that there are the different zones. I'm not sure which one would apply to IE and your scenario, but try the following on each of them (remember to set them back after testing)

    Right click on the "??????_Zone" Select the "Permission Set" Tab Change the "Permission Set" combo to "Full Trust"

    The "Membership Permission" tab may also lend some clues to which one is correct.

    Info on Code Access Security

    badbadboy : Wanted to write the same as you, but didn't find Microsoft .NET Framework 2.0 Configuration on my machine... :) weird..
    GSerg : Yeah, there's only Microsoft .NET Framework 1.1 Configuration despite I've got all of them installed and I'm an administrator. Would like to know the answer as well!
    Robert Wagner : Looks like you have a common problem. See http://home.hot.rr.com/graye/Articles/CodeAccessSecurity.htm#Install
    GSerg : >> Looks like you have a common problem Too common then. I've got VS2008 on my PC and there's no 2.0 Configuration applet, I've got VS2008 on work PC and it's no applet as well, and I've got VS2008 on yet another PC which didn't previously had VS2005 and still no applet. I'm confused.

Any downside to "WITH RECOMPILE" for monthly SQL Server stored proc processes?

I think the question says it all. I have several monthly processes in stored procedures which take anywhere from a minute to an hour. If I declare them WITH RECOMPILE, an execution plan will be generated each time.

If the underlying indexes or statistics or views are changed by the DBA, I don't want anyone to have to go in and force a recompile the SPs with an ALTER or whatever.

Is there any downside to this?

From stackoverflow
  • Under the circumstances, it would be completely harmless, and probably a good idea.

  • As I understand it, an SP should be re-compiled if needed automatically. So your concern about underlying changes doesn't really matter.

    However, the server tries to cache compiled SP plans. Using WITH RECOMPILE will free the memory that would have been used to cache the compiled procedures (at least until the next time the cache is cleared). Since they're only run monthly this seems like a good idea.

    Also, you might want to look at this article for other reason to use that directive:
    http://articles.techrepublic.com.com/5100-10878_11-5662581.html

    Cade Roux : What if there are newer, better indexes it could use, or if there were new statistics - will the old plans be automatically discarded?
    Jim McLeod : No, the only time a plan with be invalidated is when indexes or statistics used by the plan are modified. See the topic "Execution Plan Caching and Reuse" (look up "procedure cache" in the index), section "Recompiling Execution Plans" in Books Online for more details about what invalidates plans.
  • If each stored procedure is only run once per month, it is highly unlikely that the compiled procedure will still be in the procedure cache. Effectively it will be recompiling anyway.

    Even if you run the same stored procedure 100 times on your reporting day, it will only take 0-2 seconds to compile each time (depending on the complexity of the stored procedure), so it's not a massive overhead. I'd feel comfortable setting WITH RECOMPILE on those stored procedures.

    Cade Roux : Right - I'm thinking the recompile time is always neglible compared to the run time - and the run time limits me from running these processes so frequently that it could even be a factor.

Event handlers inside a Javascript loop - need a closure?

I'm working with a bit of html and Javascript code that I've taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds, and then re-builds the table using some DOM code. The code in question looks something like this:

var blah = xmlres.getElementsByTagName('blah');
for(var i = 0; i < blah.length; i++) {
    var td = document.createElement('td');
    var select = document.createElement('select');
    select.setAttribute("...", "...");
    select.onchange = function() {
        onStatusChanged(select, callid, anotherid);
    };
    td.appendChild(select);
}

When the onchange event is fired for a <select> element however, it seems like the same values are being passed to the onStatusChanged() method for every <select> in the table (I've verified that in each iteration of the loop, callid and anotherid are being given new, distinct values).

I suspect this is occuring because of the nature of how I am setting the event handler, with the select.onchange = function() syntax. If I understand how this is working correctly, this syntax sets a closure for the onchange event to be a function which refers to these two references, which end up having a final value of whatever they are set to on the last iteration of the loop. When the event fires, the value referenced by callid and anotherid is the value set in the last iteration, not the value set at the individual iteration.

Is there a way that I can copy the value of the parameters I am passing to onStatusChanged()?

I've changed the title to better reflect the question and the accepted answer.

From stackoverflow
  • You do, indeed, need to implement a closure here. This should work (let me know - I didn't test it)

    var blah = xmlres.getElementsByTagName('blah');
    for(var i = 0; i < blah.length; i++) {
        var td = document.createElement('td');
        var select = document.createElement('select');
        select.setAttribute("...", "...");
        select.onchange = function(s,c,a)
        {
            return function()
            {
                onStatusChanged(s,c,a);
            }
        }(select, callid, anotherid);
        td.appendChild(select);
    }
    
    matt b : this does indeed work - thanks!
    matt b : Also another solution I discovered - a workaround, really - would be to store the callid and anotherid as attributes in the DOM element, and inside the eventhandler just reference those via this.getAttribute()
    le dorfier : I wouldn't call it a workaround. What I would expect is to have the handler set once (not every time through the loop) and have it discover the variables itself.
  • This is the right answer. Had a similar issue and the closure solution was the ticket.

Class usage in ORM based projects

This question is about "best use" scenarios in projects with ORMs like NHibernate, Subsonic, Linq2SQL, etc...

All of these tools generate basic entity classes, some with attributes, others without. Do people use these classes as their business classes? Or is there wholesale copying of data from ORM generated classes to manually created business classes?

Thanks.

From stackoverflow
  • I tend to work the opposite way. I create the business objects the way I need them, and create NHibernate mappings from my objects to data. You can have NHibernate generate a schema for you based on your mappings, or you can create your own schema, and create the mappings to go between the two. Linq2Sql and Entity Framework do not support this. I can't speak to Subsonic on this point.

    I usually create my business classes, and get the application at least partially running without any database at all. This way I can develop a better understanding of what it is the application is supposed to do, and how it should behave before making the decision of how to persist the objects.

    Justice : This is the way to go. In fact, the whole point of OOP and ORM is so that you are free to work with business objects (the domain model)! You can use tools like NHibernate to persist your business objects transparently, but your application code does not need to deal at all with persistence.
  • There are a couple of solutions for all of the tools mentioned, the answer is it depends on the scope of your project.

    Here's a similar question about LINQ to SQL that I answered a little while ago.

    Hope that helps!

  • I normally use the entities directly in the business and presentation layers. The data layer defines the entity, the business layer manipulates the entity or queries a list of entities, and the presentation layer displays the entities.

    I think that creating separate business objects and copying data between the two would be a lot of unneeded overhead. But if you find that you have to do this, I'd recommend just wrapping the entities instead of copying data back and forth. You can hide the entity and use Properties to expose members and alter behavior.

  • SubSonic and Linq2Sql are one-to-one orm mapper. Consider the situation where the datbase is normalized. For example an employee info is broken down in 3 different tables but in your domain model you'd want only one object Employee represent the info. This is where SubSonic and Linq2Sql fail. NHibernate allows you to map your domain object to multiple tables. Also you'd want to stay away from auto generated code. NHibernate allows you to define your own POCO (Plain old C# object) domain and has different ways that allows us to map that to table(s) in database

    Ahmad : Correct Shiraz. To isolate yourself from one-to-one mapping, youl would have to use SQL and abstract yourself at that level.

Programmaticly use Gmail to receive e-mail?

I'd like to use a C# program to poll a gmail account and automatically download new messages. I know you can use gmail as an outbound SMTP server, but is there any way to access new messages sent to the account?

EDIT: Thanks for the rapid feedback....so I have two options, POP or IMAP.

Which one should I use? And why?

EDIT #2: Looks like IMAP allows me to not have to poll. Looks like the way to go.

From stackoverflow
  • You can configure GMail to let you get at your mail with a POP3 client.

  • Use Gmail with IMAP.

    Matt Cruikshank : Sure - but with what libraries does Jonathan use IMAP?
    FlySwat : I think I'll use this one: http://www.codeplex.com/InterIMAP
    Martin Vobr : What about this one? Shows how to get list of unread messages (IMAP, VB.NET) http://blog.rebex.net/news/archive/2007/05/28/howto-get-list-of-unread-messages-from-an-imap-server-in-vb-net.aspx Shows how to get message list from POP3 server (C#) http://blog.rebex.net/news/archive/2007/05/14/howto-download-emails-from-gmail-account-in-csharp.aspx
  • By the way, there is an instruction how to use SMTP with SSL with GMail: http://www.mono-project.com/FAQ:_Security#Does_SSL_works_for_SMTP.2C_like_GMail_.3F

  • You can get an Atom Feed of your GMAIL, which can be fetched with a regular web request, and parse as a regular XML document. I made a PHP page I could access from my phone (which doesn't support Atom Feeds, or pages as advanced as Google Mobile), to show me a list of new emails.

    FlySwat : Link is broken, but cool idea.
  • There is a C# component on SourceForge that lets you do exactly that. At some point I wrote an NT Service that runs in the background and downloads files from a gmail account.

How to access the Index Of A Generic.List By Reflection??

ok, ive a class and i pass an object as property.

the object that i pass is a List<X>

in my class im trying to access the Object index by reflection BUT I CAN'T!!!

Example:

this class works i just wrote down the part i want to show you and i need help.

class MyClass
{
    private object _recordSet;
    public object RecordSet
    {
        get { return _recordSet; }
        set { _recordSet = value; }
    }

    public string Draw()
    {
        system.reflection.Assembly asem = system.reflection.Assembly.getAssembly(_dataSource.GetType());

        object instance;

        instance = asem.CreateInstance(_dataSource.GetType().UnderlyingSystemType.FullName);

        //to access de Count of my List
        int recordcount = int.Parse(_dataSource.GetType().GetProperty("Count").GetValue(_dataSource,null));

        //i need to do a 
        for(int cont = 0; cont < recordCount; cont++)
        {
            _dataSource[cont].Name; // <-- THIS PART IS NOT WORKING!!! because i cant access the Index Directly.... WHAT TO DO!! ???
        }
    }
}
From stackoverflow
  • If you are using reflection (and hence lots of object), why not just cast as an IList (non-generic) instead?

    i.e.

    IList list = (IList)actualList;
    object foo = list[17];
    

    Also - for your original code with Count, you don't mean int.Parse - you should just cast (since we expect Count to be an int).

    Jeff B : Add: if (actualList is IList)
    Marc Gravell : @Jeff B - I disagree; the scenario suggests that we expect it to be a list, hence if the data *isn't* an IList, I'm happy for it to raise an exception. It depends on the scenario, of course; if it was ad-hoc data-binding to either an object, an IList or an IListSource, then "as"/"is" is necessary.
  • Just cast your object to a list first, you don't need reflection here.

  • when i try to create a ILIST object i get a compilation exception:: using generic type 'system.collections.generic.ilist' requires 1 type argument.

    @Marc Gravell it can be a ILIST because i just want to move inside the records

    PD

    Sorry about my english...

    Marc Gravell : As I tried to stress, you want the *non-generic* IList, not IList
    Marc Gravell : i.e. `System.Collections.IList`, not `System.Collections.Generic.IList`
    Marc Gravell : add "using System.Collections;" to the top of the file
  • IList newlist = (IList)_dataSource;

    object x = newlist[1];

    it give to me a compilation exception...

  • Hey hey It works i was missing this part :P System.Collections.IList thanks dude!!

CheckBoxField in GridView won't bind to a string field in the database

How do I bind a CheckBoxField in my GridView to an underlying db field that is a string. The string is either "1" or "0" but all the same the GridView won't willingly bind to it. What do I do. What is the best way to have a checkbox in the GridView and have it get and set the string in the database (or the underlying datasource).

From stackoverflow
  • The CheckBoxField binds to a boolean. You can either convert the string to a boolean in the binding expression, or cast it in the db return.

    It would make more sense for the database to store the checkbox state as a bit rather than a string. Then this problem would go away completely.

    Of course, if you need to store the third 'grayed' state, that complicates matters slightly, but you could still store the state as an int.

  • This should work:

    Checked='<%# DataBinder.Eval(Container.DataItem, "MyStringField") = "1" %>'

    Normally a checkbox value would be mapped to a bit value in your database so you wouldn't get this issue.

    minty : Almost; You got me on the right track:
    Andrew Rollings : You shouldn't need the inner Convert, should you? Also you could do: Checked = '"1" == DataBinder.Eval(Container.DataItem, "MyStringField")' for the same effect :)
    Andrew Rollings : (adding the correct <%# %> that I missed out, of course :)
    BenAlabaster : Well, theoretically, Boolean.Parse shouldn't be parsing string values, but the DataBinder.Eval should (again) theoretically convert it to a type that Boolean.Parse should be able to read that. Checked='<%# DataBinder.Eval(Container.DataItem, "MyStringField") = "1" %>' should work too.
    minty : the compiler was happiest with: Checked='<%# (string)DataBinder.Eval(Container.DataItem, "NETWORK_READ_FLAG") == "1" %>' I needed the == for c# and the cast to string so I did a value comparison instead of a reference comparison.
    BenAlabaster : Isn't it amazing the hoops that .NET makes you jump through... :)

Javascript onchange functioning in a textarea (Symbian browser)

This may apply to other mobile operating environments than just Symbian... However, the issue is that when changing a text area field I'd like to update a count as I type. This is easy to accomplish in most browsers with something like this on the textarea tag:

onkeyup="document.getElementById('size').innerHTML=this.value.length;

However, in Symbian, the onkeyup event does not appear to fire normally (it seems like all input is buffered or something). Now I can replace this with:

onblur="document.getElementById('size').innerHTML=this.value.length;

...This works fine but of course only updates the DIV when focus is lost (which is better than nothing but not as nice as having it update as you type).

Has anyone encountered an issues like this specifically on Symbian? On any other mobile browser? Any ideas/solutions?

I've been hating Symbian since I've started using it -- there are so many weirdnesses like this in their APIs. I'll vote up anyone who gripes about Symbian. ;-)

From stackoverflow
  • In Symbian's version of WebKit, the keyboard events are royally messed up. The only reliable way to handle them is to set the handler on the document [!]. Now, I have NOT tried that in the situation where textarea has focus but it definitely works with simpler controls. Basically, you use the onfocus and onblur to associate/disassociate the control-specific event handlers with the document. Of course if you want to create a reusable widget that way, you're in for a sea of hurt :(

    OTOH, if you gripe about Symbian's little keyboard quirk, take a gander at mouse events in Android's mini-Chrome... those go under the FUBAR heading!

    ...Max...

    PS. If you need more help: max-at-motovilov-dot-com. You should be able to google for this issue on forum.nokia.com but the info there ain't plentiful... it's all trial and error.

    Andrew Flanagan : I've been using forum.nokia.com for a bit. I suppose there's some useful info there but it's usually not that great. I'm hoping Stack Overflow can become a better Symbian source. Thanks for the info!
    Pat : I can't get this to work even for the simplest textareas! can you update the answer with a working sample for a textarea?
    Andrew Flanagan : I actually never did as described. Differences between the emulator and the phone and curiously non-predictable results discouraged me from continuing. So I don't really have anything -- if you'd like I'd be happy to take a look at any code you've tried andrew dot flanagan at gmail _dot_ com.

HTML Sanitizer for .NET

I'm starting a project that will be public facing using asp.net mvc. I know there are about a billion php, python, and ruby html sanitizers out there, but does anyone have some pointers to anything good in .net? What are your experiences with what is out there? I know stackoverflow is a site done in asp.net that allows freeform HTML, what does it use?

From stackoverflow

How do I enforce using a factory on a struct in C#

I have a c# struct where I need to forbid calling the no args constructor on it.

MyStruct a;
/// init a by members   // OK
MyStruct b = MyStruct.Fact(args); // OK, inits by memebers

MyStruct s = new MyStruct(); // can't have that

I'm doing this mostly to force explicet values for all members as there are no valid default values and all members must have valid values.

In C++ this would be easy, add a private constructor but c# doesn't allow that.

Is there a way to prevent the above?

I really need to enforce using a factory so preventing all public constructor calls would work just as well.


Full discloser: to avoid a mono dependency, the c# app is being automatically translated to D where new Struct() results in a pointer and this is mucking things up for me. However this question is relevant despite that so just ignore it.

From stackoverflow
  • Put it in its own assembly and have the MyStruct() without the args as a Internal (Friend in VB). Have the Factory in the same assembly as MyStruct() but with a Public accessor.

    Now the factory can access the no args MyStruct, but anything calling from outside the assembly must use the Factory.

    Edit: My bad, I failed to take into account that this is a struct. You can't do this with a struct, only with a class - in which case, my previous statement stands.

    Jon Skeet : You can't write your own paramaterless constructors for value types, so you can't define one to be internal.
    Matt Cruikshank : ...or Reflection, but yeah, you've got the best answer I can think of.
    BenAlabaster : Jon: Yeah, you're right - I instantly read this as Class - my mistake. You would have to make the struct a class.
  • You can't. All structs have a public parameterless constructor by definition in C#. (In the CLR almost none of them do, but they can always act as if they have.) See this question for why you can't define your own parameterless constructors on structs (in C#, anyway).

    In fact, you can prevent this statement if you're happy to write your value type in IL. I've just checked, and if you make sure that your value type only has a parameterless constructor, and make it internal, you won't be able to write MyStruct ms = new MyStruct(); But this doesn't prevent:

    MyStruct[] array = new MyStruct[1];
    MyStruct ms = array[0];
    

    which works around the new restriction - so it doesn't really buy you anything. That's quite good really, as messing around in IL would be messy.

    Are you sure you really want to be writing a struct in the first place? That's almost never a good idea.

    BCS : Crud.... & Yes, I need value semantics and the fact I'm throwing these things around like popcorn plus some "extra" constraints result in a major perf hit if I switch to classes
    Jon Skeet : Have you *measured* the performance to know for sure that it would actually be a perf hit? Try it with a class, making it immutable to keep the value type semantics.
    plinth : Hee - messing around in il is messy. You are my tautological hero, Jon.
    BCS : Re: perf the hit is in the constructors (See the new disclosure) when memory is allocated.
    Jon Skeet : I would fix it to be less tautological, but then it would mess up your comment ;)
    Jon Skeet : @BCS: Is D really sluggish at memory allocation then? In .NET I'd have no worries about creating millions of objects if necessary. Of course, one of the nice things about using a class is that often you can get away *without* creating a new object - just keep the reference unless you need to mutate.
    BCS : I need to create 10Ks (1+ per token in kloc files) or so distinct objects per run and the difference between stack based struct and heap based objects is not trivial. Some of this is because D, with it's system level stuff, can't use some of the GC optimizations c# can.
    BCS : For context, I expect about a 25-50% of the data structures created to be of this type.
    Jon Skeet : Are you actually gaining much by doing code conversion here? I've never been much of a fan of writing in one language and then converting to another. I'd usually rather just write in the target language to start with. It may be a bit more painful in general, but it avoids this sort of issue.
    BCS : The major down side of D is that the tools suck, IMHO the only up side to C# it that the tools are world class.
  • You can't.

    All values in a struct must be initialized at construction time, and there's no way to do that outside the constructor.

    What exactly are you trying to accomplish by doing that? Structs are value types, so you'll get a "new" struct for most operations. It will be very difficult to enforce the sorts of constraints you'd use a factory for on a struct.

    BCS : you can in a static method in the struct, as my factory method does.
    BCS : The primary constraint is to disallow default initializations. I want to force the code to explicitly provide all the values.
  • Anyone can create a struct at any time without calling a constructor, as long as they have access to the struct. Think about it this way:

    If you create an array of objects with 1000 elements, they all get initialized to null, so no constructors are called.

    With structs, there is no such thing as null. If you create an array with 1000 DateTime objects, they are all initialized to zero, which equals DateTime.Min. The designers of the runtime chose to make it so you could create an array of structs without calling the constructor N times--a performance hit many people wouldn't realize was there.

    Your factory idea is a good one, though. Would it meet your needs to create an interface and expose that, but make the struct private or internal? That's about as close as you'll get.

Decimal rounding problems

Given that Decimal.MaxValue = 79228162514264337593543950335m

Why does the next line give me 7922816251426433759354395034M in the Local window instead of 7922816251426433759354395033.5m as expected?

Decimal target = Decimal.MaxValue / 10m;

TIA

From stackoverflow
  • I suspect this is a compiler error, actually.

    Here's a short but complete program to show why I think that:

    using System;
    
    class Test
    {
        static void Main()
        {
            decimal constant = decimal.MaxValue / 10m;
            decimal calculated = decimal.MaxValue;
            calculated /= 10m;
    
            Console.WriteLine (constant);
            Console.WriteLine (calculated);        
        }
    }
    

    Output:

    7922816251426433759354395034
    7922816251426433759354395033.5
    

    I'll dig into the spec to see what guarantees are given.

    EDIT: In the spec, section 7.18 claims:

    The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions, except that where run-time evaluation would have thrown an exception, compile-time evaluation causes a compile-time error to occur.

    That's clearly not the case here. Hmm.

    EDIT: I've submitted a bug to Microsoft Connect. We'll see what happens to it.

    izokurew : This Assert fail: Assert.IsTrue(Decimal.MaxValue / 10m == 7922816251426433759354395033.5m);
    Jon Skeet : Yup, that's doing the constant check. Basically the compiler constant arithmetic is at fault here, I believe.
  • I'd guess because you are overstepping the max precision value when you are dividing by 10. http://en.wikipedia.org/wiki/Arithmetic_precision

    Jon Skeet : No, the precision is the same, as we're using a decimal type. It should easily be able to notice that the division is by zero, keep the mantissa the same and just change the exponent. It's perfectly possible to represent the exact result as a decimal.

.NET 2.0 - Tokenizing space separated text

Suppose you have output like this:

Word1           Word2   Word3      Word4

Where the number of spaces between words is arbitrary. I want to break it into an array of words.

I used the following code:

string[] tokens =
         new List<String>(input.Split(' '))
             .FindAll
             (
                 delegate(string token)
                 {
                      return token != String.Empty;
                 }
             ).ToArray();

Not exactly efficient, but does the job nicely.

How would you do it?

From stackoverflow
  • I would use a regex for the split with "\w+" for the pattern.

    FlySwat : Then I'd have two problems.
    EBGreen : :) - So twice as many chances to expand your abilities.
    EBGreen : Actually I had the pattern wrong anyway...D'OH!
    FlySwat : See, that's why I'm afraid of regex.
  • He already mentions string.Split(). What he's missing is StringSplitOptions.RemoveEmptyEntries:

    string[] tokens = input.Split(new char[] { ' ' },
        StringSplitOptions.RemoveEmptyEntries);
    
    FlySwat : Genius. I didn't notice that overload.
    EBGreen : +1 for using what is already there.
    FlySwat : You have to admit, anonymous delegates make it look cooler =)
    John Rudy : Cooler, yes, more legible and obvious ... not so much. I never noticed this overload either, but it absolutely rocks! +1!
    Joel Coehoorn : The best part is that it's easy to include tabs, newlines, and other whitespace in the array if you want.
    Mitchel Sellers : Wow, in all my time using things I've never noticed this overload...

Convert a text file with Embedded HP Printer Codes to PostScript

I have an application that prints by generating text files with embedded printer codes, then basically just copies the file to the printer to print. I need to take that print file and convert it to an image - just as if it was printed then scanned.

My first thought was to setup a printer with a postscript printer driver attached to a file port, and then run the result through ghostscript to create a tiff, but it isn't working.

Any ideas?

From stackoverflow
  • The printer codes are probably PCL. Maybe pcl-parser could serve as a start for your own tool to do this? There also seems to be a commercial product called PCLXForm.

    Edit: Also investigate GhostPCL.

    rjrapson : The PCLXForm looks like exactly what I need, and isn't too expensive. Thanks - I searched google for days before I asked here. Next time I'll just ask here first :-)

HttpContext.Cache vs Reflection: Which is more efficient?

I'm using reflection to get the Authorize Attributes from controllers and methods.

Since I will need to get this information over and over I'm wondering if it is faster to cache it or to simply continue to use reflection to get it.

Any thoughts?

From stackoverflow
  • In general Reflection is not adviced when speed it to be attained, but you must take into account the time to develop a cache that's really working (thread safe, really fast, which gives you the current value and not an old inconsistent value) and to debug it.

    It's very difficult to tell which architecture will give the best performance, but you can easily write a little test to time the two different scenarios with a small set of simple cases. You might find that the speed is quite the same, or that it is significantly different. Either way you have your answer.

    robDean : I'm not sure I get the part of "develop a cache" since it's dealing with HttpContext.Cache. Can you explain further? Thanks.
  • Hi, this answer comes a bit late but I recently had the same thought myself for one bit in a project and ended up doing the cache using the following extension method GetOrCreate remember to lock the dictionary if you do so.

    Instead you could use an ImmutableAVLTree using a lock-free strategy

    Finally, there are some usefull utilities to use reflection here

    It's part of a full framework but you can just copy this bit of code if that's the only bit you need. Hope this helps

Best way to implement a last-modified column in Sql Server 2005?

How do you implement a last-modified column in SQL?

I know for a date-created column you can just set the default value to getdate(). For last-modified I have always used triggers, but it seems like there must be a better way.

Thanks.

From stackoverflow
  • Triggers are the best way, because this logic is intimately associated with the table, and not the application. This is just about the only obvious proper use of a trigger that I can think of, apart from more granular referential integrity.

    But I think "last-modified" dates are a flawed concept anyway. What makes the "last-changed" timestamp any more valuable than the ones that came before it? You generally need a more complete audit trail.

    Shawn Simon : im using it in a grid of patients so the doctors can see the last one they modified, although i agree the date of their last visit seems more useful...
    le dorfier : If it's the last patient they modified, rather than the last time they modified each patient, then it even more needs to be in a separate table - or persisted somewhere else globally.
    Tomalak : I think in the old days they said "treat" or "heal", not "modify". SCNR ;-) More seriously, I agree with @doofledorfer in that a LastModified date is not very useful because it carries next to no information and may lead to false conclusions.
    Shawn Simon : hey man i just follow the spec : >
  • You can use the keyword DEFAULT, assuming you have a default constraint. On insert, there is no need to specify a value, You could use the keyword here too.

    No trigger and is done in the same write as the "real" data

    UPDATE
       table
    SET
       blah,
       LastUpdatedDateTime = DEFAULT
    WHERE
       foo = bar
    
    le dorfier : Agreed. Except you should declare it as DEFAULT for the column in the table schema.
    le dorfier : Mp = wait - that would only work for INSERTs.
    gbn : No, works for UPDATE http://msdn.microsoft.com/en-us/library/ms177523(SQL.90).aspx ... SET { column_name = { expression | DEFAULT | NULL } ...
    le dorfier : Agreed and raise you one. But this is using it for an evil purpose. :)
    le dorfier : gbn, you're right - new fact. But that's even more evil than a trigger. :)
    gbn : It's be a shame not to use it and enjoy the evil though...
  • Using a trigger to update the last modified column is the way to go. Almost every record in the system at work is stamped with an add and change timestamp and this has helped me quite a bit. Implementing it as a trigger will let you stamp it whenever there is any change, no matter how it was initiated.

    Another nice thing about a trigger is that you can easily expand it to store an audit trail as well without too much trouble.

  • And as long as you are doing so, add a field for last_updated_by and update the user everytime the record is updated. Not as good as a real audit table but much better than the date last updated.

    A trigger is the only method you can use to do this. And to those who said triggers are evil, no they aren't. They are the best way - by far - to maintain data integrity that is more complex than a simple default or constraint. They do need to written by people who actually know what they are doing though. But then that is true of most things that affect database design and integrity.

    le dorfier : Agreed. Note my statement about "more granular referential integrity".
  • The only other way to perform this without using triggers is to disallow any inserts/updates/deletes on the table directly via permissions, and insist all these actions are performed via stored procedures that will take care of setting the modified date.

    An administrator might still be able to modify data without using the stored procedures, but an administrator can also disable triggers.

    If there are a lot of tables that require this sort of functionality, I would favour triggers as it simplifies the code. Simple, well written and well-indexed auditing triggers are generally not too evil - they only get bad when you try to put too much logic in the trigger.

implements in UML

Any idea how I express 'implements' in UML? I'm trying to write some actionscript classes in starUML with the help of this handy little template tool: http://blog.flashmech.net/2008/09/generate-actionscript-3-with-staruml/ I want the tool to generate 'implements Iblah' in the classes but am not sure what connector to use, Aggregation? Dependecy?

Any help much appreciated.

From stackoverflow

Split EDMX for Entity Framework - Breakout object layer into separate files - possible?

Is it possible to break out the partial classes in a split edmx's objectlayer.cs into a file per class?

Thanks

From stackoverflow
  • Most of the answers so far seem to be discussing splitting the EDMX into it's parts. It sounds from your question that you want to break out the generated classes into separate files. This is not currently supported, but you may be able to do it using the T4 templates with EF in 4.0. You can already do this using Damien Guard's L2ST4 templates for LINQ to SQL.

    Jim Wooley

    www.ThinqLinq.com

Programatically opening an xml file in MS Word

I'm opening an xml file in Microsoft Word 2007 using C#. I'm using an xsl file to define the layout which worked up to the point that I needed to include an image. I used the typical src html tag and the image displays when the xml is viewed in ie but not when it's opened in Word. I get the image place holder so I can only assume Word simply can't find the image. It's sitting next to both the xml file and xsl file but that doesn't make any difference. To open the document I'm passing the filenames for both the xml and xsl. I would imagine that as the src is in the xsl it would just look relative to one of these files.

Any suggestions would be much appreciated.

[EDIT] Apologies I lost track of this one. I fixed it in the end. I'll check what my solution was tomorrow and update this.

From stackoverflow
  • Did you try specifying the full URL to the file using a format like file://c/blah/blah/abc.gif ?

  • I think you need to specify the working path for the document to point at the base location. Something like this: wordDocument.Path = xml file path (or whatever that image assumes to be the base)

Best Environment For Large Web Application

We are developing a web application, which will have a database with over 5 millon documents, all of them will be in various languages. The site is planned to have more than 3 million visits per month (hopefully more).

We need a stable and scalable solution.

We are now using JEE over JBoss application server with PGSQL DB, but we would like to know if this fits the problem or there is a better solution, because the project is a the beginning and changes are yet viable.

Also, as many of us, doesn't have a lot of experience with this type of projects, the opinions of the ones who does, will be very useful!

I hope I made myself clear. Please let me know if you need more information.
Thanks in advance.

From stackoverflow
  • I personally would not take responsibility to offer own solution to a team without asking for advice from somewhere else first. Same way as chaKa does. What I would not do is to rely on one source of help making final decision.

    You may need to consider following criteria:

    1. How much time do you have? What is development plan? Should you start right away or you will be given time to learn.
    2. Do you need framework? Are you expected to deliver quickly? How many requirements do you have? It all affects will it be framework based solution or from scratch.
    3. Will you support project as well? How many people will do it? You need to know also will project grow slowly or it should be deployed quickly and forgotten.
    4. What skills does your team have? What are they good at?
    5. What would make you excited and want to do your best implementing solution?

    I believe there is more to think about...

    Mg : I have considered some of the points you mention above. We do not have much time, so there isn't very much time to learn. We are 5 people working on this, will the same experience and similar knowledges. We are all good at learning, and we like challenges. We have hell a lot of requirements.
    Mg : Deliver was yesterday. We haven't selected an alternative, we now how the business tier will be, but, when we looked into the requirements, we found that maybe jps and servlets would not fulfill our needs. Thats why we are seeking advice from others who have experience with this kind of applications
  • The architectural design considerations of your solution are probably more important than the choice of "platform". In other words, how are you going to make your application scale? Do you need to store distributed session? Do you need real-time database synchronization or something a little less up to date? How will you do request load balancing, or handle fail over? Can the business logic work over a distributed set of nodes/sites or whatever you envisage.

    Once you have a design that suits your purposes then the choice of your implementation platform can be a better informed decision. Whether it's java, .net, rails or whatever doesn't really matter. They all have their strength and weaknesses, as do the members of your team. Use their strengths to guide this part of your decision making process. Don't try to learn a new technology in tandem with building what sounds like a fairly serious site.

    I've used JBoss on a pretty large distributed ebook delivery system with tens of thousands of page views per day and it never missed a beat. Likewise I think Stack Overflow is a more than adequate example of the capabilities of the ASP.NET platform with regards to the numbers you are mentioning.

    Hope that helps.