Link to home
Create AccountLog in
Avatar of janjuama84
janjuama84

asked on

Convertion from VB to C#

Can somebody please convert following code to C#?


''' <summary>
''' This page allows for classes to have seats deducted from it
''' </summary>
''' <remarks></remarks>
Partial Class _Default
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' if first load then setup classes
        If Not Page.IsPostBack Then

            InitialiseClasses()
            UpdateLabels()

        End If
    End Sub

    ''' <summary>
    ''' Initialises the Class Arrays based on parameters passed
    ''' </summary>
    ''' <param name="ClassSizes">The Size of the Classes</param>
    ''' <param name="AmountofClasses">The Amount of Classes to Create</param>
    ''' <remarks></remarks>
    Private Sub InitialiseClasses(Optional ByVal ClassSizes As Integer = 15, Optional ByVal AmountofClasses As Integer = 3)
        Dim TheClasses As New ArrayList()
        Dim i As Integer

        ' For the Amount of Classes Specified
        For i = 1 To AmountofClasses
            ' Add an integer of size ClassSizes to the ArrayList
            TheClasses.Add(ClassSizes)
        Next

        ' Add the Classes Arraylist to the Session
        Session("Classes") = TheClasses

        ' Create an Arraylist of the size of ClassSizes and bind it to the Radio List
        radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes)
        radSeatsAvailable.DataBind()
    End Sub



    ''' <summary>
    ''' Creates and returns an array list of 1 to the specified size
    ''' </summary>
    ''' <param name="ArrayListLength">The Size of the ArrayList</param>
    ''' <returns>ArrayList of integers from 1 to ArrayListLength</returns>
    ''' <remarks></remarks>
    Private Function PopulateArrayList(ByVal ArrayListLength As Integer) As ArrayList
        Dim myarray As New ArrayList
        Dim i As Integer

        ' For the size of the arraylist
        For i = 1 To ArrayListLength
            ' Add an integer of the value i
            myarray.Add(i)
        Next

        Return myarray

    End Function



    ''' <summary>
    ''' Resets the radiolist when the dropdownlist with classes is changed
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlClass_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlClass.SelectedIndexChanged

        ' ReBind the radiolist based on the amount of seats left in the class
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item(ddlClass.SelectedIndex).ToString)
        radSeatsAvailable.DataBind()

        UpdateLabels()

    End Sub

    ''' <summary>
    ''' On Click update the amount of seats left in the class
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click

        ' Deduct the amount of seats based on the radio button selected
        Session("classes").Item(ddlClass.SelectedIndex) = Session("classes").Item(ddlClass.SelectedIndex) - radSeatsAvailable.SelectedIndex - 1

        ' Bind the new amount of seats left
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item(ddlClass.SelectedIndex).ToString)
        radSeatsAvailable.DataBind()

        UpdateLabels()

    End Sub


    ''' <summary>
    ''' Updates the labels for user feedback
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateLabels()
        lblClass1.Text = Session("classes").Item(0).ToString
        lblClass2.Text = Session("classes").Item(1).ToString
        lblClass3.Text = Session("classes").Item(2).ToString
    End Sub

End Class
ASKER CERTIFIED SOLUTION
Avatar of kprestage
kprestage

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of kprestage
kprestage

Avatar of janjuama84

ASKER

what to do with

        // Warning!!! Optional parameters not supported
        // Warning!!! Optional parameters not supported

how to convert those?
Optional parameters are not supported in C#.  TO get the same behavior, you need to create mutliple methods.  For example.

if you have a sub in vb called dosomething with two parameters and one is optional, in c# you would do

public void dosomething(int MyParm1){
//your code
}

public void dosomething(int MyParm1, int MyParm2){
//your code to use when both paramters are needed.
}



this line

 Private Sub InitialiseClasses(Optional ByVal ClassSizes As Integer = 15, Optional ByVal AmountofClasses As Integer = 3)

is converted into this

private void InitialiseClasses(int ClassSizes, void =, void 15, int AmountofClasses, void =, void 3) {

doesn't look right
Can you help me convert this properly....

   ''' <summary>
    ''' Resets the radiolist when the dropdownlist with classes is changed
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlClass_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlClass.SelectedIndexChanged

        ' ReBind the radiolist based on the amount of seats left in the class
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item(ddlClass.SelectedIndex).ToString)
        radSeatsAvailable.DataBind()

        UpdateLabels()

    End Sub
Automated tools can't do it all!  Try just changing it to

private void InitialiseClasses(int ClassSizes, int AmountofClasses) {

   /// <summary>
    ///  Resets the radiolist when the dropdownlist with classes is changed
    ///  </summary>
    ///  <param name="sender"></param>
    ///  <param name="e"></param>
    ///  <remarks></remarks>
    protected void ddlClass_SelectedIndexChanged(object sender, System.EventArgs e) {
        //  ReBind the radiolist based on the amount of seats left in the class
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item[ddlClass.SelectedIndex].ToString);
        radSeatsAvailable.DataBind();
        UpdateLabels();
    }
In the IDE, highlight the ddlClass object and in the SelectedIndexChanged event, enter ddlClass_SelectedIndexChanged as the handler.  Or to manually do it, you could put this in your page load

ddlClass.SelectedIndexChanged += ddlClass_SelectedIndexChanged(object sender, System.EventArgs e);
this is the code I have right now

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Labs_lab2 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e) {
        //  if first load then setup classes
        if (!Page.IsPostBack) {
            InitialiseClasses();
            UpdateLabels();
        }
}

    private void InitialiseClasses(int ClassSizes, int AmountofClasse) {
        ClassSizes = 15;
        AmountofClasse = 3;
        ArrayList TheClasses = new ArrayList();
        // Warning!!! Optional parameters not supported
        // Warning!!! Optional parameters not supported
        int i;
        //  For the Amount of Classes Specified
        for (i = 1; (i <= AmountofClasses); i++) {
            //  Add an integer of size ClassSizes to the ArrayList
            TheClasses.Add(ClassSizes);
        }
        //  Add the Classes Arraylist to the Session
        Session("Classes") = TheClasses;
        //  Create an Arraylist of the size of ClassSizes and bind it to the Radio List
        radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes);
        radSeatsAvailable.DataBind();
    }


    private ArrayList PopulateArrayList(int ArrayListLength) {
        ArrayList myarray = new ArrayList();
        int i;
        //  For the size of the arraylist
        for (i = 1; (i <= ArrayListLength); i++) {
            //  Add an integer of the value i
            myarray.Add(i);
        }
        return myarray;
    }
   

    protected void ddlClass_SelectedIndexChanged(object sender, System.EventArgs e) {
        //  ReBind the radiolist based on the amount of seats left in the class
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item[ddlClass.SelectedIndex].ToString);
        radSeatsAvailable.DataBind();
        UpdateLabels();
    }
   

    protected void btnPurchase_Click(object sender, System.EventArgs e) {
        //  Deduct the amount of seats based on the radio button selected
        Session("classes").Item[ddlClass.SelectedIndex] = (Session("classes").Item[ddlClass.SelectedIndex]
                    - (radSeatsAvailable.SelectedIndex - 1));
        //  Bind the new amount of seats left
        radSeatsAvailable.DataSource = PopulateArrayList(Session("classes").Item[ddlClass.SelectedIndex].ToString);
        radSeatsAvailable.DataBind();
        UpdateLabels();
    }
   

    private void UpdateLabels() {
        lblClass1.Text = Session("classes").Item[0].ToString;
        lblClass2.Text = Session("classes").Item[1].ToString;
        lblClass3.Text = Session("classes").Item[2].ToString;
    }
}








Are you getting errors?
yes here is it

Compiler Error Message: CS1501: No overload for method 'InitialiseClasses' takes '0' arguments

Source Error:

 

Line 15:         //  if first load then setup classes
Line 16:         if (!Page.IsPostBack) {
Line 17:             InitialiseClasses();
Line 18:             UpdateLabels();
Line 19:         }
 
Ok....so your call to initialiseClasses was not using either of the optional paramters in VB, so you new method should be this.  Replace your existing IntialiseClasses method with these 2 methods.

  private void InitialiseClasses() {
        ClassSizes = 15;
        AmountofClasse = 3;
        ArrayList TheClasses = new ArrayList();
        // Warning!!! Optional parameters not supported
        // Warning!!! Optional parameters not supported
        int i;
        //  For the Amount of Classes Specified
        for (i = 1; (i <= AmountofClasses); i++) {
            //  Add an integer of size ClassSizes to the ArrayList
            TheClasses.Add(ClassSizes);
        }
        //  Add the Classes Arraylist to the Session
        Session("Classes") = TheClasses;
        //  Create an Arraylist of the size of ClassSizes and bind it to the Radio List
        radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes);
        radSeatsAvailable.DataBind();
    }


  private void InitialiseClasses(int ClassSizes, int AmountofClasse) {
        ArrayList TheClasses = new ArrayList();
        int i;
        //  For the Amount of Classes Specified
        for (i = 1; (i <= AmountofClasses); i++) {
            //  Add an integer of size ClassSizes to the ArrayList
            TheClasses.Add(ClassSizes);
        }
        //  Add the Classes Arraylist to the Session
        Session("Classes") = TheClasses;
        //  Create an Arraylist of the size of ClassSizes and bind it to the Radio List
        radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes);
        radSeatsAvailable.DataBind();
    }


Or even better, with this....

 private void InitialiseClasses() {
        ClassSizes = 15;
        AmountofClasse = 3;
        InitialiseClasses(ClassSizes, AmountofClasse);
}


  private void InitialiseClasses(int ClassSizes, int AmountofClasse) {
        ArrayList TheClasses = new ArrayList();
        int i;
        //  For the Amount of Classes Specified
        for (i = 1; (i <= AmountofClasses); i++) {
            //  Add an integer of size ClassSizes to the ArrayList
            TheClasses.Add(ClassSizes);
        }
        //  Add the Classes Arraylist to the Session
        Session("Classes") = TheClasses;
        //  Create an Arraylist of the size of ClassSizes and bind it to the Radio List
        radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes);
        radSeatsAvailable.DataBind();
    }




got this error now

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0118: 'System.Web.UI.Page.Session' is a 'property' but is used like a 'method'

Source Error:

 

Line 40:         }
Line 41:         //  Add the Classes Arraylist to the Session
Line 42:         Session("Classes") = TheClasses;
Line 43:         //  Create an Arraylist of the size of ClassSizes and bind it to the Radio List
Line 44:         radSeatsAvailable.DataSource = PopulateArrayList(ClassSizes);
 
try

Session["Classes"] = TheClasses;
that solved that issues here is another one

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'object' does not contain a definition for 'Item'

Source Error:

 

Line 61:     protected void ddlClass_SelectedIndexChanged(object sender, System.EventArgs e) {
Line 62:         //  ReBind the radiolist based on the amount of seats left in the class
Line 63:         radSeatsAvailable.DataSource = PopulateArrayList(Session["classes"].Item[ddlClass.SelectedIndex].ToString);
Line 64:         radSeatsAvailable.DataBind();
Line 65:         UpdateLabels();
 

Source File: C:\Inetpub\int422_071sq08\webcontent\Labs\lab2.aspx.cs    Line: 63
In C# you need to type everything.  objects in Session are object types.  To access their members you need to explicitly convert them to what they are.

radSeatsAvailable.DataSource = PopulateArrayList(((ArrayList )Session["classes"]).Item[ddlClass.SelectedIndex].ToString);
its not helping me man... lots of error... not sure what to do
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'object' does not contain a definition for 'Item'

Source Error:

 

Line 61:     protected void ddlClass_SelectedIndexChanged(object sender, System.EventArgs e) {
Line 62:         //  ReBind the radiolist based on the amount of seats left in the class
Line 63:         radSeatsAvailable.DataSource = PopulateArrayList(Session["classes"].Item[ddlClass.SelectedIndex].ToString);
Line 64:         radSeatsAvailable.DataBind();
Line 65:         UpdateLabels();
 
This is the same exact error as above, you need to type Session["classes"] to an ArrayList

radSeatsAvailable.DataSource = PopulateArrayList(((ArrayList )Session["classes"]).Item[ddlClass.SelectedIndex].ToString);

or to make it easier to understand....

ArrayList x = (ArrayList) Session["classes"];
radSeatsAvailable.DataSource = PopulateArrayList(x.Item[ddlClass.SelectedIndex].ToString);
radSeatsAvailable.DataBind();
UpdateLabels();



this one now

 

Line 26:    
Line 27:
Line 28:     private ArrayList PopulateArrayList(int ArrayListLength) {
Line 29:         ArrayList myarray = new ArrayList();
Line 30:         int i;
 
Try

System.Collections.ArrayList myarray = new System.Collections.ArrayList(ArrayListLength);