Monday, February 21, 2011

Why declare static classes as sealed and abstract in C#?

This MSDN article states that static classes should be declared as sealed and abstract. I was under the impression that static classes were already sealed. Why would you also need to declare a static class as sealed?

From stackoverflow
  • That may be the difference between good code and crappy code. In fact, it depends on how you're going to use your code - separate it - and what kind of security you need for each class.

  • I think the pertinent bit from that article is:

    "Do declare static classes as sealed and abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes."

    Remember, the .NET framework supports a lot of different languages.

    Guy : Thanks - It was late at night and I wasn't reading properly
  • It appears to be saying that declaring a class as sealed and abstract with a private constructor is an alternative to the static class if the language does not support static classes.

  • C# v1 did not allow 'static' keyword on class. So if you have a class with only static methods, it was suggested to declare it 'sealed', 'abstract' and make constructor private. This means that no one can instantiate your class or try to inherit from it. [It doesn't make sense to inherit from a class which has only static methods.]

    C# v2 allowed the static keyword on a class so you don't have to use the above trick to enforce correct usage.

    Henk Holterman : C#1 did have a static keyword, it just wasn't allowed on class definitions
    SolutionYogi : Henk, you are right. I corrected my answer, thank you.
    • One of the effects of marking a class as abstract is that it cannot be instantiated.
    • One of the effects of marking a class as sealed is that is cannot be inherited.

    That's what a static class actually is -- a class that cannot be instantiated and that cannot be inherited.

    So the article doesn't state you should mark your static classes as abstract and sealed additionally, but this is the way how static classes are represented in IL.

    (Other languages may allow you to do this if they do not have a static keyword, although I doubt it, because an abstract sealed class doesn't make a lot of sense semantically.)

C# WinForms User/Permission management

Can anyone provide me a link of any example WinForms application that implements the concept of User authentication and authorization one the basis of Roles or Groups?

The application should allow access of users to the main-menu on the basis of their roles.

From stackoverflow
  • If you are building the menu from a database table, you can security trim it by joining it to another table or view that excludes records on the basis of the permissions set.

    The diagram below illustrates the basics of setting up role permissions for users, and contains a couple of other tables for security trimming individual controls on the form as well.

    Controls Based Security in a Windows Forms Application
    http://www.simple-talk.com/dotnet/windows-forms/controls-based-security-in-a-windows-forms-application/
    .

    alt text

AfxGetAppName() returns garbage characters

I have the following line of code in my application:

CString strAppName = AfxGetAppName();

Sometimes it fills strAppName up with garbage characters, and I can't figure out why.

Anyone have any ideas?

TIA.

From stackoverflow
  • That is possible if you change m_pszAppName manually.

    At the very beginning of application initialization, AfxWinInit calls CWinApp::SetCurrentHandles, which caches the current value of the m_pszAppName pointer as follows:
    pModuleState->m_lpszCurrentAppName = m_pszAppName;

    That is, the module state struct holds a copy of the m_pszAppName pointer. If you change m_pszAppName in InitInstance as adviced in MSDN, you still have the old pointer value in pModuleState->m_lpszCurrentAppName. The AfxGetAppName() function returns AfxGetModuleState()->m_lpszCurrentAppName.

    You could add data breakpoint on m_lpszCurrentAppName and on m_pszAppName. It is initialized in <...>\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\appinit.cpp file. You'll see what is going on with that variable and who's trying to change it.

    Justin : My application does not change `m_pszAppName`. It changes some registry keys though, could that be the issue?
    Kirill V. Lyadvinsky : I don't think that changing registry keys could implicitly change `m_pszAppName`.

sql server Invalid object name '#temp'

Hi , i have created a procedure

create procedure testProcedure_One

as

DECLARE @Query nvarchar(4000)

begin

SET @Query = 'SELECT * into #temptest FROM Table1'

Exec sp_Executesql @query

SELECT * FROM #temptest drop table #temptest end

When i run the procedure testProcedure_One i am getting "Invalid object name '#temp' " error message

but if i use ##temp means its working.

create procedure testProcedure_two

as

DECLARE @Query nvarchar(4000)

begin

SET @Query = 'SELECT * into ##temptest FROM Table1'

Exec sp_Executesql @query

SELECT * FROM ##temptest drop table ##temptest end

testProcedure_two is working fine the What might be the issue . How can i solve it ?

Please help.

From stackoverflow
  • Presumably you have following code that SELECTs from #temp, giving you the error?

    It's down to scope. ##temp is a global temporary table, available in other sessions. #temp is "local" temporary table, only accessible by the current executing scope. sp_executesql runs under a different scope, and so it will insert the data into #temp, but if you then try to access that table outside of the sp_executesql call, it won't find it.

    e.g. This errors as #Test is created and only visible to, the sp_executesql context:

    EXECUTE sp_executesql N'SELECT 1 AS Field1 INTO #Test'
    SELECT * FROM #Test
    

    The above works with ##Test as it creates a global temporary table.

    This works, as the SELECT is part of the same scope.

    EXECUTE sp_executesql N'SELECT 1 AS Field1 INTO #Test; SELECT * FROM #Test'
    

    My questions would be:
    1) do you really need to use temp tables, can you not find a solution without them using e.g. a subquery?
    2) do you really need to execute sql like this using sp_executesql?

    Jebli : Thanks.I know it was out of scope but it didn't strike for me that i have to do the select with the same query.
  • thanks AdaTheDev had given me the right solution.

TSR Programs in C

can anybody suggest a good book (or website link)for studying TSR programs in C?? Thanks in advance..

From stackoverflow
  • What? TSR? Are you on DOS still?

    See wikipedia

  • On the assumption that you really do mean TSR as in Terminate and Stay Resident, here's a wander through ancient history:

    The long out of print The Programmer's Guide MSDOS by Dennis M. Jump (1984, ISBN 0-8359-5655-5) was a good reference to all you'd need to know. Of course, C compilers that targeted pure DOS without too much runtime burden were rare, but Borland TurboC came pretty close. I'm pretty sure I once wrote a TSR in Datalight C too, but it has been a long time since those days.

    The modern descendant of TurboC is still available, and the compiler is free. I don't think it can target classic real-mode DOS any more, however.

    True Turbo C v2.01 is available at the museum.

    The FreeDOS folk probably have useful tools as well.

  • The A to Z of C has a chapter on TSR programming.

to implement online converting engine from browser to web server

Hello im looking for common way to build the right client server architecture when my goal is : the browser submit data to the web server -> the web server response with converted pdf file based on that data now my question is this : where to implement this convention engine ( that is static lib ) as web server plug in (apache, lighthttpd,nginx)? or as separate process in different server ?

From stackoverflow
  • This sounds to me like you are wanting to dynamically generate PDFs based on the data captured from a web-based form? If so, there are many different ways to achieve this- and each person from each language discipline will promote their method as being simple.

    : i know , this is what i like to learn

Simulate keypress in a Linux C console application

Is there any way to simulate a keypress in Linux using C?

In my specific situation, I'm on Ubuntu 9.04 and need a simple app that invokes a press on the "pause" button when launched. That would get an iframe in Firefox to refresh using Javascript.

From stackoverflow
  • I assume you mean the "X11 application" - it is not entirely clear from your description what you are planning to do. The below code snippet will send the "pause" keycode to the application that currently has the keyboard input focus under X11 using XTest extension - from what I've read this is the most compatible way to "fake" the keyboard events. See if you might apply this to your scenario (no error check on whether the XOpenDisplay succeeded, to make it simpler).

    #include <X11/Xlib.h>
    #include <X11/keysym.h>
    #include <X11/extensions/XTest.h>
    ...
    Display *display;
    unsigned int keycode;
    display = XOpenDisplay(NULL);
    ...
    keycode = XKeysymToKeycode(display, XK_Pause);
    XTestFakeKeyEvent(display, keycode, True, 0);
    XTestFakeKeyEvent(display, keycode, False, 0);
    XFlush(display);
    

    You will need to link with the -lX11 -lXtst.

    Obviously the firefox would need to have focus at that time.

    However, I would be curious to know what is the bigger task that you are trying to accomplish - I suspect there should be a more elegant solution than spoofing the keypress events.

    Andrew Y : side comment about my own use case for the above code: I had it an OpenCV-powered weekend hack that allowed me to fake the arrow keypresses by moving my head in front of webcam (so I could scroll the /. posts and eat sandwich at the same time :-)
    Baversjo : I want to use a griffin powermate to reload an iframe :)
    Andrew Y : Ah, so it's pretty much the same usage as my case :)