Results 1 to 9 of 9

Thread: Visual Basic 6.0 to to C#

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    Visual Basic 6.0 to to C#

    Hi;

    I'm already using Visual Basic 6.0. Furthermore i would like to learn C# but it's look like hard for now. Where should I start or or could you give me samples.

    For example I would like to add two textbox value by using function in C#.

    In Visual basic

    I'm using global function in Bas module like below
    Code:
    Public Result As Double
    Public Function Add(Val1 As Double, Val2 As Double)
    Result = Val1 + Val2
    End Function
    and in Form1
    Code:
    Private Sub Command1_Click()
    Dim a As Double
    Dim b As Double
    a = Val(Text1)
    b = Val(Text2)
    Call Add(a, b)
    MsgBox Result
    End Sub
    Thank you

    Levent
    Last edited by levent; Nov 21st, 2009 at 12:58 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Visual Basic 6.0 to to C#

    C# does not support bas modules so your best bet is a class. Here is a simple exmple.
    Code:
    using System;
    
    namespace MathFunctions
    {
    	/// <summary>
    	/// Summary description for clsMathFunctions.
    	/// </summary>
    	public class clsMathFunctions
    	{
    		public clsMathFunctions()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    
    		public double Addition(double Val1, double Val2)
    		{
    
    			return (Val1 + Val2);
    		}
    	}
    }
    Now in your form you would place a using MathFunctions at the top of your page.
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using MathFunctions;
    And then in the click event of the button
    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    	clsMathFunctions objMath = new clsMathFunctions();
    	double dblRetrun, dblVal1, dblVal2;
    
    	dblVal1 = Convert.ToDouble(textBox1.Text);
    	dblVal2 = Convert.ToDouble(textBox2.Text);
    
    	dblRetrun = objMath.Addition(dblVal1, dblVal2);
    
    	MessageBox.Show(textBox1.Text + " + " + 
    		textBox2.Text + " = " + dblRetrun.ToString());
    
    	objMath = null;
    }
    Last edited by MarkT; Nov 19th, 2009 at 08:51 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    Re: Visual Basic 6.0 to to C#

    Thank you.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Visual Basic 6.0 to to C#

    The C# equivalent of a module is a static class. If you declare your class static and all its members static then you won't have to create an instance of the class. You can just refer to the method qualified with the class name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    Re: Visual Basic 6.0 to to C#

    So, we are using Class instead Module and we can use this functions in every form

    Like below, right?

    Code:
    using MathFunctions;
    Best regards
    Last edited by levent; Nov 20th, 2009 at 10:39 AM.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Visual Basic 6.0 to to C#

    If the class is stored in another dll or you are using another namespace for the class then you can use the using keyword. If not then you can use it like it is, eg. class1.AddNumebers("1","2").
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Visual Basic 6.0 to to C#

    The syntax "using Foo;" is strictly for namespaces, not classes.
    You can use "using Foo = SomeClass;", but this just allows you to replace the classname "SomeClass" with "Foo" (this is called 'aliasing' the class name).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Visual Basic 6.0 to to C#

    Quote Originally Posted by David Anton View Post
    The syntax "using Foo;" is strictly for namespaces, not classes.
    You can use "using Foo = SomeClass;", but this just allows you to replace the classname "SomeClass" with "Foo" (this is called 'aliasing' the class name).
    I'm sure David already knows this but, just to clarify, you can import a type in VB with an Imports statement, although I would recommend against it, but, as David said, in C# a 'using' statment can only import a namespace, not a type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Visual Basic 6.0 to to C#

    The thing is C# is actually pretty simple once you sit down and learn the different constructs, anyone going from one language to another should have little issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width