-1

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.

Community
  • 1
  • 1
narue1992
  • 1,143
  • 1
  • 14
  • 40
  • 1
    Duplicated here: http://stackoverflow.com/questions/30870487/c-sharp-equivalent-for-vb-module – Fruitbat Jul 08 '15 at 17:42
  • @Fruitbat I looked at your duplicate link and it doesn't look like what I have. May be asking the same question but results are not what I need. Sstan's answer was what I need based on my format. So if you downgraded my post then I don't know why. – narue1992 Jul 08 '15 at 18:11
  • Not me. I just put the link there because I thought it was what you needed. – Fruitbat Jul 08 '15 at 18:28
  • @Fruitbat I see. I just got a "duplicate" alert at top with same link so that is why I asked. Thanks. – narue1992 Jul 08 '15 at 18:45

1 Answers1

4

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.

sstan
  • 35,425
  • 6
  • 48
  • 66
  • Thank you for clarification and example. The format of what you sent is exactly similar to my old version. I really appreciate it! I also wasn't fully aware yet that c# uses + so appreciate that information. – narue1992 Jul 08 '15 at 18:12