Conditional Compilation Problem

Waterside
Waterside used Ask the Experts™
on
I'm trying to create a Universal App and I'm following this example http://blogs.msdn.com/b/bingdevcenter/archive/2014/06/24/using-maps-in-universal-apps.aspx

I've run the MapView.cs file through a VB converter but the result fails on  Me.Children.Add(_map). as it's undeclared or inaccessible  The Code is ..

#If WINDOWS_PHONE_APP Then
Imports Windows.UI.Xaml.Controls.Maps
Imports Windows.UI.Xaml.Media
Imports Windows.UI.Xaml.Shapes
Imports Windows.UI.Xaml
#Elif WINDOWS_APP Then
Imports Bing.Maps
#End If

Namespace App3
    Public Class MapView
        Inherits Grid
#If WINDOWS_APP Then
		Private _map As Map
		#Elif WINDOWS_PHONE_APP Then
		Private _map As MapControl
#End If

        Public Sub New()
#If WINDOWS_APP Then
			Private _map = New Map()
			#Elif WINDOWS_PHONE_APP Then
			Private _map = New MapControl()
#End If

            Me.Children.Add(_map)
        End Sub
    End Class
End Namespace

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Would rearranging your code this way help?
#If WINDOWS_PHONE_APP Then
Imports Windows.UI.Xaml.Controls.Maps
Imports Windows.UI.Xaml.Media
Imports Windows.UI.Xaml.Shapes
Imports Windows.UI.Xaml
#Elif WINDOWS_APP Then
Imports Bing.Maps
#End If

Namespace App3
    Public Class MapView
        Inherits Grid
#If WINDOWS_APP Then
		Private _map As Map
		#Elif WINDOWS_PHONE_APP Then
		Private _map As MapControl
#End If

        Public Sub New()
#If WINDOWS_APP Then
			_map = New Map()
			Me.Children.Add(_map)
			#Elif WINDOWS_PHONE_APP Then
			_map = New MapControl()
			Me.Children.Add(_map)
#End If
            
        End Sub
    End Class
End Namespace

Open in new window

ste5anSenior Developer

Commented:
Also consider using a more OOP like approach. Use the strategy pattern instead of conditional compilation.

Create your own IMap interface. Derive two classes MapPhone and MapApp from it. Use an IoC container to inject the correct map "strategy".

Then your code becomes much more cleaner and easier to read. E.g. (untested)
Namespace App3
    Public Class MapView
        Inherits Grid

		Private _map As IMap

        Public Sub New()
			_map = IoC.Resolve(Of IMap)()
			Me.Children.Add(_map)            
        End Sub
    End Class
End Namespace

Open in new window

Top Expert 2015

Commented:
There are a few strange things in your code. These damn converters always do bad things, even for simple code. You are always better to rewrite the code, specially when it is so short.

The variable is declared both at the class level and in the constructor????

The variable is declared as Private in the constructor. You cannot do that. Only Dim and Static are accepted inside a method. And because it is already declared at the class level, you do not need to declare it in there, simply call the constructor as shown by MlandaT.

As for the conditional compilation, do you see parts of the code in color or is almost everything grey? If the 2 clauses of your conditions both show in grey, then something is wrong with your setup. You might have copied you code in the wrong type of project, in which case the conditional compilation variables are not defined.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
MlandaT's suggestion has fixed the Add(_map) error but I do have grey code every where.

Any ideas James ?
Top Expert 2015

Commented:
If you look at the project in the Solution Explorer, do you see (Universal Windows) besides the name of the project?

If not, then you are not in a Universal project and the constants are not defined. Simply create a new project by making sure that you are selecting a Universal Windows project as the project template.

Author

Commented:
Yes it's Universal.  But I have no template for VB.net so I had to add the Win8.1 and WP8.1 projects.

The Import statements also show a grey.
Top Expert 2015
Commented:
In newer versions of Visual Studio, Import statements are usually grey when you do not need them. Most of the time, this happens because the Import is already defined at the bottom of the References tab of the project's Properties window.

As for the conditional compilation, I think that it comes from the fact that the C# code uses a template that is not available for VB.

I played around with the different templates, and here is what I got;

In both C# and VB there is a Blank App (Universal Windows) template. When you work with these, the compilation constants are not defined. Also, at first sight, there does not seem to be an easy way to switch between a Phone and a Windows application.

In C#, there is also a BlankApp (Universal Windows 8.1) template. This one does have the 2 constants, and also shows a drop down in the tool bar that lets you switch between the Windows and Phone, activating the proper piece of code.

Both templates have slightly different Properties window and NuGet Package Manager options, so they are definitively different.

Unfortunately, there is no (Universal Windows 8.1) for VB. I did a quick search and did not find one anywhere.

So you either have to take more time than I did looking for a template similar to the 8.1 (I have to go and cannot look further), or find sample code that shows you how to work with the minimal template that is provided for VB. You might want to start here: http://blogs.msdn.com/b/vbteam/archive/2014/06/21/vb-universal-app-downloads.aspx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial