Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. converting vb.net code to c#

converting vb.net code to c#

Scheduled Pinned Locked Moved C#
csharptoolshelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vijju04
    wrote on last edited by
    #1

    Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju

    H L 3 Replies Last reply
    0
    • V vijju04

      Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You've already asked this question not more than an hour ago - be patient. See my reply, because you seem to be missing a key feature of the .NET Framework - the fact that it doesn't matter in what language the source was written because the assemblies all contain IL (unless they're mixed mode, which currently only MC++ supports (from the Microsoft camp)).

      Microsoft MVP, Visual C# My Articles

      V 1 Reply Last reply
      0
      • V vijju04

        Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Besides, do you maybe want to put this in context? What are you trying to do? Extract a value from a collection? If all you did was convert from one language to another, you're constrained by what the rest of the code did. Now, if you convert the concept, you could use many types to do this. For example, you could use a Hashtable or StringDictionary (or several other things in the BCL) to store string key/value pairs and refer to them by name:

        StringDictionary dict = new StringDictionary();
        dict["One"] = "Apple";
        dict["Two"] = "Orange";
        dict["Three"] = "Banana";
        Console.WriteLine(dict["Two"]); // Prints Orange to the console.

        When moving from one application framework to another, don't just convert your code. Advancements are made with newer frameworks. If all you're doing is porting your code just to run on the .NET Framework, you're doing a very stupid thing (any article by a decent author will tell you as much) - if it works, don't change it. If you have to change it to add new functionality later and to be maintainable by less-than-knowledge programmers, then change it in steps.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        • V vijju04

          Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          My VB is a bit rusty but I think this is your answer.

          private string GetSingleProperty(string elementName)
          {
          // g_pPropSet must be defined to the entire class
          // GetProperty must be a defined method within this object

          object Values = g_pPropSet.GetProperty(elementName);
          if (Values == null)
          return String.Empty;
          if (Values.GetType() != typeof(String))
          return String.Empty;
          return (string)Values;
          }

          H 1 Reply Last reply
          0
          • H Heath Stewart

            You've already asked this question not more than an hour ago - be patient. See my reply, because you seem to be missing a key feature of the .NET Framework - the fact that it doesn't matter in what language the source was written because the assemblies all contain IL (unless they're mixed mode, which currently only MC++ supports (from the Microsoft camp)).

            Microsoft MVP, Visual C# My Articles

            V Offline
            V Offline
            vijju04
            wrote on last edited by
            #5

            Hi Heath, thanks for replying. actually i am not very good at vb, but i am a little comfortable programming with c#. that is one reason i wanted to convert that code into c#. also, i wanted to know more about coupling and de-coupling, if i want to run my .dll project (which is going to be plug-in) as an executable at the same time. if you suggest any examples or material to follow-up the understand the architecture design, that will be GREAT. and also i didn't knew that my question was posted more than one time, and sorry about that.. Thanks vijju

            H 1 Reply Last reply
            0
            • L LongRange Shooter

              My VB is a bit rusty but I think this is your answer.

              private string GetSingleProperty(string elementName)
              {
              // g_pPropSet must be defined to the entire class
              // GetProperty must be a defined method within this object

              object Values = g_pPropSet.GetProperty(elementName);
              if (Values == null)
              return String.Empty;
              if (Values.GetType() != typeof(String))
              return String.Empty;
              return (string)Values;
              }

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              That would still require the Microsoft.VisualBasic.Compatibility assembly, since whatever Type g_pPropSet seems to be from there (since everything else is defined in the BCL assemblies). He doesn't want to reference that assembly, even though it really doesn't matter.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              • V vijju04

                Hi Heath, thanks for replying. actually i am not very good at vb, but i am a little comfortable programming with c#. that is one reason i wanted to convert that code into c#. also, i wanted to know more about coupling and de-coupling, if i want to run my .dll project (which is going to be plug-in) as an executable at the same time. if you suggest any examples or material to follow-up the understand the architecture design, that will be GREAT. and also i didn't knew that my question was posted more than one time, and sorry about that.. Thanks vijju

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                Coupling and decoupling has nothing to do with how code is executed - it has to do with how code is referenced. Knowing the C# language doesn't mean you know .NET. If you want to learn more, I suggest you read the SDK, which should be installed on your machine (it is installed by default with VS.NET, but can be installed separately) and available online at http://msdn.microsoft.com/library/en-us/dnanchor/html/netfxanchor.asp[^]. Understanding at least the basic concepts of the CLR (like what assemblies are) is important. Since you didn't tell me what g_pPropSet is defined as, I can only assume it's defined in the Microsoft.VisualBasic.Compatibility assembly. As I mentioned before - converting the code isn't a good idea, especially if you're new to .NET; you'll never learn anything that way. So, just converting the code to C# won't get rid of the assembly dependency - you'll have to rewrite it to use a similar class. Judging by what you seem to be trying to do, a suitable class might be the StringDictionary as I mentioned in my other reply. If you want to know more about architecting .NET solutions, visit http://msdn.microsoft.com/patterns[^].

                Microsoft MVP, Visual C# My Articles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups