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
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;
}
Re: Visual Basic 6.0 to to C#
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.
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
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").
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).
Re: Visual Basic 6.0 to to C#
Quote:
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.
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.