C# equivalent syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C# newcomer

    C# equivalent syntax

    Hi all
    Please help me with the C# equivalent syntax for the
    following C++ code. Thanks a lot in advance.

    double *firstVal;
    int secondVal;
    double thirdVal;
    int fourthVal;

    firstVal = new double[secondVal];
    *(firstVal + thirdVal) = fourthVal;

    delete [] firstVal;
  • Tu-Thach

    #2
    RE: C# equivalent syntax

    Are we glad C# does not have those pointers! Anyways, that code does not make much sense

    Tu-Thac

    ----- C# newcomer wrote: ----

    Hi al
    Please help me with the C# equivalent syntax for the
    following C++ code. Thanks a lot in advance

    double *firstVal;
    int secondVal
    double thirdVal
    int fourthVal

    firstVal = new double[secondVal]
    *(firstVal + thirdVal) = fourthVal

    delete [] firstVal

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: C# equivalent syntax

      C# newcomer <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
      > Please help me with the C# equivalent syntax for the
      > following C++ code. Thanks a lot in advance.
      >
      > double *firstVal;
      > int secondVal;
      > double thirdVal;
      > int fourthVal;
      >
      > firstVal = new double[secondVal];
      > *(firstVal + thirdVal) = fourthVal;
      >
      > delete [] firstVal;[/color]

      Converting bare syntax is rarely a good idea, especially in thise case
      where there isn't necessarily an equivalent. Instead, tell us what
      you're trying to do and we can tell you what idiom is usually used in
      C#.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Stoitcho Goutsev \(100\) [C# MVP]

        #4
        Re: C# equivalent syntax

        Hi C# newcomer,
        Welcome to C# :)
        [color=blue]
        > double *firstVal;[/color]
        you can't use pointers anymore. You have to use indeces to access items[color=blue]
        > int secondVal;
        > double thirdVal;
        > int fourthVal;
        >
        > firstVal = new double[secondVal];
        > *(firstVal + thirdVal) = fourthVal;
        >
        > delete [] firstVal;[/color]

        int firstVal;
        int secondVal;
        double thirdVal;
        int fourthVal;

        //I assume all valrirables are initialized
        firstVal = 0;
        double[] doubleArray = new double[secondVal];
        doubleArray[firstVal + thirdVal] = fourthVal;

        Pay attention that the firstVal is index rather than pointer. This could
        affect your program logic.

        --
        B\rgds
        100
        "C# newcomer" <anonymous@disc ussions.microso ft.com> wrote in message
        news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl.. .[color=blue]
        > Hi all
        > Please help me with the C# equivalent syntax for the
        > following C++ code. Thanks a lot in advance.
        >
        > double *firstVal;
        > int secondVal;
        > double thirdVal;
        > int fourthVal;
        >
        > firstVal = new double[secondVal];
        > *(firstVal + thirdVal) = fourthVal;
        >
        > delete [] firstVal;[/color]


        Comment

        • Stoitcho Goutsev \(100\) [C# MVP]

          #5
          Re: C# equivalent syntax

          This is code snippet, I believe, and it makes sense as so.

          --
          B\rgds
          100
          "Tu-Thach" <anonymous@disc ussions.microso ft.com> wrote in message
          news:93EF0E49-2974-4319-8433-B86000089329@mi crosoft.com...[color=blue]
          > Are we glad C# does not have those pointers! Anyways, that code does not[/color]
          make much sense.[color=blue]
          >
          > Tu-Thach
          >
          > ----- C# newcomer wrote: -----
          >
          > Hi all
          > Please help me with the C# equivalent syntax for the
          > following C++ code. Thanks a lot in advance.
          >
          > double *firstVal;
          > int secondVal;
          > double thirdVal;
          > int fourthVal;
          >
          > firstVal = new double[secondVal];
          > *(firstVal + thirdVal) = fourthVal;
          >
          > delete [] firstVal;
          >[/color]


          Comment

          • Tu-Thach

            #6
            Re: C# equivalent syntax

            You either have to cast thirdVal to an int or declare it as an int otherwise this code will not work

            Tu-Thac

            ----- Stoitcho Goutsev (100) [C# MVP] wrote: ----

            Hi C# newcomer
            Welcome to C# :
            [color=blue]
            > double *firstVal[/color]
            you can't use pointers anymore. You have to use indeces to access item[color=blue]
            > int secondVal
            > double thirdVal
            > int fourthVal[color=green]
            >> firstVal = new double[secondVal][/color]
            > *(firstVal + thirdVal) = fourthVal[color=green]
            >> delete [] firstVal[/color][/color]

            int firstVal
            int secondVal
            double thirdVal
            int fourthVal

            //I assume all valrirables are initialize
            firstVal = 0
            double[] doubleArray = new double[secondVal]
            doubleArray[firstVal + thirdVal] = fourthVal

            Pay attention that the firstVal is index rather than pointer. This coul
            affect your program logic

            --
            B\rgd
            10
            "C# newcomer" <anonymous@disc ussions.microso ft.com> wrote in messag
            news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl..[color=blue]
            > Hi al
            > Please help me with the C# equivalent syntax for th
            > following C++ code. Thanks a lot in advance[color=green]
            >> double *firstVal[/color]
            > int secondVal
            > double thirdVal
            > int fourthVal[color=green]
            >> firstVal = new double[secondVal][/color]
            > *(firstVal + thirdVal) = fourthVal[color=green]
            >> delete [] firstVal[/color][/color]



            Comment

            • phoenix

              #7
              Re: C# equivalent syntax


              "Tu-Thach" <anonymous@disc ussions.microso ft.com> schreef in bericht
              news:93EF0E49-2974-4319-8433-B86000089329@mi crosoft.com...[color=blue]
              > Are we glad C# does not have those pointers! Anyways, that code does not[/color]
              make much sense.[color=blue]
              >
              > Tu-Thach
              >[/color]

              I'm glad it still has those pointers when you really need them ...

              Yves


              Comment

              • Stoitcho Goutsev \(100\) [C# MVP]

                #8
                Re: C# equivalent syntax

                Oh, yes, sorry. I think it is and error in the original post and I missted
                it.
                Actually, I believe C# newcommer wanted to write
                int thirdVal;
                and
                double fourtVal;.

                Anyways I believe the question was prety clear, since it wasn't
                what-is-wrong-with-my-code one.
                --
                B\rgds
                100
                "Tu-Thach" <tuthach@ongtec h.com> wrote in message
                news:890CE033-3F1C-4B5F-BAB4-93D5BF2D9D3C@mi crosoft.com...[color=blue]
                > You either have to cast thirdVal to an int or declare it as an int[/color]
                otherwise this code will not work.[color=blue]
                >
                > Tu-Thach
                >
                > ----- Stoitcho Goutsev (100) [C# MVP] wrote: -----
                >
                > Hi C# newcomer,
                > Welcome to C# :)
                >[color=green]
                > > double *firstVal;[/color]
                > you can't use pointers anymore. You have to use indeces to access[/color]
                items[color=blue][color=green]
                > > int secondVal;
                > > double thirdVal;
                > > int fourthVal;[color=darkred]
                > >> firstVal = new double[secondVal];[/color]
                > > *(firstVal + thirdVal) = fourthVal;[color=darkred]
                > >> delete [] firstVal;[/color][/color]
                >
                > int firstVal;
                > int secondVal;
                > double thirdVal;
                > int fourthVal;
                >
                > //I assume all valrirables are initialized
                > firstVal = 0;
                > double[] doubleArray = new double[secondVal];
                > doubleArray[firstVal + thirdVal] = fourthVal;
                >
                > Pay attention that the firstVal is index rather than pointer. This[/color]
                could[color=blue]
                > affect your program logic.
                >
                > --
                > B\rgds
                > 100
                > "C# newcomer" <anonymous@disc ussions.microso ft.com> wrote in message
                > news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl.. .[color=green]
                > > Hi all
                > > Please help me with the C# equivalent syntax for the
                > > following C++ code. Thanks a lot in advance.[color=darkred]
                > >> double *firstVal;[/color]
                > > int secondVal;
                > > double thirdVal;
                > > int fourthVal;[color=darkred]
                > >> firstVal = new double[secondVal];[/color]
                > > *(firstVal + thirdVal) = fourthVal;[color=darkred]
                > >> delete [] firstVal;[/color][/color]
                >
                >
                >[/color]


                Comment

                • Ken Onweller \(.NET MCSD\)

                  #9
                  Re: C# equivalent syntax

                  double firstVal[];
                  int secondVal;
                  double thirdVal;
                  int fourthVal;

                  firstVal = new double[secondVal];
                  firstVal[thirdVal] = fourthVal;

                  "C# newcomer" <anonymous@disc ussions.microso ft.com> wrote in message
                  news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl.. .[color=blue]
                  > Hi all
                  > Please help me with the C# equivalent syntax for the
                  > following C++ code. Thanks a lot in advance.
                  >
                  > double *firstVal;
                  > int secondVal;
                  > double thirdVal;
                  > int fourthVal;
                  >
                  > firstVal = new double[secondVal];
                  > *(firstVal + thirdVal) = fourthVal;
                  >
                  > delete [] firstVal;[/color]


                  Comment

                  • Bruno Jouhier [MVP]

                    #10
                    Re: C# equivalent syntax

                    double[] firstVal;
                    int secondVal;
                    double thirdVal;
                    int fourthVal;

                    // secondVal, thirdVal and fourthVal must be set before the following lines,
                    otherwise the C# compiler will reject them.
                    firstVal = new double[secondVal];
                    // C++ version of following line would also benefit from the rewriting
                    below!
                    firstVal[(int)thirdVal] = fourthVal;
                    // no need to delete array, GC does it!

                    Morale: C# just forces you to write something that make sense (from a type
                    checking standpoint). You can forget about ugly pointer hacks (like
                    *(firstVal + thirdVal) -- does C++ really accept this without barking, seems
                    to me that an (int) cast would be necessary on thirdVal)

                    Bruno.

                    "C# newcomer" <anonymous@disc ussions.microso ft.com> a écrit dans le message
                    de news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl.. .[color=blue]
                    > Hi all
                    > Please help me with the C# equivalent syntax for the
                    > following C++ code. Thanks a lot in advance.
                    >
                    > double *firstVal;
                    > int secondVal;
                    > double thirdVal;
                    > int fourthVal;
                    >
                    > firstVal = new double[secondVal];
                    > *(firstVal + thirdVal) = fourthVal;
                    >
                    > delete [] firstVal;[/color]


                    Comment

                    • Stoitcho Goutsev \(100\) [C# MVP]

                      #11
                      Re: C# equivalent syntax

                      Hi Ken,
                      As long as it is a code snipped and we don't actually know how the code,
                      which simplification we have, works
                      lines like
                      *(firstVal + thirdVal) = fourthVal;

                      Makes me believe that the array is indexed changing both firstVal and
                      thirdVal
                      otherwise it would be
                      firstVal[thirdVal] = fourthVal;

                      That's way I believe we should keep virstVal as an index to the array and
                      have one separate array object

                      --
                      B\rgds
                      100
                      "Ken Onweller (.NET MCSD)" <anonymous@disc ussions.microso ft.com> wrote in
                      message news:exqpOTr5DH A.2556@TK2MSFTN GP09.phx.gbl...[color=blue]
                      > double firstVal[];
                      > int secondVal;
                      > double thirdVal;
                      > int fourthVal;
                      >
                      > firstVal = new double[secondVal];
                      > firstVal[thirdVal] = fourthVal;
                      >
                      > "C# newcomer" <anonymous@disc ussions.microso ft.com> wrote in message
                      > news:6e3001c3e6 85$3d56bf80$a60 1280a@phx.gbl.. .[color=green]
                      > > Hi all
                      > > Please help me with the C# equivalent syntax for the
                      > > following C++ code. Thanks a lot in advance.
                      > >
                      > > double *firstVal;
                      > > int secondVal;
                      > > double thirdVal;
                      > > int fourthVal;
                      > >
                      > > firstVal = new double[secondVal];
                      > > *(firstVal + thirdVal) = fourthVal;
                      > >
                      > > delete [] firstVal;[/color]
                      >
                      >[/color]


                      Comment

                      Working...