Solved

Convert CPP to C#

Posted on 2014-04-12
6
542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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