1

I'm attempting to move from using VB WPF to C# WPF, What I have attempted so far is using an online converter because of the amount of code I have. The problem being that I have come into some troubles understanding some errors presented and being a beginner in C# I'm a little lost.

The code below is what I'm currently using with standard VB WPF and work perfectly fine and a copy of what the c# converter changes it into. (Note I have added Bing Maps WPF Reference to both VB and C#)

Private Sub Aberdeen() Handles BTNCounty.Click
    If TXTCounty.Text = "Aberdeen" Then

        Dim CountyLocation(2) As Microsoft.Maps.MapControl.WPF.Location

        CountyLocation(0) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
        CountyLocation(1) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
        CountyLocation(2) = New Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633)


Dim names = New String() {"Aberdeen Central",   "Aberdeen Lochnagar", "Aberdeen Kincorth"}

 For index = 0 To CountyLocation.Length - 1
            Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()

            Dim CoordinateTip = New ToolTip()
            CoordinateTip.Content = names(index)

            Pin.Location = CountyLocation(index)
            Pin.ToolTip = CoordinateTip
            BingMap.Children.Add(Pin)



        Next


    End If
End Sub

Below is the example of the converted code into c#

private void Aberdeen()
{

if (TXTCounty.Text == "Aberdeen") {
    Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];

    CountyLocation(0) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
    CountyLocation(1) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
    CountyLocation(2) = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);


    dynamic names = new string[] {
        "Aberdeen Central",
        "Aberdeen Lochnagar",
        "\tAberdeen Kincorth"
    };

    for (index = 0; index <= CountyLocation.Length - 1; index++) {
        dynamic Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();

        dynamic CoordinateTip = new ToolTip();
        CoordinateTip.Content = names(index);

        Pin.Location = CountyLocation(index);
        Pin.ToolTip = CoordinateTip;
        BingMap.Children.Add(Pin);



    }


}
}

I recieve 3 errors which I was wondering if you could tell me what they mean and how to resolve the issue?

  1. CountyLocation is a variable but used like a method?

2 The name index does not exist in the current context?

3 System.Windows.FrameworkElement.ToolTip is a property but is used like a type?

Any help would be much appreciated as this very much foreign territory for me.

1
  • Your VB.net code is a Form Project. The C# converted to a console application. Error 1 is due to the textbox on the form doesn't exist in the console application. Commented Jan 17, 2016 at 16:55

1 Answer 1

6

Please see the answers inline. The main issue is that the converter has converted all your type inference calls (Dim variable = ...) to dynamics, which is incorrect. You should use var for type inference.

private void Aberdeen()
{

    if (TXTCounty.Text == "Aberdeen") {
        Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];

        // Error 1: Setting array variables is done using square brackets, otherwise it's considered a method invocation
        CountyLocation[0] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
        CountyLocation[1] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
        CountyLocation[2] = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);

        // extra: you don't need dynamic here, just var will do
        var names = new string[] {
            "Aberdeen Central",
            "Aberdeen Lochnagar",
            "\tAberdeen Kincorth"
        };

        // Error 2: you need to declare the index variable (added var)
        for (var index = 0; index <= CountyLocation.Length - 1; index++) {
            // Error 3: don't need dynamic here
            var Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();

            // don't need dynamic here 
            var CoordinateTip = new ToolTip();
            // Same as error 1: Array access is done with square brackets
            CoordinateTip.Content = names[index];

            // Same as error 1: Array access is done with square brackets    
            Pin.Location = CountyLocation[index];
            Pin.ToolTip = CoordinateTip;
            BingMap.Children.Add(Pin);
        }   
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The two array accessors also need fixing (e.g. names(index) should be names[index]).
Good catch, added it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.