C# equivalent syntax

C

C# newcomer

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;
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

C# newcomer said:
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;

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#.
 
S

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

Hi C# newcomer,
Welcome to C# :)
double *firstVal;
you can't use pointers anymore. You have to use indeces to access items
int secondVal;
double thirdVal;
int fourthVal;

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

delete [] firstVal;

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 said:
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;
 
S

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

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

Guest

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# :
double *firstVal
you can't use pointers anymore. You have to use indeces to access item
int secondVal
double thirdVal
int fourthVal
firstVal = new double[secondVal]
*(firstVal + thirdVal) = fourthVal
delete [] firstVal

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 said:
Hi al
Please help me with the C# equivalent syntax for th
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
 
P

phoenix

Tu-Thach said:
Are we glad C# does not have those pointers! Anyways, that code does not make much sense.

Tu-Thach

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

Yves
 
S

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

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 said:
You either have to cast thirdVal to an int or declare it as an int
otherwise this code will not work.
Tu-Thach

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

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

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 said:
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;
 
B

Bruno Jouhier [MVP]

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.
 
K

Ken Onweller \(.NET MCSD\)

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

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

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

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) said:
double firstVal[];
int secondVal;
double thirdVal;
int fourthVal;

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

C# newcomer said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top