Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Plz Help - Visual Basic .net/visual C++ .net Code (http://www.programmingforums.org/showthread.php?t=12393)

VBuser Jan 17th, 2007 8:31 AM

Plz Help - Visual Basic .net/visual C++ .net Code
 
Hi! Plz help anyone!!! I wrote this program from a visual basic .net book. It works fine but when i wanna write the same program using visual c++ .net code i get errors. Im using Visual studio.net 2003


:

  Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intHours, intAllow As Integer, sngRate As Single
        ' display hours worked
        For intHours = 1 To 50
            Me.HoursList.Items.Add(intHours)
        Next intHours
        'display pay rates

        For sngRate = 6 To 12 Step 0.5
            Me.RateList.Items.Add(Format(sngRate, "standard"))
        Next sngRate

        'display witholding allowances
        For intAllow = 0 To 10
            Me.AllowList.Items.Add(intAllow)
        Next intAllow
        'select default item in each list box
        Me.HoursList.SelectedItem = 40
        Me.RateList.SelectedIndex = 0
        Me.AllowList.SelectedIndex = 0


    End Sub

    Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim intHours As Integer
        Dim sngRate, sngGross, sngFWT, sngFICA, sngNet As Single
        'assign selected items to variables
        intHours = Me.HoursList.SelectedItem
        sngRate = Me.RateList.SelectedItem

        'calculate gross pay
        If intHours <= 40 Then
            sngGross = intHours * sngRate
        Else
            sngGross = 40 * sngRate + (intHours - 40) * sngRate * 1.5
        End If
        'calculate FWT
        sngFWT = GetFwtTax(sngGross)
        'calculate fica tax
        sngFICA = sngGross * 0.0765
        'round gross pay, fwt and fica tax
        sngGross = Math.Round(sngGross, 2)
        sngFWT = Math.Round(sngFWT, 2)
        sngFICA = Math.Round(sngFICA, 2)
        'calculate net pay
        sngNet = sngGross - sngFWT - sngFICA
        'display calculated amounts
        Me.GrossLabel.Text = Format(sngGross, "currency")
        Me.FWTLabel.Text = Format(sngFWT, "currency")
        Me.FICALabel.Text = Format(sngFICA, "currency")
        Me.NetLabel.Text = Format(sngNet, "currency")
    End Sub

    Private Function GetFwtTax(ByVal sngWeekPay As Single) As Single
        Dim intAllow As Integer, sngTaxWages, sngTax As Single
        'assign number of witholding allowances to a variable
        intAllow = Me.AllowList.SelectedItem

        'calculate taxable wages
        sngTaxWages = sngWeekPay - intAllow * 55.77
        'determine marital status then calculate FWT
        If Me.SingleRadio.Checked = True Then
            Select Case sngTaxWages
                Case Is <= 51
                    sngTax = 0
                Case Is <= 552
                    sngTax = 0.15 * (sngTaxWages - 51)
                Case Is <= 1196
                    sngTax = 75.15 + 0.28 * (sngTaxWages - 552)
                Case Is <= 2662
                    sngTax = 255.47 + 0.31 * (sngTaxWages - 1196)
                Case Is <= 5750
                    sngTax = 709.93 + 0.36 * (sngTaxWages - 2662)
                Case Else
                    sngTax = 1821.61 + 0.396 * (sngTaxWages - 5750)
            End Select
        Else    'marriedradio is selected
            Select Case sngTaxWages
                Case Is <= 124
                    sngTax = 0
                Case Is <= 960
                    sngTax = 0.15 * (sngTaxWages - 124)
                Case Is <= 2023
                    sngTax = 125.4 + 0.28 * (sngTaxWages - 960)
                Case Is <= 3292
                    sngTax = 423.04 + 0.31 * (sngTaxWages - 2023)
                Case Is <= 5809
                    sngTax = 816.43 + 0.36 * (sngTaxWages - 3292)
                Case Else
                    sngTax = 1722.55 + 0.396 * (sngTaxWages - 5809)
            End Select
        End If
        'return FWT
        Return sngTax
    End Function

   
    Private Sub ClearLabels(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles NameTextBox.TextChanged, HoursList.SelectedValueChanged, _
            RateList.SelectedValueChanged, AllowList.SelectedValueChanged, _
            MarriedRadio.Click, SingleRadio.Click

        Me.GrossLabel.Text = ""
        Me.FWTLabel.Text = ""
        Me.FICALabel.Text = ""
        Me.NetLabel.Text = ""
    End Sub

    Private Sub NameTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles NameTextBox.Enter
        Me.NameTextBox.SelectAll()
    End Sub

End Class


Arevos Jan 17th, 2007 9:23 AM

Quote:

Originally Posted by VBuser (Post 122727)
It works fine but when i wanna write the same program using visual c++ .net code i get errors.

It usually helps to mention (a) what the errors are, and (b) show people the code causing the errors. How on Earth is anyone meant to tell what's wrong with your program if you don't show the source code or what errors occurred?

melbolt Jan 17th, 2007 9:57 AM

I hope you know this but...

C++ .NET is not VB .NET
big syntax difference there

just a few examples (semicolons, no "dim", etc, etc)

you're pretty much going to have to re-write the whole thing if you want to run it in C++.

VBuser Jan 17th, 2007 10:11 AM

Quote:

Originally Posted by melbolt (Post 122730)
I hope you know this but...

C++ .NET is not VB .NET
big syntax difference there

just a few examples (semicolons, no "dim", etc, etc)

you're pretty much going to have to re-write the whole thing if you want to run it in C++.

I do know but i wanted the code for c++ .net cause i get errors when coding the calcbutton event

ReggaetonKing Jan 17th, 2007 10:50 AM

Then post your code you wrote in C++ and we will do our best.

pegasus001 Jan 17th, 2007 10:53 AM

Have you imported the right libraries. Because if you did everything should work fine. It is not difficult to translate between .NET languages. But you must know something else, if you want to compile the code in visual studio you must compile it with /clr(in the console) or enable the clr in the project options.

VBuser Jan 18th, 2007 11:42 AM

Well its not a console appliction, its a windows form app. And if u look at the VB code at the top, the calcButton event at the for loop, by trying to write the same thing with c++ code, it gives errors
:

int hours
for(hours=1; hours<=50; hours++)
{
        this->HoursListBox->Items->Add(hours);
}

it gives error C2664: c:\Documents and Settings\fe\My Documents\Visual Studio Projects\Payroll\Form1.h(366): error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add' : cannot convert parameter 1 from 'int' to 'System::Object __gc *'

DaWei Jan 18th, 2007 12:14 PM

The error message is pretty clear. Check the prototype for Add and explicitly convert 'hours' to an object of that type. You might consider resorting to your help files, using the term, "C2664". Just a thought.

Arevos Jan 18th, 2007 12:23 PM

I haven't done much work with the C++ .NET interface, but you probably have to do something like:
:

System::Int32 hours;
for(hours=1; hours<=50; hours++)
{
        this->HoursListBox->Items->Add(hours);
}


lectricpharaoh Jan 19th, 2007 12:19 AM

You might need to explicitly 'box' the int. I'm not sure if VC++ boxes and unboxes stuff automagically, whereas C# does (which has good and bad sides). Try casting it to System::Object first, like so:
:

this->HoursListBox->Items->Add((System::Object)hours);
@Arevos: If managed C++ is like C#, the int type is probably an alias for System.Int32, so the changed definition would be unnecessary in that case (and fail to resolve the problem). Still, I suppose it's worth a shot, as I don't know if managed C++ behaves this way (never used it; if I'm gonna write managed code, I'll stick to C#, using C++ for native code).


All times are GMT -5. The time now is 3:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC