Solved

Convert CPP to C#

Posted on 2014-04-12
6
528 Views
Last Modified: 2014-04-12
How do I convert the following to C#

struct fpoint : pair<double> {
    fpoint() : pair<double>() { }
    fpoint(double x, double y) : pair<double>(x, y) { }
};
0
Comment
Question by:JElster
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 39996739
The C# equivalent could be

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base((x, y) { }
}; 

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 39996749
Get a bunch of compile errors,, like missing param.
Probably close... thx
0
 
LVL 86

Expert Comment

by:jkr
ID: 39996758
Soory, had my mind tilted towards C++ a bit too much - does

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base() { key = x; value = y; }
}; 
                                            

Open in new window


work better?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:JElster
ID: 39996764
Error      3      Structs cannot contain explicit parameterless constructors      

Error      2      Type 'System.Collections.Generic.KeyValuePair<...>' in interface list is not an interface
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 39996766
I disagree slightly, but I don't think it's of major concern. From what I read, the Tuple class would be closer to the exact match for pair.

e.g.

public class fpoint : Tuple<double, double>
{
    public fpoint() : base(default(double), default(double)) { }
    public fpoint(double x, double y) : base(x, y) { }
}

Open in new window


The reason I disagree is that in C# you cannot reassign struct members once you have created the instance--you have to create a second instance with the new values. This is because KeyValuePair is a value type. A Tuple is a reference type, so you can reassign its member even after the instance is created. Albeit in either case there is no swap method, so you'd need to roll your own.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39996770
Actually, never mind. The Tuple's members are read-only also. I saw "tuple" mentioned in the documentation I linked to, so I naturally thought it would be the equivalent. I think either data structure would yield the same results.

Of course, you could always write your own class/struct to be an exact match!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question