views:

507

answers:

6

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,

+2  A: 

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.

Jon Skeet
Please look at my answer.
Soul_Master
A: 

VB.NET Module is a class having static members.

adatapost
A: 

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(); %>
awe
Not equivalent. Because I must specify class name before call its property of field.
Soul_Master
I just try it. But I need to use this keyword for call your method. Moreover, This idea can't be use in another context because it's extention method.
Soul_Master
Fair enough. Your sollution with creating a new site class that inherits the System.Web.Mvc.ViewMasterPage class is better anyway. As a rule, you should use this approach when you can. The extentions apporach should only be used when you can't control which class is used.
awe
A: 

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.

CraigTP
+1  A: 

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,

Soul_Master
I don't think it's terribly elegant, because you're abusing inheritance IMO. I think it would be much nicer to just explicitly specify the name of a class, e.g. "SiteSettings.WebSiteName".
Jon Skeet
I know. But in Asp.net MVC project, short resource name is very useful. Because it can reduce overall source code in view page. Moreover, this source code will be generated automatically after developer save resource file (*.resx). So, it isn't problem about abusing/hacking. On the other hand, I don't understand why c# doesn't allow me to use Module syntax. It likes converting vb.net syntax to c# syntax more than reference directly. Thanks.
Soul_Master
A: 

vb.net modules there fields are implicitly static ,all u need to do is to create a class with static members in c#.

solomonope
Please reread my question.
Soul_Master