2

I hope this is not a silly question but I'm trying to create a simple form which has 2 numeric updowns with a button I take the values and store them in a dictionary as two strings. Now I think this goes fine but I want to show the values after pressing the button also in separated textboxes.

namespace TestWayPointEditor
{
    public struct Coordinate
    {
    public string Latitude { get; set; }
    public string Longitude { get; set; }

    public Coordinate(string latitude, string longitude)
    {
    this.Latitude = latitude;
    this.Longitude = longitude;
    }

}
public partial class Form1 : Form
    {

    HashSet<Coordinate> WayPoints = new HashSet<Coordinate>();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        //Set lat and lon values in a string
        string Lat = LatUpDown.Value.ToString();
        string Lon = LongUpDown.Value.ToString();

        WayPoints.Add(new Coordinate(Lat, Lon));

    }
}
}

enter image description here My form looks like this to give an idea of what I am doing

So if I have a latitude and longitude and press add it should appear in the textboxes and save it on the background. If I change the values and press add the second row should be filled and this should go on and on. Besides of that I want when I close this form (it is a second form of project) and open it again I did not loose my values.

I gues I have to make a class for this and it should be public. But I actually dont know where to start. I hope someone could guide.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
ElectricRay81
  • 121
  • 11
  • 5
    posted Image in link is blank – Lucifer Jun 03 '18 at 08:20
  • See [here](https://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us) for serializing (saving) a dictionary. The display in a textboxes should be trivial.. – TaW Jun 03 '18 at 08:25
  • 2
    I think the dictionary is not the best choice for your problem, since you will not be able to save more than one same key "latitude". You can instead use list of objects consisting two strings. – L_J Jun 03 '18 at 08:36

1 Answers1

3

The Dictionary is better for data with key-value structure when we might need to get the value using the key...

Obviously latitude isn't a key for longitude therefore the Dictionary collection isn't the best choice here unless the key would contain something meaningful.

We could create a custom struct Coordinate to store our values. An example of structure like this is System.Drawing.Point.

public struct Coordinate
{
    public string Latitude  { get; } 
    public string Longitude { get; }

    public Coordinate(string latitude, string longitude)
    {
        this.Latitude = latitude;
        this.Longitude = longitude;
    }
}

As we don't want our collection to allow adding the same value twice the List<T> isn't the best choice either.

We can benefit from using the HashSet<T> which has high-performance set operations and contains no duplicate elements:

HashSet<Coordinate> WayPoints = new HashSet<Coordinate>();

public Form1()
{
    InitializeComponent();
}

...

WayPoints.Add(new Coordinate(.. , ..));

Now if we need WayPoints to exist even after the form is closed we have to move it outside of the Form into some other object.

If WayPoints collection should have values after we restart the app - a use of persistent storage is required and we could serialize and save our values into database or into file.

An example of a very simple static storage:

public static class DataStorage
{
    public static HashSet<Coordinate> WayPoints { get; }

    static DataStorage()
    {
        WayPoints = new HashSet<Coordinate>();
    }

    public static Coordinate? TryGetCoordinate(string latitude, string longitude)
    {
        var coordinate = new Coordinate(latitude, longitude);
        return WayPoints.Contains(coordinate) ? (Coordinate?)coordinate : null;
    } 
}

P.S.

We could loop through all WayPoints using foreach and assign each coordinate to some Textbox.Text.

In case when we need to get certain Coordinate from WayPoints all we need to do is to create a new instance of Coordinate and check if it exists in WayPoints collection. We've implemented it in the method TryGetCoordinate.

Use it like so:

Coordinate? foundInStorage = DataStorage.TryGetCoordinate("123.5", "300");
if(foundInStorage != null)
{
    // something with foundInStorage.Value
}
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • Thanks for the help I have applied the suggestion and it seems to work up to the point where i try to get some info out of the HashSet for example: label3.Text = WayPoints.Count Or i try to read out a particular value I would like to place a latitude in 1 txtbox and longitude in a textbox next to it. I took a look on MSDN but didnt tell me what I needed I thought this would do the job: TxtLat1.Text = WayPoints.ToString() – ElectricRay81 Jun 03 '18 at 09:43
  • @ElectricRay81 Updated the answer with few examples – Fabjan Jun 03 '18 at 10:33
  • Ohh sorry just see the comment regarding the example I will try thanks a lot Fabjan! – ElectricRay81 Jun 03 '18 at 10:45
  • I would suggest you to try to compile `return WayPoints.Contains(coordinate) ? coordinate : null;`... Hint: a `struct` cannot be `null` – Camilo Terevinto Jun 03 '18 at 10:48
  • I'm lost I got only red lines LOL – ElectricRay81 Jun 03 '18 at 11:02
  • But I dont want to find anything I just want to place Latitude in one textbox and longitude on a another after pressing the add button. Than if I press another time I want the new latitude and longitude values appear in two textboxes below it. The idea was to create a table of values which I make with the numericUpDown. Than by pressing adding it gets stored in the list. Like this I would be able to create a table of waypoints. After that I wanted to make something so I can scroll thru the table. But I dont see how this is all possible with the HashSet. – ElectricRay81 Jun 03 '18 at 11:12
  • @Fabjan I have updated my code as I have now in the original question. If I add your latest piece of code I get red lines – ElectricRay81 Jun 03 '18 at 11:22
  • @ElectricRay81. `WayPoints` *is* a table of waypoints and to 'scroll through all of them' you could use the `foreach` loop to iterate through this collection. Code example that I gave you should be enough to figure this out and yes it compiles and works. Note that I've used `.NET 4.6.1` and `C#6.0`, example for older versions is [here](https://dotnetfiddle.net/HyV3GI) – Fabjan Jun 03 '18 at 11:36
  • Ok I struggle on as you can see I'm pretty new and still learning a lot so probably its due to my lack of knowledge. Thanks for all the help – ElectricRay81 Jun 03 '18 at 14:14
  • @ElectricRay81 Good luck :) – Fabjan Jun 03 '18 at 14:19