Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Discussions

evildictaitor evildictait​or I'll live forever, or die trying!
  • zero-length string == nothing ??

    stevo_ said:
    VB will use its own library to do string comparisons, of which is will essentially say that, null is nothing, zero length is nothing. If you pull the IL apart, you'll find VB calling a visual basic compiler services method of some sort.

    Kinda one of the reasons I don't like VB, brings a ton of crap with it (althought I think I heard you can tell the compiler not to do this now).
    Under C# this would definitely not be the case - (String.Empty != null). There is a static method on the string class that provides simmilar functionality:

    static bool String.IsNullOrEmpty(String str);

  • switch ​(object.​GetType) ...

    littleguru said:
    TommyCarlier said:
    *snip*
    This looks exactly like the property initializers in C# 3.5...
    True. I forgot about those. They are the C# equivilent in terms of compilation to the With statement in VB, although they can only be used as part of a constructing statement (not just on some arbitrary object)

  • Problems with System.​Web.​Extensions

    Open up the Solution Explorer and expand the References tab. You should delete System.Web.Extentions and then re-add the file via Add Reference and it'll be in the .NET tab.

  • switch ​(object.​GetType) ...

    Yggdrasil said:
    TommyCarlier said:
    *snip*
    VB also had the With keyword to do the same. I don't know if it's available still in VB.NET.
    It's not actually the same thing, since this implies that all method calls work on the same instance, while the fluent interfaces can, theoretically, modify the object being passed between them, like the "20.minutes().ago()" example.
    With actually produces more efficient IL as well (unless you optimise the C# version, whereupon it becomes the same):

    var MyObject = new MyObject();
    MyObject.param1 = "foo"
    MyObject.param2 = "bar"

    goes to

    newobj MyObject()
    stloc.0
    ldloc.0
    ldstr "foo"
    stfld .param1
    ldloc.0
    ldstr "bar"
    stfld .param2

    With MyObject
      .param1 = "foo"
      .param2 = "bar"

    goes to

    newobj MyObject
    stloc.0
    ldloc.0
    dup
    ldstr "foo"
    stfld .param1
    ldstr "bar"
    stfld .param2


    and both optimise to

    newobj MyObject()
    dup
    stloc.0
    dup
    ldstr "foo"
    stfld .param1
    ldstr "bar"
    stfld .param2

  • C# Event​Handlers...

    Bas said:
    evildictaitor said:
    *snip*
    So why didn't they change it to just say "button_Click(); (Press Tab to insert)"?
    I suspect they just haven't implemented it yet.

    If they're sensible they should have it as an option, because lovely as lambda functions are, not everyone likes them.

    The bit that impressed me was that lambda functions are in fact entirely syntactic sugar, and therefore also work under .NET 2.0, so you're right, there's a good argument that this should be changed in future versions of VS/C#.

  • C# Event​Handlers...

    Bas said:
    Why does Visual Studio still come up with the "New EventHandler(button_Click); (Press tab to insert)" tooltip when that approach is no longer necessary?
    For big functions it's still nice, and you'll notice that at the point where TAB builds the stub, it's also got the button_Click bit highlighted, so you can immediately type (o,e) => someFunc(e)

  • Why can't you explicitly cast an int to a string in C#?

    Dexter said:
    evildictaitor said:
    *snip*
    Depends on what you really mean by "contract" in this case. In any case:

    - .NET/C# interfaces are a weak form of contracts. All they do is require a class to implement some methods, everything else is left to the documentation.
    - There's nothing stopping an implementation to exceed contract requirements.
    - Stream's IDisposable implementation exceeds contract requirements and documents that thus becoming a contract in itself because that's how contracts are defined in .NET/C#.
    - Contract or not, the fact that Stream.Dispose writes changes to the store was documented and no matter how the internal implementation changs this must remain true.


    And since _stevo mentioned transactions: the TransactionScope is a perfect example of exceeding IDisposable's "contract". It does the opposite of Stream, it rollbacks any changes that were made. Not only it does that but that's the only way to rollback a transaction because there's no Rollback method provided. Even more, TransactionScope.Dispose documentation does not mention anything about realising managed and unmanaged resources.

    I guess TransactionScope's creation was the moment when IDisposable silently converted to a general purpose interface beyond its "release managed and unmanaged resources" original intent. Because this interface was used by the using C# keyword it is now used to model scope based lifetime similar to C++. Now this is not strictly related to "to Close or not to Close" but about what an "IDisposable contract" can really mean.

    If a class chooses to do something as part of a method that is not defined by the contract's method that's up to it. But you shouldn't rely (in general) on IDisposable doing anything more than disposing of unmanaged resources and memory which is what implementing IDisposable's contract means.

  • switch ​(object.​GetType) ...

    jh71283 said:
    This is a total guess, but would Object.GetType.ToString work?

    If so, you could switch on that...
    You can do, but then you might as well forget about performance if you're doing that.

  • Dynamic generic type creation problem

    You won't be able to get a strongly typed version if the type isn't known until runtime. Strong-typing is a compiler protection which relys on the type of the variable being known at compile-time.

    The alternative is that if you know that at such-and-such a point in your code that such-and-such an object is definitely an int then you can tell the compiler this fact using the cast-directive:

    public void main(){
      someFunc(42, true);
    }

    public void someFunc (object o, bool oIsDefinitelyAnInt){
      if(oIsDefinitelyAnInt){
        int i = (int)o;  // casts o to an int, and from here on you can use i as if it were an int. Note that if your assertion is wrong you'll get an exception here.
      }
    }

  • switch ​(object.​GetType) ...

    blowdart said:
    evildictaitor said:
    *snip*
    Actually that's what I do elsewhere *grin*

    The problem arises due to WCF error handlers; you can apply a global error handler to each endpoint. It gets exceptions as a parameter; which you may well then want to turn into SOAP faults.

    Now in my case it's reasonably easy; there's only 5 exceptions I want to marshal to faults (so a small if IS else if IS works); but I thought it would be nice to open it up.
    By the way, if all of your objects extend from Exception, why did you say they arrive as type Object? You should always try and type things to the most specific type that encompases all of the arguments, which is Exception in this case. As a general rule (so not always) if you're dealing with things typed as object then you've done something wrong.