|
-
Nov 19th, 2009, 07:58 AM
#1
Thread Starter
Addicted Member
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.
-
Nov 19th, 2009, 08:44 AM
#2
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.
-
Nov 19th, 2009, 01:52 PM
#3
Thread Starter
Addicted Member
Re: Visual Basic 6.0 to to C#
-
Nov 19th, 2009, 04:47 PM
#4
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.
-
Nov 20th, 2009, 10:34 AM
#5
Thread Starter
Addicted Member
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.
-
Nov 20th, 2009, 10:05 PM
#6
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").
-
Nov 21st, 2009, 11:21 AM
#7
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).
-
Nov 21st, 2009, 11:33 AM
#8
Re: Visual Basic 6.0 to to C#
 Originally Posted by David Anton
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.
-
Nov 21st, 2009, 04:21 PM
#9
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|