user94893
user94893

Reputation:

Is it possible to create VB.net module-like in C# on .Net 3.5?

I need to use some feature of VB.net that is Module for my Asp.net MVC project. But I found that C# can't handle Module like VB.net does. Please look at the following code.

VB.Net - Globalization Project

Public Module [Module Name]
    Public Const WebsiteName As String = "[My Website Name]"
End Module

VB.Net - Asp.net MVC View Page

<%@ Import Namespace="[Globalization Project Namespace]" %>

<%= WebsiteName %>

C#.Net - Asp.net MVC View Page

<%@ Import Namespace="[Globalization Project Namespace]" %>

<%= WebsiteName                // Error because C# doesn't understand this statement %>
<%= [Module Name].WebsiteName  // It works fine. %>

I think C# see all modules like classes. So, I can't call it directly without giving Module name. Do you have any idea for solving this question?

PS.1 I know. Module isn't correct OOP. But It's short and powerful.

PS.2 I can use VB.net for Globalization project because all of source code will be generated by my custom macro. But I don't like to write source code in VB.net. So, I don't use VB.net as primary language of Asp.net MVC View Page or other project.

Thanks,

Upvotes: 0

Views: 1780

Answers (4)

awe
awe

Reputation: 22462

Static class is the C# equivalent of a VB Module:

public static class MyCSharpModule
{
    public const string WebsiteName = "[My Website Name]";
}

Well, it's the same from a C# user of the module, because in C# you have to specify the class name, even if it is created as a VB Module. If referenced from VB, you need to specify the class name if it comes from a C# dll, but not if it comes from a VB dll...

You could try to use extentions to the Page class:

public static class MyExtentions
{
    public static string WebsiteName(this System.Web.Mvc.ViewMasterPage page)
    {
        return "[My Website Name]";
    }
}

In the page you would call it as a method on the Page:

<%= WebsiteName(); %>

Upvotes: 0

user94893
user94893

Reputation:

I found some possibility. let's see my source code.

C# - Globalization project

namespace [SolutionName].Globalization.Shared
{
    public class Site : System.Web.Mvc.ViewMasterPage
    {
        public static [PropertyName] { get;set; }
    }
}

I think, you know what will happened next!

C# - Asp.net MVC View Page in same View Section

<%@ Master Language="C#" Inherits="[SolutionName].Globalization.Shared.Site" %>
<%@ Import Namespace="[SolutionName].Globalization.Shared" %>

<%= [PropertyName] %>

C# - Asp.net MVC View Page in same View Section

<%@ Import Namespace="[SolutionName].Globalization.Shared" %>

<%= Site.[PropertyName] %>

C# - Asp.net MVC View Page in different View Section

<%@ Import Namespace="[SolutionName].Globalization" %>

<%= Shared.Site.[PropertyName] %>

Right? I think it's very beatiful source code. Do you have any suggestion? Please tell me.

PS.1 The only thing that I worry is my Globalization project. It's quite strange because almost classes in this project inherit from View Page class in Asp.net MVC project.

PS.2 Don't worry about creating above code. Because I will use VS Macro for generating & modifying all of them.

Thanks,

Upvotes: 1

CraigTP
CraigTP

Reputation: 44939

There is no direct equivalent to a VB.NET module in C#, however, for most uses, I've found using a public static class (in your most "top-level" namespace) to suffice.
(You'll still have to qualify members and methods with the class name, though).

Something like:

namespace MyApplication
{
   public static class Globals
   {
      public const string app_name = "This is my application name!";
   }
}

and using it like this:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Globals.app_name;                        
        }
    }
}

For globally available constant values (such as application name, version number etc.) it usually works quite well.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502696

No, C# doesn't have any equivalent to VB.NET modules.

(In VB.NET, access will be fully qualified by the compiler.)

You might be interested in this blog post by Eric Lippert though, talking about the possibility of including "top level" methods in a future release.

Upvotes: 2

Related Questions