首页 \ 问答 \ 将VB.net模块转换为C#(convert VB.net Module to C#)

将VB.net模块转换为C#(convert VB.net Module to C#)

我决定在使用VB.net后第一次尝试使用C#。

出于好奇,当我使用VB.net时,我有:

Dim conn As OleDbConnection = New OleDbConnection("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

在尝试将此格式转换为C#时,我执行了以下操作:

string strAccessConn = "Provider=****;user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd

但是,我的strUserId,strDatabase和strPssWd保存在我的Module.vb中,用于VB.net,如下所示:

Module Module1
  Friend strDatabase As String = "****"
  Friend strUserID As String = "****"
  Friend strPssWd As String = "****"

End Module

如何在C#中创建模块(一个例子会有所帮助)谢谢!

仅供参考:我被告知C#等效VB'模块'是重复的。

然而,他们的帖子的格式和过程并不等同于我的。 我问一个基于数据库连接的模块。


I decided to try out C# for the first time after using VB.net.

Out of curiousity, when I used VB.net I had :

Dim conn As OleDbConnection = New OleDbConnection("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

When trying to convert this format to C# I did the following:

string strAccessConn = "Provider=****;user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd

However, my strUserId, strDatabase and strPssWd where saved in my Module.vb for VB.net like so:

Module Module1
  Friend strDatabase As String = "****"
  Friend strUserID As String = "****"
  Friend strPssWd As String = "****"

End Module

How do I make the Module in C# (an example would be helpful) Thanks!

FYI: I was told C# Equivalent for VB 'module' was a duplicate.

However the formatting and process of their post isn't equivalent to mine. I am asking a module based on database connection.

更新时间:2023-03-05 18:03

最满意答案

您可以将常量放在public static class如下所示:

public static class MyConnectionStringConstants
{
    public const string strDatabase = "****";
    public const string strUserID = "****";
    public const string strPssWd = "****";
}

要使用它,您需要像这样引用常量:

string strAccessConn = "Provider=****;user id=" + MyConnectionStringConstants.strUserID + ";data source=" + MyConnectionStringConstants.strDatabase + ";password=" + MyConnectionStringConstants.strPssWd

顺便说一句,在C#中,你使用+运算符连接字符串,而不是&运算符。


You can put constants in a public static class like this:

public static class MyConnectionStringConstants
{
    public const string strDatabase = "****";
    public const string strUserID = "****";
    public const string strPssWd = "****";
}

To use it, you will need to refer to the constants like this:

string strAccessConn = "Provider=****;user id=" + MyConnectionStringConstants.strUserID + ";data source=" + MyConnectionStringConstants.strDatabase + ";password=" + MyConnectionStringConstants.strPssWd

BTW, in C#, you concatenate strings using the + operator, not the & operator.

相关问答

更多
  • 你可以使用Lutz Roeders Reflector( http://www.red-gate.com/products/reflector ),它可以将整个装配反编译成Visual Studio项目。 这样,您可以将ANY .NET Langauge转换为该程序支持的语言之一(C#.VB.NET,MC ++,Delphi,Chrome) You can use Lutz Roeders Reflector (http://www.red-gate.com/products/reflector) whic ...
  • 从; Scott Hanselman的博客 Telerik Code Converter - 将C#转换为VB和VB转换为C#的网站。 CarlosAg的CodeTranslator - 第一个,也是许多人说的最好的。 一个AJAXy代码转换器,可以用于C#和VB.NET。 DeveloperFusion Code Converter - 这个在线实用程序还将在C#和VB之间转换.NET 3.5语法和LINQ。 From ; Scott Hanselman's Blog Telerik Code Conve ...
  • 你这样做是这样的: private double[] _PatchSpectrum = new double[49] public double[] GetPatchSpectrum { get { return _PatchSpectrum; } } public double this[int index] { set { this._PatchSpectrum[index] = value; } } You'd do this like: private double[] _Pa ...
  • 像这样的东西应该工作: private clsImageMapCalculations myImageMapCalculations = new clsImageMapCalculations((Exception ex) => UnhandledExceptionHandler()); 可以通过转换工具创建问题的部分是: Sub(ex As Exception) UnhandledExceptionHandler() 可以在C#中转换为相应的lambda表达式: (Exception ex) => ...
  • 除了书籍/博客,学习C#/ VB墙的另一面的一个好方法是用C#编写一些代码,编译它,然后在Reflector中打开DLL并将其视为VB代码。 这将允许你回答你自己关于VB.NET的问题。 例如,假设你想看看如何在VB.NET中使用泛型。 你用C#编写一个简单的库: void SomeMethod() { List list = new List(); } 编译这个,然后在Reflector中打开,它会显示你: Sub SomeMethod Dim list as Lis ...
  • 你可以这样做: string AllData = @"I'm a multi-line string"; string[] rows = AllData.Split('\r', '\n'); 或者像这样: string[] rows2 = AllData.Split( System.Environment.NewLine.ToCharArray() ); 问题是,你真的想分开分开吗? 将它们拆分在一起也是一个不好的主意。 因为Unix / Linux / Mac(通常是POSIX)不使用\ r \ ...
  • 你可以看看这篇文章,值得一读: 将VB6转换为VB.NET,第一部分 我的建议是翻译用户实际使用的部分,并根据他/她的要求添加新功能。 You could to take a look into this article, it's worth reading as a warm up: Converting VB6 to VB.NET, Part I My advice is to translate just pieces your user actually uses, adding new feat ...
  • 编程语言做出了不同的权衡。 VB.NET与Strict = Off是非常宽松的。 这是出于历史兼容性原因。 几十年前,编程语言设计者认为这种行为对初学者很有帮助。 现在我们知道这种松懈的行为对于程序正确性和开发速度来说是可怕的。 JavaScript遇到了同样的问题,但更糟糕。 使用严格的语义。 Programming languages make different trade-offs. VB.NET with Strict=Off is very lax. This is for historic c ...
  • Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ...
  • 您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)