将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.
最满意答案
您可以将常量放在
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 ...
-
我可以用什么工具将C#3转换为VB.Net 9 .Net 3.5?(What tool can I use to convert C# 3 to VB.Net 9 .Net 3.5? [closed])[2022-03-13]
从; 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 ... -
从VB.NET转换为C#(Convert from VB.NET to C#)[2022-02-26]
你这样做是这样的: 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 \ ...
-
VB转C#或VB.net转换(VB to C# or VB.net conversion)[2022-08-20]
你可以看看这篇文章,值得一读: 将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 ...
-
VB.Net模块行为(VB.Net module behavior)[2022-06-25]
Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ... -
将VB.net模块转换为C#(convert VB.net Module to C#)[2023-03-05]
您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...