Using "My" in C#

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

I've tried following the simple instructions to be able to use MyServices
within C#...Add a project reference to Microsoft.VisualBasic.dll and add:
"using Microsoft.VisualBasic.MyServices;" to the code module. After that, I
expected to be able to use MyClock or MyComputerInfo, but none of the "My"
stuff appears in the intellisense. I've tried rebuilding (after adding the
reference and the using statement) and still no luck.

What am I doing wrong?

-Scott
 
Scott M. said:
I've tried following the simple instructions to be able to use MyServices
within C#...Add a project reference to Microsoft.VisualBasic.dll and add:
"using Microsoft.VisualBasic.MyServices;" to the code module. After that, I
expected to be able to use MyClock or MyComputerInfo, but none of the "My"
stuff appears in the intellisense. I've tried rebuilding (after adding the
reference and the using statement) and still no luck.

What am I doing wrong?

The easiest way to find out what the VB code would do is to compile
some VB and then look at it in ILDASM or Reflector.

However, I've seen that certainly you could use the
Microsoft.VisualBasic.Devices namespace and create a new instance of
ComputerInfo, for example.
 
Am I correct in noticing that the one big difference between using the "My"
keyword/namespace in VB .NET and using the classes within the Devices
namespace in C# is that in C#, you must make an instance of the class before
you can use it, while in VB .NET you don't?
 
Scott M. said:
Am I correct in noticing that the one big difference between using the "My"
keyword/namespace in VB .NET and using the classes within the Devices
namespace in C# is that in C#, you must make an instance of the class before
you can use it, while in VB .NET you don't?

I suspect (although I haven't tried it) that the difference is actually
that it's not *clear* that you're creating an instance of the class
before using it in VB - but that it's still doing it under the covers.
 
Here's a support file that we insert into some VB to C# conversions to cover
aspects of 'My' that aren't converted in-line
//----------------------------------------------------------------------------------------
// Copyright © 2005 - 2007 Tangible Software Solutions Inc.
//
// This file provides classes to reproduce most of the My.Computer and My.User
// functionality in VB. Calls to My.Computer.Clipboard,
My.Computer.FileSystem,
// and My.Computer.Registry have been converted where they were referenced.
//
// Note: My.Settings is converted elsewhere to the standard C#
Properties.Settings.
// Note: My.Resources is converted elsewhere to the standard C#
Properties.Resources.
// Note: My.Application calls are redirected to the converted
My.MyApplication
//----------------------------------------------------------------------------------------

using Microsoft.VisualBasic.ApplicationServices;
using Microsoft.VisualBasic.Devices;
using System.Security.Principal;
using System.Windows.Forms;

namespace My
{
internal static class Computer
{
//Instant C# Notes:
//Calls to My.Computer.Clipboard have been redirected to
System.Windows.Forms.Clipboard
//Calls to My.Computer.FileSystem have been redirected to
Microsoft.VisualBasic.FileIO.FileSystem
//Calls to My.Computer.Registry have been redirected to
Microsoft.Win32.Registry

internal readonly static Audio Audio;
internal readonly static Clock Clock;
internal readonly static ComputerInfo Info;
internal readonly static Keyboard Keyboard;
internal readonly static Mouse Mouse;
internal readonly static string Name;
internal readonly static Network Network;
internal readonly static Ports Ports;
internal readonly static Screen Screen;

static Computer()
{
Audio = new Audio();
Clock = new Clock();
Info = new ComputerInfo();
Keyboard = new Keyboard();
Mouse = new Mouse();
Network = new Network();
Ports = new Ports();
Screen = Screen.PrimaryScreen;

ServerComputer ThisServerComputer = new ServerComputer();
Name = ThisServerComputer.Name;
}
}

internal static class User
{
private static Microsoft.VisualBasic.ApplicationServices.User ThisUser =
new Microsoft.VisualBasic.ApplicationServices.User();

internal static IPrincipal CurrentPrincipal
{
get
{
return ThisUser.CurrentPrincipal;
}
}
internal static bool IsAuthenticated
{
get
{
return ThisUser.IsAuthenticated;
}
}
internal static string Name
{
get
{
return ThisUser.Name;
}
}
internal static void InitializeWithWindowsUser()
{
ThisUser.InitializeWithWindowsUser();
}
internal static bool IsInRole(BuiltInRole ThisRole)
{
return ThisUser.IsInRole(ThisRole);
}
internal static bool IsInRole(string ThisRole)
{
return ThisUser.IsInRole(ThisRole);
}
}
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
C++ to Ruby
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB or C# to C++/CLI
 
The "My" namespace is something that is VB.NET compiler specific and depends
on the (VB) project type, a Web project will have a different "My" namespace
than a Forms project for instance, "My" doesn't really exists outside the VB
IDE, that is C# projects won't see it.
That's also why "My" functionality isn't fully supported from other
languages other than VB, from C# you can refer to the Microsoft.VisualBasic
namespaces like Devices and MyServices and use their classes, but you can't
use the My. syntax.

Willy.
 
Thanks!


David Anton said:
Here's a support file that we insert into some VB to C# conversions to
cover
aspects of 'My' that aren't converted in-line:
//----------------------------------------------------------------------------------------
// Copyright © 2005 - 2007 Tangible Software Solutions Inc.
//
// This file provides classes to reproduce most of the My.Computer and
My.User
// functionality in VB. Calls to My.Computer.Clipboard,
My.Computer.FileSystem,
// and My.Computer.Registry have been converted where they were
referenced.
//
// Note: My.Settings is converted elsewhere to the standard C#
Properties.Settings.
// Note: My.Resources is converted elsewhere to the standard C#
Properties.Resources.
// Note: My.Application calls are redirected to the converted
My.MyApplication.
//----------------------------------------------------------------------------------------

using Microsoft.VisualBasic.ApplicationServices;
using Microsoft.VisualBasic.Devices;
using System.Security.Principal;
using System.Windows.Forms;

namespace My
{
internal static class Computer
{
//Instant C# Notes:
//Calls to My.Computer.Clipboard have been redirected to
System.Windows.Forms.Clipboard
//Calls to My.Computer.FileSystem have been redirected to
Microsoft.VisualBasic.FileIO.FileSystem
//Calls to My.Computer.Registry have been redirected to
Microsoft.Win32.Registry

internal readonly static Audio Audio;
internal readonly static Clock Clock;
internal readonly static ComputerInfo Info;
internal readonly static Keyboard Keyboard;
internal readonly static Mouse Mouse;
internal readonly static string Name;
internal readonly static Network Network;
internal readonly static Ports Ports;
internal readonly static Screen Screen;

static Computer()
{
Audio = new Audio();
Clock = new Clock();
Info = new ComputerInfo();
Keyboard = new Keyboard();
Mouse = new Mouse();
Network = new Network();
Ports = new Ports();
Screen = Screen.PrimaryScreen;

ServerComputer ThisServerComputer = new ServerComputer();
Name = ThisServerComputer.Name;
}
}

internal static class User
{
private static Microsoft.VisualBasic.ApplicationServices.User ThisUser =
new Microsoft.VisualBasic.ApplicationServices.User();

internal static IPrincipal CurrentPrincipal
{
get
{
return ThisUser.CurrentPrincipal;
}
}
internal static bool IsAuthenticated
{
get
{
return ThisUser.IsAuthenticated;
}
}
internal static string Name
{
get
{
return ThisUser.Name;
}
}
internal static void InitializeWithWindowsUser()
{
ThisUser.InitializeWithWindowsUser();
}
internal static bool IsInRole(BuiltInRole ThisRole)
{
return ThisUser.IsInRole(ThisRole);
}
internal static bool IsInRole(string ThisRole)
{
return ThisUser.IsInRole(ThisRole);
}
}
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
C++ to Ruby
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB or C# to C++/CLI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top