Tuesday, April 5, 2011

Quickest way to implement a new interface member in many classes?

Do you know a quick way to implement method(s) from an Interface to a Class. If yes, how can you do it?

Situation : I have an Interface used by over 15 concrete classes. I added a new method and I need to implement this new method in all concrete class.

Update

All my concrete class implement the interface and all the method fine. Later, I add a new method in the interface. To be able to compile, I need to implement the new method in all class. I do not want to go 1 by 1 on each class to implement the method. Is there a way, like "Right clicking the new method" in the interface that will go in all concrete class and all automaticly the new method. This way I will not have to open all class?

From stackoverflow
  • Provide an abstract base class with a default implementation, and then have all your concrete classes inherit that abstract class.

    Ian Varley : Don't know why this was -1 ... that's the right answer. Unless you were looking for "copy and paste it".
    Joel Coehoorn : Maybe Daok was looking for an IDE help, rather than a code solution.
    Daok : I cannot inherit abstract class. Class can inherit only 1 class and many Interface. I am more searching a way to do it, see Aku answer. This solution is not what I search.
    Joel Coehoorn : Your abstract class could also inherit anything else you need.
    Daok : Not all these concretes classes inherit the same class. So I would have to create many Abstract Class. This will become bloaty for no real reason.
  • yes,

    immplement the interface class in each of the 15 classes. Example:

    public class MyClass:MyInteface{
    
    }
    

    afterwards, Hover over the first letter of the interface name, and select from dropdown "Explicitly implement interface ".

    I am assuming you are using Visual Studio.

    Daok : I know that way... I am searching something faster.
    Paige Watson : While this is a nice way to stub out the class functions, the IDE will add an exception ("This functionality has not yet been implemented" or something) to the function. Make sure that you remove this if your class is not going to do anything with the added function or you'll have problems later.
  • Since you mentioned that you have ReSharper installed, here some way to quickly implement this:

    • Use "Find Usages Advanced" with "Implementations" checkbox checked
    • For each class use quick action "Implement members"

    Also you can use "solution wide analysis" feature of ReSharper - it will quickly find all classes that don't implement this new method

    EDIT:

    Finally I found a really quick way:

    • Save method signature in clipboard.
    • Position cursor on Boo in IFoo interface (notice code error - empty code block, this is intentional).
    • Right click and choose Refactor->"Push Members down"
    • Select needed classes in the shown dialog box and click Next.
    • Restore method signature from clipboard

      internal interface IFoo { void Boo() { } }

      class Boo:IFoo { }

      class Foo: IFoo { }

    Daok : This is the only answer for the moment that look "fast". Thx aku.
    Mark Brittingham : ReSharper is an amazing tool, eh? You *did*, however, steal my answer ;-)

Friday afternoons....

What do you do on a Friday afternoon at work when you've lost your drive to work?

From stackoverflow
  • I answer questions on Stack Overflow for some intellectual stimulation.

  • Spend some time on StackOverload, of course....

    Tony Lambert : is that a StackOverflow competitor?
    duffymo : The competitor to StackOverflow.
  • Have a life.

    Preferably without computers and internet access...

  • I go home and see the wife and kids....

  • I usually start working on a side project or start learning something new, like F#. Yea, I am a geek, :)

  • Nothing that will have consequences.

    Far future prototyping. Exploratory test harnesses. Housekeeping.

Hex-Value in Visual Basic

Can someone just help me refresh my mind?

How do you specify hex values in a Visual Basic 6 / VBScript Source?

It's not 0xABCD as it is in C++, that's what I can remember... It was something similar... But what?

From stackoverflow
  • msgbox hex(255)

    BlaM : No, the other way around. For example if I want to assign a hex-value to a variable, not to encode an integer to a hex string.
    Fredou : your correct, my mistake
  • Try &HABCD, that's how it works for most BASIC languages.

    BlaM : Great, thanks. That's it!
    schnaader : No problem. I always knew using PowerBasic for DOS would be useful for something ;)
  • &H<hex-value> if my memory serves my correctly.

    Like: &HABCD

  • VBScript/VBA/VB6 (and lower):

    Dim MyValue As Integer
    MyValue = &h1234
    

    VB (.NET Framework 2.0) and lower:

    Dim MyValue As Integer = &h1234
    

    VB (.NET Framework 3.0/3.5):

    Dim MyValue = &h1234
    

    All versions can use the same syntax as lower versions but not the other way around...

Silverlight Architecture Guidance - Lazy Loading

Background: We have an offshore group working up a Silverlight 2 prototype for us. There is the conception that we need to be very concerned with lazy loading of various "screens"/parts of the application. The offshore group has decided to dynamically load assemblies in order to achieve this; however, I would think MS has already dealt with this issue.

Question: Does Silverlight already deal with loading assemblies in an intelligent manner or is that something that we will have to be concerned with?

From stackoverflow
  • Silverlight does have a built in ability to fetch various bits of the application in an on demand basis. However all these bits would be listed initially in the manifest.

    However I suspect your partners are thinking in terms of dynamically determining new chunks of the application being downloaded and displayed even after the intial xap has been built.

    I don't think you should be too concerned about this, its not actually excessively difficult to achieve.

    brendanjerwin : Not concerned with dynamically adding new parts. We will redeploy the whole app if there is new functionality. I believe the concern is completely about performance. i.e. excessive startup time downloading the whole app at once.
  • Cut+paste from my existing answer on another question.. Jesse Liberty has some decent tutorials on multi-page Silverlight apps:

    Tutorial and Update

    You can make your initial app nothing more than a basic "stub" and then load the other bits once you at least have some GUI available. Doesn't come out of the box, but it's straightforward to do.

    brendanjerwin : But the question is: DO I want to? Is it something that should be done? And why?
    Steven Robbins : Well that's entirely up to you. How big is the payload going to be if it's all in one? How long will that take to download? Is that an acceptable time for a user to see "nothing"? These are the questions that dictate whether or not to lazy load them or not.
  • Tim Heuer has a good video explaining how you can dynamicly load assemblies at runtime:

    http://silverlight.net/learn/learnvideo.aspx?video=65687

    There should also be a version of the Prism toolkit from the Microsoft PnP people sometime in the 1st quarter that will help with that.

    Steven Robbins : There's something very wrong when I have more up votes on a Silverlight question than Shawn Wildermuth does :-)

Why do managed attributes just work for class attributes and not for instance attributes in python?

To illustrate the question check the following code:

class MyDescriptor(object):
  def __get__(self, obj, type=None):
    print "get", self, obj, type
    return self._v
  def __set__(self, obj, value):
    self._v = value
    print "set", self, obj, value
    return None

class SomeClass1(object):
  m = MyDescriptor()

class SomeClass2(object):
  def __init__(self):
    self.m = MyDescriptor()

x1 = SomeClass1()
x2 = SomeClass2()

x1.m = 1000
# ->  set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> 10000
x2.m = 1000 # I guess that this overwrites the function. But why?
# ->
print x1.m
# -> get <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> <class '__main__.SomeClass1'> 10000
print x2.m
# -> 10000
  1. Why doesn't x2.m = 1000 not call the __set__-function? It seems that this overwrites the function. But why?
  2. Where is _v in x1? It is not in x1._v
From stackoverflow
  • To answer your second question, where is _v?

    Your version of the descriptor keeps _v in the descriptor itself. Each instance of the descriptor (the class-level instance SomeClass1, and all of the object-level instances in objects of class SomeClass2 will have distinct values of _v.

    Look at this version. This version updates the object associated with the descriptor. This means the object (SomeClass1 or x2) will contain the attribute _v.

    class MyDescriptor(object):
      def __get__(self, obj, type=None):
        print "get", self, obj, type
        return obj._v
      def __set__(self, obj, value):
        obj._v = value
        print "set", self, obj, value
    
    Philipp der Rautenberg : Thanks, that is a good idea to implement. One question to _v: I also couldn't access it via "MyDescriptor._v" - which is clear as it not a class attribute of MyDescriptor. But what is the explicit name of the instance? It should be accessible somehow, right?
    Sebastjan Trepča : You cannot access it, because it gets overwritten. In S.Lott case, you can access it via x._v
    Sebastjan Trepča : ehh, sorry, x1._v
    S.Lott : Descriptor instances are HARD to access -- the point is that the containing class transparently calls their __set__ and __get__ functions for you. It never refers to the descriptor -- it calls the __set__ or __get__.
    S.Lott : @Sebastjan Trepča: it's better to completely fix a comment by posting a new one and deleting the old comment. A sequence of comments is hard to read. But a new, complete comment is better.
  • You should read this and this.

    It overwrites the function because you didn't overload the __set__ and __get__ functions of SomeClass but of MyDescriptor class. Maybe you wanted for SomeClass to inherit MyDescriptor? SomeClass1 prints the "get" and "set" output because it's a static method AFAIK. For details read the upper links.

  • I found _v of x1: It is in SomeClass1.__dict__['m']._v

    For the version suggested by S.Lott within the other answer: _v is in x1._v

Setting the umask of the Apache user

I am setting up a LAMP server and would like to set Apache's umask setting to 002 so that all Apache-created files have the group write permission bit set (so members of the same group can overwrite the files).

Does anyone know how to do this? I know that on Ubuntu, you can use the /etc/apache2/envvars file to configure the umask, but the server is running CentOS.

Update This question is related to another I asked a while ago (http://stackoverflow.com/questions/174715/linux-users-and-groups-for-a-lamp-server). If prefered, please update this other question with what the best set-up is to use for having a developer user on a server that can edit files created by the apache user.

From stackoverflow
  • Drifting away from the "tried and true Apache way" is usually not recommended. Lots of time and hard won experience has gone into the selection of such things.

    DavidWinterbottom : Must have been asleep when that memo was passed around - any links for the tried and tested way?
    Maine : -1 Spreading FUD rarely helps.
    Rob Wells : @Maine, it's not FUD. Google umask 002 apache and take your pick.
    Rob Wells : @DavidWinterbottom, this has been policy since the mid-nineties for the site that I'm associated with. Thiird biggest website in the world btw.
    Rob Wells : Forgot to say, see also the book of "lock it down unless you really need it" security. Only explicitly allow what you want to allow.
    dannysauer : @Rob - Using a umask of 002 will not be a problem unless the apache user's primary group contains untrusted users (which would be a terrible setup) or Apache is a member of a group with untrusted users /and/ is writing to a directory owned by that group with the setgid bit set. Further, the Apache way is the Unix way - to create files using the most permissive values, and let the local sysadmin determine appropriate permission restrictions using the umask. Ergo, this is misguided FUD.
  • Apache inherits its umask from its parent process (i.e. the process starting Apache); this should typically be the /etc/init.d script. So put a umask command in that script.

  • For CentOS and other Red Hat distros, add the umask setting to /etc/sysconfig/httpd and restart apache.

    [root ~]$ echo "umask 002" >> /etc/sysconfig/httpd
    [root ~]$ service httpd restart
    

    More info: Apache2 umask | MDLog:/sysadmin

    For Debian and Ubuntu systems, you would similarly edit /etc/apache2/envvars.

How to resolve naming conflicts when multiply instantiating a program in VxWorks.

I need to run multiple instances of a C program in VxWorks (VxWorks has a global namespace). The problem is that the C program defines global variables (which are intended for use by a specific instance of that program) which conflict in the global namespace. I would like to make minimal changes to the program in order to make this work. All ideas welcomed!

Regards

By the way ... This isn't a good time to mention that global variables are not best practice!

From stackoverflow
  • The easiest thing to do would be to use task Variables (see taskVarLib documentation).

    When using task variables, the variable is specific to the task now in context. On a context switch, the current variable is stored and the variable for the new task is loaded.

    The caveat is that a task variable can only be a 32-bit number. Each global variable must also be added independently (via its own call to taskVarAdd?) and it also adds time to the context switch.

    Also, you would NOT be able to share the global variable with other tasks.
    You can't use task variables with ISRs.

  • Another possible solution would be to put your application's global variables in a static structure. For example:

    From:

    
    int global1;
    int global2;

    int someApp() { global2 = global1 + 3; ... }

    TO: typedef struct appGlobStruct { int global1; int global2; } appGlob;

    int someApp() { appGlob.global2 = appGlob.global1 + 3; }

    This simply turns into a search & replace in your application code. No change to the structure of the code.

  • I had to solve this when integrating two third-party libraries from the same vendor. Both libraries used some of the same symbol names, but they were not compatible with each other. Because these were coming from a vendor, we couldn't afford to search & replace. And task variables were not applicable either since (a) the two libs might be called from the same task and (b) some of the dupe symbols were functions.

    Assume we have app1 and app2, linked, respectively, to lib1 and lib2. Both libs define the same symbols so must be hidden from each other.

    Fortunately (if you're using GNU tools) objcopy allows you to change the type of a variable after linking.

    Here's a sketch of the solution, you'll have to modify it for your needs.

    First, perform a partial link for app1 to bind it to lib1. Here, I'm assuming that you've already partially linked *.o in app1 into app1_tmp1.o.

        $(LD_PARTIAL) $(LDFLAGS) -Wl,-i -o app1_tmp2.o app1_tmp1.o $(APP1_LIBS)
    

    Then, hide all of the symbols from lib1 in the tmp2 object you just created to generate the "real" object for app1.

        objcopymips `nmmips $(APP1_LIBS) | grep ' [DRT] ' | sed -e's/^[0-9A-Fa-f]* [DRT] /-L /'` app1_tmp2.o app1.o
    

    Repeat this for app2. Now you have app1.o and app2.o ready to link into your final application without any conflicts.

    The drawback of this solution is that you don't have access to any of these symbols from the host shell. To get around this, you can temporarily turn off the symbol hiding for one or the other of the libraries for debugging.

  • Another Possibility:
    If you are using Vxworks 6.x, you can make a Real Time Process application.
    This follows a process model (similar to Unix/Windows) where each instance of your program has it's own global memory space, independent of any other instance.