Best practices when converting from vb6 to C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    Best practices when converting from vb6 to C#

    Hello everybody,

    I've recently switched from using VB6 to C#.

    To be honest, I'm not sure if I shouldn't go back because it seems more complicated and less quick then VB6. But I want to give it a fair chance.

    Up until now, I've been programming software for controlling a specific graphics engine, using it's API.

    In VB6, I would have a module where I would put all of my global variables and where I would initialize them. See the example below which only has a few statements:
    Code:
    global Engine as new xpEngine
    global MyScene1 as xpScene
    global MyScene2 as xpScene
    
    global Obj as xpBaseObject
    global TxtObj as xpTextObject
    
    public sub InitApplication
    engine.getSceneByName "Scene1", MyScene1
    engine.getSceneByName "Scene2", MyScene2
    end sub
    Now in VB6 these variables could be used throught out the different forms and modules.

    So in my Main form I could use the following syntax pretty much everywhere.
    Code:
    MyScene1.getObjectByName "Textfield1", TxtObj
    TxtObj.Text = "Text to be put in the first textfield"
    
    MyScene1.getObjectByName "Textfield2", TxtObj
    TxtObj.Text = "Text to be put in the second textfield"
    Now when I would like to translate this into C#,
    I can't declare anything as a global variable anymore.
    But does this mean that I have to link every Scene in my application to the variable MyScene1 everytime I want to use it?

    Also in VB6 when I started a new project, I just had to import my module in my project to start programming. It was very useful. It appears that in C# I will have to write time and time again?

    Am I missing something? Or is there a way to make it easier for me?

    Any help is appreciated.

    Cheers,
    Kenneth
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    In C# public is the equivalent of global. It is the same if VB6. Global was only for backwards compatibility with previous versions of VB. In VB6 you are supposed to use public instead of global.

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      There is a paradigm leap that you have to make when moving from VB6 to C#. It is not easy and we could have lengthy discussions on the various merits and drawbacks of the different paradigms involved in each system, but the important thing is that you do need to make an adjustment to the way you think.

      With all due respect to Rabbit (and please, correct me if I am wrong), I believe his statements are partially correct. Global is not quite the same as public. It would be more accurate to state that global is like public static. Global in VB6 does not require instantiation. Declaring something "public" in C# does. If you declare something "public static" it does not.

      I believe that the code you provided would look something like this in C#:

      Code:
      public class Whatever
      {
        public static xpEngine Engine;
        public static xpScene MyScene1;
        public static xpScene MyScene2;
        
        public static xpBaseObject Obj;
        public static xpTextObject TxtObj;
       
        public void InitApplication()
        {
          engine.getSceneByName("Scene1", Whatever.MyScene1);
          engine.getSceneByName("Scene2", Whatever.MyScene2);
        }
      }
      These fields could then be used throughout your project (and any projects referencing your project) as follows:

      Code:
          Whatever.TxtObj.Text = "Text to be put in the first textfield"
      The details of the library are not known to me so it is hard for me to provide more detailed information, but basically you need to qualify each reference to any of the public static fields with the class name.

      Also, to answer your second question: you do not have to rewrite the same module over and over again. You simply add your previous project as a reference by going to Project > Add Reference... (in VS 2008 at least) and pointing to your compiled assembly. Generally you use a "Class Library" type of project for this situation. Any class declared as public in your class library will be available for use in other projects but classes default to private so you will have to explicitly state that a class is public if you wish it to be exposed.

      Hope this helps!

      Comment

      • Cainnech
        New Member
        • Nov 2007
        • 132

        #4
        Thanks Joseph for your excellent explanation!

        I thought I read somewhere that Public Static statements were considered as not proper programming but as I read from your post that's not the case.

        I'll give it a try but most likely I'll be back with more questions as it is indeed a paradigm leap :-)

        Thanks,
        Kenneth

        Comment

        • Joseph Martell
          Recognized Expert New Member
          • Jan 2010
          • 198

          #5
          No problem Kenneth! Keep the questions coming!

          The idea that public static is not considered "proper" is a whole other discussion that gets into the merits/drawbacks of global state. That is best saved for another post :)

          Comment

          • Mikkeee
            New Member
            • Feb 2013
            • 94

            #6
            Cainnech, I too came from the VB world and also had the same concerns. I felt that there was nothing I couldn't accomplish in VB but man was I wrong. I have to occasionally jump in to some VB6 projects for a company I consult for and I feel like I'm coding with my hands tied behind my back. Just ridiculously easy tasks in the .net world require way more work in the VB6 world (xml parsing, web service interaction, database interaction, etc). You will be way more productive than you ever thought possible once you get past the learning curve.

            Comment

            • Cainnech
              New Member
              • Nov 2007
              • 132

              #7
              Hi Mikkeee,

              Thanks for the encouraging words. However I'm under the impression that there are some features which are missing in C#.

              Just to give an example, if you use labels to select a value.
              Code:
              for i=0 to lblLabelName.ubound
               lblLabelName(i).backcolor = vbgrey
              next
              
              lblLabelName(Index).backcolor = vbgreen
              This would be the code if you click on a label to be able to select it. It would first reset all of the labels and then set the background of the selected label to green.

              I haven't found a way yet to do the same in C#.

              So I agree that C# is probably much more powerful but VB6 was much easier to control your objects.

              But as Joseph already mentioned, you really have to change the way you think but it's going to take some time for me.

              Greets,
              Kenneth

              Comment

              • Mikkeee
                New Member
                • Feb 2013
                • 94

                #8
                Kenneth,

                Change the way you think and it will come. I really liked indexed controls in VB but found that you really don't need them. In c# you just need to assign the same click event to all the labels and you're good to go. This will accomplish exactly what you want.

                Code:
                foreach (Object objControl in this.Controls)
                {
                    if (objControl.GetType() == typeof(Label)) 
                        ((Label)objControl).BackColor = Color.Gray;
                }
                ((Label)sender).BackColor = Color.Green;

                Comment

                • Cainnech
                  New Member
                  • Nov 2007
                  • 132

                  #9
                  Hello guys, I'm having another issue in this conversion.

                  In my application, I'm using a library to perform some jobs.

                  In one of the calls I need to create a new object but it want a new object. However I can't seem to get it done. So I'm hoping that someone here can help me out.

                  This is what I need to do:

                  Code:
                  Scene.CreateObject(string ObjType, out xpBaseObject NewObject)
                  So Scene.CreateObj ect is a part of the library I'm working with. The same applies for xpBaseObject.

                  So when I use this method I'm doing:
                  Code:
                  Scene.CreateObject("Quad", out obj);
                  obj is the name of my xpBaseObject.

                  But when I try to run the program I get a NullReferenceEx ception but I can't seem to find the correct solution.

                  Comment

                  • Cainnech
                    New Member
                    • Nov 2007
                    • 132

                    #10
                    Oh, got it guys.

                    The problem was actually when I defined the xpBaseObject. I had to make it into a public static.

                    Comment

                    Working...