1

I found very promising code online and I'd like to try it out. Since my project is written in VB.net and the code in question is in C# I started to translate the bits and pieces I need to VB.net. I'm done with translating, but I can't figure out what exactly one specific line of code does and how to translate that to VB.net. And I'm out of ideas how to phrase a search for google to maybe find an answer myself.

So maybe you can shed some light on the mystics of C#?

Here's the C# source code (stripped down to the relevant bits):

public class TestClass
{
    private ListView listView;

    public TestClass(ListView input)
    {
        this.listView = input;
        this.listView.Loaded += new RoutedEventHandler(ListViewLoaded);
        this.listView.Unloaded += new RoutedEventHandler(ListViewUnloaded);
    }

    public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached(
        "Enabled",
        typeof(bool),
        typeof(TestClass),
        new FrameworkPropertyMetadata(new PropertyChangedCallback(OnEnabledChanged)));

    private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        ListView input = obj as ListView;
        new TestClass(input);   // <== this is the mystic line
    }
}

I'm struggling with the last line of code in the static procedure OnEnabledChanged. It looks like the constructor of this class is called but the result isn't assigned to anything.

Translating that to VB.net to just New TestClass(input) results in an syntax error.

I tried a few automatic translators on the internet but they only return New TestClass(input), so they're as smart (or unknowing) as I am.

So can you tell me what this line of code does so that I can translate that to working VB.net?

Stefan
  • 17,448
  • 11
  • 60
  • 79
Nostromo
  • 1,177
  • 10
  • 28

1 Answers1

1

If the line is isolated, i.e.: it really is just new TestClass(input);, it might be intended to trigger some code of the constructor, perhaps some static initialization.

Although bad practice, you can translate it to VB just by storing it in an temporary object. The result is equivalent.

So, in stead of: New TestClass(input)

Dim temp = New TestClass(input)

So in general, about the mystic line:

private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    ListView input = obj as ListView;
    new TestClass(input);   // <== this is the mystic line
}

It constructs an object of TestClass and calls it's constructor, passing in the ListView. The result is discarded.

As mentioned: there is an action applied to the input. While this is really bad practice; you lose the reference to the object which mutates the input list. It's hard to undo it's actions - and can be hard to debug.

But; it also means you cannot omit the line.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • The code in the constructor doesn't do any static stuff, at least not that I can see. It just stores the input parameter to a class variable and attaches two events to it. – Nostromo Jul 12 '20 at 08:45
  • Than the line can be omitted - but: you need to be really sure. I'll update my answer. – Stefan Jul 12 '20 at 08:46
  • I added the code of the constructor in my description. – Nostromo Jul 12 '20 at 08:49
  • Ah, sorry, it is as I just thought; there is something applied to the `input`... ok, then you cannot omitt the line... but the code is a bit messy so to say. – Stefan Jul 12 '20 at 08:51
  • I tried the temporary variable, but my VB.net code doesn't do what I expected it to do, so I have to debug my code and the C# code to find the differences. Thank you for now. – Nostromo Jul 12 '20 at 09:01
  • Possibly there is another issue somewhere. As for your question: putting it in a `Dim temp` is the equivalent. Do nite that thanks is expressed by an upvote ;-) – Stefan Jul 12 '20 at 09:03
  • After fixing the issues the code works with the temporary variable. I think I'm going to change that bit of the code to make it more readable for me. – Nostromo Jul 12 '20 at 10:00
  • I think that will help :-) ... also: mind the event handler binding in the constructor - it does lead to strange results. For example: it might even fire, and/or keep the actual object alive and result in a memory leak. – Stefan Jul 12 '20 at 10:02
  • You actually don't need the temporary variable, although using it is no big deal. The problem is that the compiler won't let you start a line with the New keyword. The solution is to precede it with the Call keyword. This is the one valid use I have found for Call in VB.NET. – jmcilhinney Jul 12 '20 at 12:14
  • @jmcilhinney: I was impressed.... but after I tried it, it didn't work: `Call New Object()` With *Expression is not a method* .... if I do `Call New Object().ToString()` it works fine. Any advise? – Stefan Jul 12 '20 at 12:53
  • 1
    Hmmm... I guess it must be the case that, when I did that, I did actually call a method on the new object and that's why it was allowed. That makes sense because, as suggested, creating a new object and not using it for anything is really not something you should be doing if that type is designed well, so I doubt that it's something that I would have done. I guess I just didn't realise that it was the method call that made the `Call` keyword OK. In that case, I retract my advice. – jmcilhinney Jul 12 '20 at 13:44
  • @jmcilhinney: I agree;in general it's a bit smelly. But thanks for the heads up on `Call`, it's easily forgotten. – Stefan Jul 12 '20 at 16:54