473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Best practices when converting from vb6 to C#

132 100+
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:
Expand|Select|Wrap|Line Numbers
  1. global Engine as new xpEngine
  2. global MyScene1 as xpScene
  3. global MyScene2 as xpScene
  4.  
  5. global Obj as xpBaseObject
  6. global TxtObj as xpTextObject
  7.  
  8. public sub InitApplication
  9. engine.getSceneByName "Scene1", MyScene1
  10. engine.getSceneByName "Scene2", MyScene2
  11. end sub
  12.  
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.
Expand|Select|Wrap|Line Numbers
  1. MyScene1.getObjectByName "Textfield1", TxtObj
  2. TxtObj.Text = "Text to be put in the first textfield"
  3.  
  4. MyScene1.getObjectByName "Textfield2", TxtObj
  5. TxtObj.Text = "Text to be put in the second textfield"
  6.  
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
Feb 6 '13 #1

✓ answered by Joseph Martell

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#:

Expand|Select|Wrap|Line Numbers
  1. public class Whatever
  2. {
  3.   public static xpEngine Engine;
  4.   public static xpScene MyScene1;
  5.   public static xpScene MyScene2;
  6.  
  7.   public static xpBaseObject Obj;
  8.   public static xpTextObject TxtObj;
  9.  
  10.   public void InitApplication()
  11.   {
  12.     engine.getSceneByName("Scene1", Whatever.MyScene1);
  13.     engine.getSceneByName("Scene2", Whatever.MyScene2);
  14.   }
  15. }
  16.  
These fields could then be used throughout your project (and any projects referencing your project) as follows:

Expand|Select|Wrap|Line Numbers
  1.     Whatever.TxtObj.Text = "Text to be put in the first textfield" 
  2.  
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!

9 2115
Rabbit
12,516 Expert Mod 8TB
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.
Feb 6 '13 #2
Joseph Martell
198 Expert 128KB
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#:

Expand|Select|Wrap|Line Numbers
  1. public class Whatever
  2. {
  3.   public static xpEngine Engine;
  4.   public static xpScene MyScene1;
  5.   public static xpScene MyScene2;
  6.  
  7.   public static xpBaseObject Obj;
  8.   public static xpTextObject TxtObj;
  9.  
  10.   public void InitApplication()
  11.   {
  12.     engine.getSceneByName("Scene1", Whatever.MyScene1);
  13.     engine.getSceneByName("Scene2", Whatever.MyScene2);
  14.   }
  15. }
  16.  
These fields could then be used throughout your project (and any projects referencing your project) as follows:

Expand|Select|Wrap|Line Numbers
  1.     Whatever.TxtObj.Text = "Text to be put in the first textfield" 
  2.  
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!
Feb 7 '13 #3
Cainnech
132 100+
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
Feb 7 '13 #4
Joseph Martell
198 Expert 128KB
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 :)
Feb 8 '13 #5
Mikkeee
94 64KB
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.
Feb 9 '13 #6
Cainnech
132 100+
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.
Expand|Select|Wrap|Line Numbers
  1. for i=0 to lblLabelName.ubound
  2.  lblLabelName(i).backcolor = vbgrey
  3. next
  4.  
  5. lblLabelName(Index).backcolor = vbgreen
  6.  
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
Feb 9 '13 #7
Mikkeee
94 64KB
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.

Expand|Select|Wrap|Line Numbers
  1. foreach (Object objControl in this.Controls)
  2. {
  3.     if (objControl.GetType() == typeof(Label)) 
  4.         ((Label)objControl).BackColor = Color.Gray;
  5. }
  6. ((Label)sender).BackColor = Color.Green;
  7.  
Feb 9 '13 #8
Cainnech
132 100+
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:

Expand|Select|Wrap|Line Numbers
  1. Scene.CreateObject(string ObjType, out xpBaseObject NewObject)
  2.  
So Scene.CreateObject is a part of the library I'm working with. The same applies for xpBaseObject.

So when I use this method I'm doing:
Expand|Select|Wrap|Line Numbers
  1. Scene.CreateObject("Quad", out obj);
  2.  
obj is the name of my xpBaseObject.

But when I try to run the program I get a NullReferenceException but I can't seem to find the correct solution.
Mar 12 '13 #9
Cainnech
132 100+
Oh, got it guys.

The problem was actually when I defined the xpBaseObject. I had to make it into a public static.
Mar 12 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Mike D | last post by:
What are the best practices when using a db and include files? I typically store my connection string in an include file. I then open my db do what I need to and close the connection. I haven't...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
3
by: newtophp2000 | last post by:
I have several forms that display information from the database after users log in. I would like the column titles to be sortable so that when the user clicks on a column heading, the data...
1
by: AJ | last post by:
Hi all, I am wondering if their are any links out their dealing with best practices when it comes to enterprise web site organization. We are still maintaining classic ASP applications so...
1
by: J Rice | last post by:
I have a question: When should syslog.closelog() be called? I have a daemon that spends most of its time asleep and quiet, but writes messages to the mail log when active. Should I open the...
2
by: blueskies | last post by:
Can anyone using both ASP.NET and MM Contribute provide some pointers or links to best practices when implementing a website with content provided by end users? Thanks for any suggestions or...
8
by: Flavio | last post by:
Hi, Nowadays the addition of functionality to programs by means of plugins is very frequent. I want to know the opinions of experienced Python developers about the best practices when it...
2
by: nttfitb149 | last post by:
Can you recommend the best practices when using Ajax
0
by: Vince Filby | last post by:
Should WriteXml include the tags that encompass the object? Take a Phone class for example, should WriteXml() emit this: <Phone> <Description>text</Description> <Number>1-222-333-1234</Number>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.