9 Replies - 693 Views - Last Post: 19 November 2012 - 02:15 PM Rate Topic: -----

#1 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Visual Basic to C# conversion help......

Posted 19 November 2012 - 12:27 PM

I'm hoping that I can get some pointers from you guys/gals about converting vb to c#. I am currenting in a .NET Programming glass that we have worked with nothing by vb the entire class and now our week 7 assignment is the convert a GPA Calculation program we created in vb to C#. I have pretty much all the foundation and everything built I just can't get part to work.

What I have in VB is below. But what it does is after a number of tests is inputted on Form1 and "Submit" is clicked it reads TextBox6.Text and then prompts an InputBox that allows you to input a score for each test you identified i.e. if you said 3 tests it will run three times then be calculated and output the GPA for the tests to the second form. It worked perfectly in VB but I cant get it to work in C#. Any help would be greatly appreciated.

Dim Prompt, Title As String
            Dim A As Short
            Total = 0
            Prompt = "Enter Test Scores For " & TextBox1.Text
            Grade = TextBox6.Text
            If Grade > 0 Then ReDim Scores(Grade - 1)
            For A = 0 To UBound(Scores)
                Title = "Score " & (A + 1)
                Scores(A) = InputBox(Prompt, Title)
                Total = Total + Scores(A)
            Next
            Result = Format(Total / Grade, "00.0")
            TestInfo.Label12.Text = Result

            If TextBox2.Text = "Enter Course Title" Then
                TestInfo.Show()





            string Prompt = null;
            string Title = null;
            short A = 0;
            int Total, Result;
            float Grades;
            int[] Scores = new int[A];
            Prompt = "Enter Test Scores For " + TextBox1.Text;
            Grades = TextBox6.Text;
            if (Grades > 0)
                Scores = new float[Grades];
            for (A = 0; A <= Information.UBound(Scores); A++)
            {
                Title = "Score " + (A + 1);
                Scores[A] = Interaction.InputBox(Prompt, Title);
                Total = Total + Scores[A];
            }
            Result = (Total / Grades);



Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic to C# conversion help......

#2 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 12:29 PM

I'm hoping that I can get some pointers from you guys/gals about converting vb to c#. I am currenting in a .NET Programming glass that we have worked with nothing by vb the entire class and now our week 7 assignment is the convert a GPA Calculation program we created in vb to C#. I have pretty much all the foundation and everything built I just can't get part to work.

What I have in VB is below. But what it does is after a number of tests is inputted on Form1 and "Submit" is clicked it reads TextBox6.Text and then prompts an InputBox that allows you to input a score for each test you identified i.e. if you said 3 tests it will run three times then be calculated and output the GPA for the tests to the second form. It worked perfectly in VB but I cant get it to work in C#. Any help would be greatly appreciated.

Visual Basic Code
Dim Prompt, Title As String
            Dim A As Short
            Total = 0
            Prompt = "Enter Test Scores For " & TextBox1.Text
            Grade = TextBox6.Text
            If Grade > 0 Then ReDim Scores(Grade - 1)
            For A = 0 To UBound(Scores)
                Title = "Score " & (A + 1)
                Scores(A) = InputBox(Prompt, Title)
                Total = Total + Scores(A)
            Next
            Result = Format(Total / Grade, "00.0")
            TestInfo.Label12.Text = Result

            If TextBox2.Text = "Enter Course Title" Then
                TestInfo.Show()




C# Code
            string Prompt = null;
            string Title = null;
            short A = 0;
            int Total, Result;
            float Grades;
            int[] Scores = new int[A];
            Prompt = "Enter Test Scores For " + TextBox1.Text;
            Grades = TextBox6.Text;
            if (Grades > 0)
                Scores = new float[Grades];
            for (A = 0; A <= Information.UBound(Scores); A++)
            {
                Title = "Score " + (A + 1);
                Scores[A] = Interaction.InputBox(Prompt, Title);
                Total = Total + Scores[A];
            }
            Result = (Total / Grades);


Was This Post Helpful? 0
  • +
  • -

#3 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 12:30 PM

I'm hoping that I can get some pointers from you guys/gals about converting vb to c#. I am currenting in a .NET Programming glass that we have worked with nothing by vb the entire class and now our week 7 assignment is the convert a GPA Calculation program we created in vb to C#. I have pretty much all the foundation and everything built I just can't get part to work.

What I have in VB is below. But what it does is after a number of tests is inputted on Form1 and "Submit" is clicked it reads TextBox6.Text and then prompts an InputBox that allows you to input a score for each test you identified i.e. if you said 3 tests it will run three times then be calculated and output the GPA for the tests to the second form. It worked perfectly in VB but I cant get it to work in C#. Any help would be greatly appreciated.

Visual Basic Code
Dim Prompt, Title As String
            Dim A As Short
            Total = 0
            Prompt = "Enter Test Scores For " & TextBox1.Text
            Grade = TextBox6.Text
            If Grade > 0 Then ReDim Scores(Grade - 1)
            For A = 0 To UBound(Scores)
                Title = "Score " & (A + 1)
                Scores(A) = InputBox(Prompt, Title)
                Total = Total + Scores(A)
            Next
            Result = Format(Total / Grade, "00.0")
            TestInfo.Label12.Text = Result

            If TextBox2.Text = "Enter Course Title" Then
                TestInfo.Show()




C# Code
            string Prompt = null;
            string Title = null;
            short A = 0;
            int Total, Result;
            float Grades;
            int[] Scores = new int[A];
            Prompt = "Enter Test Scores For " + TextBox1.Text;
            Grades = TextBox6.Text;
            if (Grades > 0)
                Scores = new float[Grades];
            for (A = 0; A <= Information.UBound(Scores); A++)
            {
                Title = "Score " + (A + 1);
                Scores[A] = Interaction.InputBox(Prompt, Title);
                Total = Total + Scores[A];
            }
            Result = (Total / Grades);


Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 01:23 PM

C# will not automatically convert text to a numeric value, so you'll need to do it in line 8 and most likely again in 14.

In C# we usually do not declare loop control variables outside the loop. So instead of
short A= 0; 
...
for (A = 0; A <= Information.UBound(Scores); A++)
we would write
for (int A = 0; A <= Information.UBound(Scores); A++)
.

Using short/float is rare unless there is a specific reason to do so, normally int/double will be used.

Variables are usually named in camel case (examples: myVariable, yourVariable, thisVariableHere) rather than pascal case (examples: MyVariable, YourVariable, ThisVariableHere). Method names use pascal case.
Was This Post Helpful? 1
  • +
  • -

#5 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 01:56 PM

View PostMomerath, on 19 November 2012 - 01:23 PM, said:

C# will not automatically convert text to a numeric value, so you'll need to do it in line 8 and most likely again in 14.

In C# we usually do not declare loop control variables outside the loop. So instead of
short A= 0; 
...
for (A = 0; A <= Information.UBound(Scores); A++)
we would write
for (int A = 0; A <= Information.UBound(Scores); A++)
.

Using short/float is rare unless there is a specific reason to do so, normally int/double will be used.

Variables are usually named in camel case (examples: myVariable, yourVariable, thisVariableHere) rather than pascal case (examples: MyVariable, YourVariable, ThisVariableHere). Method names use pascal case.


            string Prompt, Title;
            int Grades, Result;
            int A = 0;
            int Total = 0;
            int[] Scores = new int[A];
            Prompt = "Enter Test Scores For " + TextBox1.Text;
            Grades = Convert.ToInt32(Test1);
            if (Grades != 0)
                Scores = new int[Grades];
            for (A = 0; A <= Information.UBound(Scores); A++)
            {
                Title = "Score " + (A + 1);
                Scores[A] = Convert.ToInt32(Interaction.InputBox(Prompt, Title));
                Total = Total + Scores[A];
            }
            Result = (Total / Grades);



Thanks for the help! I am now error free....I know I assigned A = 0 before and in the for statement but it killed all my errors. Now when I run it i can't get past the Convert.ToInt32 on line 8, does it look like I converted it wrong? It is throwing "Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'."
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 01:59 PM

You are trying to convert the textbox control to an int, rather than the contents of the textbox control to an int.
Grades = Convert.ToInt32(Test1.Text);
should fix that. Just for general knowledge (and you'll be wanting to use in the future) look up Int32.Parse and Int32.TryParse.
Was This Post Helpful? 1
  • +
  • -

#7 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 02:04 PM

Sweet, thanks for the help. What you said makes perfect sense, I don't know why I didn't think about it like that. This got my Inputbox working, now I have to work on getting the values inputted to add up again and convert to my GPA....again thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 02:05 PM

Why the hell are there three copies of the same post???
Was This Post Helpful? 0
  • +
  • -

#9 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 02:06 PM

I thought posting it three times would add to them importance......just kidding. It was an obvious error on my part and didn't see where I could go back and edit to remove them.
Was This Post Helpful? 0
  • +
  • -

#10 03EVOAWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 11

Re: Visual Basic to C# conversion help......

Posted 19 November 2012 - 02:15 PM

Also sorry about posting help in the title....won't happen again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1