jump to navigation

Things you never did with your code in school that you MUST do 15 August 2012

Posted by golda972 in Uncategorized.
add a comment

I’ve never had much patience for top ten lists. So this may or may not end up being ten items. For today, I have two items.

In any case – let’s face it: coding in school is not the same as coding for real. OK, Capstone/final project may approach real life. That really depends on the project.

So if you’re still running a one-man ship after graduation, here are some things that you will really, really hug yourself for doing when things go wrong:

  1. Version control. This does not need to cost money. Subversion is a wonderful product with plugins that you can use to integrate it with Windows and Visual Studio, if those are the tools you know from school. Start by installing Tortoise for SVN – then, right-click on the folders of your project and you’ll see the menu that lets you add them to version control. Then, install AnkhSVN for integration with Visual Studio.
  2. Error logging. Code that will be developed on one computer, tested on a second, and run on many others will not provide you with the feedback you need to debug it. Unless you tell it to give you that feedback. Some experience with school projects may have left you with an inclination to wrap things in a try block and pair that with an empty catch, so that when your code fails no one will know. Outgrow that. At the very least, write your errors to a text file (include the exception’s Message and StackTrace).

גישה לפקדים בתוך TemplateField 6 December 2011

Posted by golda972 in Uncategorized.
add a comment

שאלה מחברת הסדנא מלכה:

 יש לי גריד ובתוכו TemplateField ובתוכו כמה רכיבים ItemTemplate.

אחד הרכיבים הוא Asp:Label שאני שופכת אליו תוכן של שדה מהDB, שהוא שדה שמהווה שרשור של כמה שורות מטבלה. השורות מופרדות בפסיקים. אני רוצה בקוד להפוך כל פסיק לתגית <br /> כדי שיציג את התוכן בכמה שורות.

האם יש דרך לגשת דרך הקוד לLabel הזה?

 

תשובת הסדנא בדוט נט:

המקום בשביל גישה לשדות של שורה הוא בevent של RowDataBound, בו בeventArgs יש לך את השורה ואז את יכולה לחפש בתוכו את הפקד שלך:

 

    <!– .aspx –>

    <asp:GridView
ID=”gv”
runat=”server”
OnRowDataBound=”gv_RowDataBound”>

        <Columns>

            <asp:TemplateField>

                <ItemTemplate>

                    <asp:Label
ID=”l”
runat=”server”
/>

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

    </asp:GridView>

 

    //in CodeBehind (.aspx.cs)

    protected
void gv_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        Label l = e.Row.FindControl(“l”) as
Label;

 

        if (l != null)

            l.Text = “hi”;

    }

 

למה זה קורה: כי יש אחד בID הזה לכל שורה ושורה, מה שגורם למצב שאין דרך לגשת לפקד סתם עפ”י השם שלו.

אם יש לך גם שורות header וfooter הפקד לא יימצא ולכן הבדיקה לnull. אפשר באותה מידה לבדוק את e.Row.RowType.

 

מקווה שזה מספיק ברור.

sq33G

SO alert: rolling your own strongly-typed DataRow 30 November 2011

Posted by golda972 in ADO.net, C#, Stack Overflow, strongly-typed DataSet.
Tags:
add a comment

I hate answering my own questions on SO, but when people insist on answering in the comments, I am left with no choice.

Check it out.

Custom FxCop Rules, Adventure #1 22 November 2011

Posted by golda972 in C#, code analysis, documentation, FxCop.
add a comment

Static code analysis is a beautiful thing. It is also integrated into the higher-end versions of Visual Studio 2010.

Sometimes, though, the built-in rules are not enough. For example:

CA1001:  Types that own disposable fields should be disposable.

This rule is sensible. Nobody wants resources to float around until their containing class gets finalized at some indeterminate point in the future.

The first* issue that arose in our project is that, to my great sorrow, all data management in our project is done with strongly-typed DataSets. Lots and lots of strongly-typed DataSets. And by way of a grandparent, DataSet, DataTable, and DataView all implement IDisposable but do not have any resources that actually need to be released.

This rule (and its brother, CA2000: Dispose objects before losing scope) are important for catching big, fat bugs (“Why won’t the FileStream open? Oh, wait, I never closed it when EatSushiFromFile() crashed last time”). But with all the noise from “Thou shalt dispose the SushiTable because it inherits from DataTable which implements IDisposable,” no one really wants to hear about the rule (“Suppress! Suppress!”).

Getting Started

The process starts with a surprise:

Surprise! Microsoft doesn’t provide documentation for writing custom code analysis rules!

Who does? (I asked)

The Microsoft Code Analysis blog has a good, complete Quick Start – complete except that it tells you nothing about how Introspection, the API that powers FxCop’s static code analysis, is used.

Jason Kresowaty’s FxCop resources contain a more complete documentation. Again, this is enough to get you started, but not enough to carry you through.

The main prize from Jason Kersowaty is his Introspector – this gem is absolutely necessary for revealing what data you can visit when searching for problems in the code under analysis.

The last tool in your arsenal is a .NET decompiler. I went with the open source ILSpy. The tried and true method to write your own rule is to study (and copy and paste, where appropriate) the rules written by the people who had some understanding of the Introspection API. The libraries containing these rules can be found under the same folder as FxCop itself on your computer:

Team Tools\Static Analysis Tools\FxCop\Rules\

I really think that this is enough information to start with. Next post should be about the process and the design pattern I used to create one of my rules.

*Actually, another issue came first. But that’s HP’s business, not yours.

To get rid of the bouncy ball… 9 November 2011

Posted by golda972 in Linux.
Tags: , ,
1 comment so far

There is a reason why Linux is still not ready for non-techie home users.

Fedora 15 + KDE, added a bouncy ball widget. Set it to automatically bounce, just to see what happens.

Uh oh. There goes a bouncy red ball all over my desktop. No obvious way to remove it from the widgets menu. I would right-click on it… but it’s moving too fast!

Google being my friend, I punched in “kde plasma get rid of bouncing ball”.

Oh, glory!

A bug report for KDE on this very topic.

And I quote:

“——- Comment #2 From Salvo “LtWorf” Tomaselli 2010-05-04 07:16:14 ——-
Do you also have in mind some jedi technique to slow down the time so i can
delete a moving plasmoid?”

“——- Comment #7 From Bryan 2011-06-28 22:38:27 ——-
Had to do something like `konsole`, `kquitapp plasma-desktop`, `kate
~/.kde/share/config/plasma-desktop-appletsrc &` then edit the file, save and
finally run `plasma-desktop` from the konsole window, then run `plasma-desktop`
using the run dialog. [caution: order matters on this]”

(I used KWrite. The beautiful point is – Aunt Marge and her bridge club are *not* about to do this.)

Hacking for the Hardware-Challenged 30 June 2011

Posted by golda972 in bootable USB, Linux, rescue.
add a comment

Hacking in the good sense, of course. (No, this is not a .NET post.)

For people with up to 128 MB of flash memory available, there aren’t that many options for bootable media. Even my old friend, Parted Magic, is currently up to 176.2 MB. That’s a tad too large! (for the hardware-challenged)

The good news is that old versions of Parted Magic can still be downloaded. Version 4.11 slides easily by at 98.1 MB.

For people with a little more space, check out the Fedora minimal boot media option. (Ha, now I’ll get you to install Fedora. That’s not a live CD image!)

I might post about writing .NET with Mono sometime soon…

COM Interop: int[*] cannot be cast to… anything useful? 12 June 2011

Posted by golda972 in C#, exception, interop, Windows.
add a comment

What’s to be done with a non-zero based array in .NET?

Back in the day of VB6, arrays were commonly started at 1. So you had arrays of three members: 1, 2, and 3.

But VB6 is no longer being supported, and today we have (in its place?) VB.NET and, of course C#. Theoretically all arrays forever after shall be 0-based… except. VBA (which looks a lot like VB6) still uses 1-based arrays. And Microsoft Office’s Interop libraries are in VBA.

So a property like Range().SynonymInfo.PartOfSpeechList returns int[*], a 1-based array of integers. Which is utterly useless in .NET.

All the old posts (on StackOverflow, eg) suggest casting a non-zero based array (like int[*]) to the untyped Array. This works fine through .NET 3.5. In .NET 4.0 this cast is invalid, though.

[Update]

Cast to Object, and then you can cast to Array… even in .NET 4.0:

Object objThings = (Object)Range().SynonymInfo.PartOfSpeechList;

Array things = (Array)objThings;

>Fresh Perspective: Specifications 18 October 2009

Posted by golda972 in documentation, functional specification, mazal tov, personnel.
add a comment

>I can tell she is a born…. HUNGARIAN! (bravo, bravo, bravo!) 29 September 2009

Posted by golda972 in Hungarian notation, Intellisense, personnel, UI.
add a comment

>Null Coalescing 22 September 2009

Posted by golda972 in C#.
add a comment