Conditional Compilation Problem

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

WatersideAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
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

0
ste5anSenior DeveloperCommented:
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

1
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

Any ideas James ?
0
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
WatersideAuthor 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.
0
Jacques Bourgeois (James Burger)PresidentCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.