Link to home
Start Free TrialLog in
Avatar of RayT
RayTFlag for United States of America

asked on

My Not Working

The following code is throwing an error

Const f As String = my.Computer.FileSystem.SpecialDirectories.MyDocuments
It is saying my is not declared.

How do I fix this?

Microsoft Visual Studio Professional 2019
Version 16.10.3
VisualStudio.16.Release/16.10.3+31424.327
Microsoft .NET Framework
Version 4.8.03752

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Do you require me instead of my
Are you writing a VB.Net app? What kind of project (winforms, wpf, ...)?

Pretty sure that your issue comes from the fact that MyDocuments property cannot be used to initialized a constant. Replace Const with Dim and the error will be solved.
User generated image
BTW, VB.Net is not case-sensitive.
Yes try Dim
You cannot declare a constant like that
It should be Dim as it's a variable
Const is always a fixed value like
Const x = 300
Avatar of RayT

ASKER

This didi not work

Dim f As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments

I check another project.  It's shows that My is { } Namespace

How do I get this Namespce into this application?
Again: Are you writing a VB.Net app? What kind of project (winforms, wpf, ...)?

Which framework are you targeting? 
You want to ensure that you have imported the Microsoft.VisualBasic namespace.

HTH,

-saige-
Avatar of RayT

ASKER

It's a VB.NET App and I'm using Framework 4.8
can you provide the exact error? what type of project is it?
Avatar of RayT

ASKER

        Dim x As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments

        Dim y As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

My. does not work. It showing My is not declared.  It may be inaccessible due to its protection level.

My workaround is the 2nd one.  It works.  However, I prefer using the 1st one.
again: what type of project is it?

have you tried targeting the 4.7.2 framework? (which framework is your other project targeting)?
I really suspect that you are using a project type that is incompatible with the My Namespace.

If you create a demo Windows Forms project, the My Namespace will be automatically created for you as shown here:
User generated image
As I stated, ensure that you have a reference to Microsoft.VisualBasic and have imported the namespace.

-saige-
Avatar of RayT

ASKER

Yeah :(  I upraded to the recommended 4.8 and it broke things.  Do you  think i should go back to 4.7.2?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The property, My.Computer.FileSystem.SpecialDirectories.MyDocuments, is available in .NET Framework 4.8.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.specialdirectories.mydocuments?view=netframework-4.8#microsoft-visualbasic-fileio-specialdirectories-mydocuments

As I stated before, add a reference to Microsoft.VisualBasic and ensure that you have it imported.

-saige-
Avatar of RayT

ASKER

I may go back to 4.8 for now until I can find out the difference between

ProductVersions
.NET5.0, 6.0 Preview 3
.NET Core3.0, 3.1
.NET Framework2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

SpecialDirectories.MyDocuments Property (Microsoft.VisualBasic.FileIO) | Microsoft Docs 
Avatar of RayT

ASKER

After reading this I may go back to 4.8

What's new in .NET 5 | Microsoft Docs 
Avatar of RayT

ASKER

Much thanks to all!