I'm playing around wuth C# at the moment and I'm having problems converting some code. The code in VB is:
Public Class DBParameters
Public ParmName As String
Public ParmType As SqlDbType
Public ParmSize As Integer
Public ParmValue
Public Sub New(ByVal ParmName As String, ByVal ParmType As SqlDbType, ByVal ParmSize As Integer, ByVal ParmValue As String)
MyBase.New()
Me.ParmName = ParmName
Me.ParmType = ParmType
Me.ParmSize = ParmSize
Me.ParmValue = ParmValue
End Sub
End Class
This allows me to create a new object of type DBParameters by using:
" new DBParameters("this", sqltype, 5, "foo") etc.... "
However, I can;t find the right way to get it to work under C#
I'm oretty sure it's a problem with the "New" sub.
The code I have in C# for it so far is:
Like this, I get the following error "Too many arguments to 'Public Sub New()'" and I can't a working equivalent to MyBase.New() in C# I have tried
using base.new() but that causes a compilatin error. What am I doing wrong here? Cheers Mark
Hi, The C# syntax and for the class constructors differ somewhat to those of VB.NET. Constructors in C# have the same name as the class rather than the name New. MyBase is refered to as base in C#, and calling the base class constructor has a different syntax.
Also in C# the equivilant of Me is this. Also keep in mind that C# is a case sensitive language. C# also provides some convenient aliases for many of the primitive datatypes, for example 'int' and 'string' rather than 'Int32' and 'String' however either will
work it is just a matter of convenience and provides C like feel for those of use moving from C/C++.
public class DBParameters
{
public string ParmName;
public SqlDbType ParmType;
public int ParmSize;
public string ParmValue;
public DBParameters( string ParmName, SqlDbType ParmType, int ParmSize, string ParmValue ) :
base()
{
this.ParmName = ParmName;
this.ParmType = ParmType;
this.ParmSize = ParmSize;
this.ParmValue = ParmValue;
}
}
Glosh
Member
445 Points
89 Posts
converting from VB to C#
Oct 30, 2003 02:45 PM|LINK
Public Class DBParameters Public ParmName As String Public ParmType As SqlDbType Public ParmSize As Integer Public ParmValue Public Sub New(ByVal ParmName As String, ByVal ParmType As SqlDbType, ByVal ParmSize As Integer, ByVal ParmValue As String) MyBase.New() Me.ParmName = ParmName Me.ParmType = ParmType Me.ParmSize = ParmSize Me.ParmValue = ParmValue End Sub End ClassThis allows me to create a new object of type DBParameters by using: " new DBParameters("this", sqltype, 5, "foo") etc.... " However, I can;t find the right way to get it to work under C# I'm oretty sure it's a problem with the "New" sub. The code I have in C# for it so far is:public void New(string ParameterName, SqlDbType ParameterType ,int ParameterSize, object ParameterValue, ParameterDirection ParameterDirection){ this.ParameterName = (string)ParameterName; this.ParameterType = (SqlDbType)ParameterType; this.ParameterSize = (int)ParameterSize; this.ParameterValue = (object)ParameterValue; this.ParameterDirection = (ParameterDirection)ParameterDirection; }Like this, I get the following error "Too many arguments to 'Public Sub New()'" and I can't a working equivalent to MyBase.New() in C# I have tried using base.new() but that causes a compilatin error. What am I doing wrong here? Cheers Marktaylorza
Member
325 Points
65 Posts
Re: converting from VB to C#
Oct 31, 2003 12:48 AM|LINK
public class DBParameters { public string ParmName; public SqlDbType ParmType; public int ParmSize; public string ParmValue; public DBParameters( string ParmName, SqlDbType ParmType, int ParmSize, string ParmValue ) : base() { this.ParmName = ParmName; this.ParmType = ParmType; this.ParmSize = ParmSize; this.ParmValue = ParmValue; } }Welcome to C# :) Hope this helpsMy Blog
Glosh
Member
445 Points
89 Posts
Re: converting from VB to C#
Nov 03, 2003 08:39 AM|LINK