> I'm learning C# from a C/C++ background, and I came across an odd
> problem.
>
> I have a hashtable that holds a string and a struct like so:
<snip>
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
> Hello all,
> I'm learning C# from a C/C++ background, and I came across an odd
> problem.
>
> I have a hashtable that holds a string and a struct like so:
From your definition of MyData below, you have a class, not a struct.
class and struct have very different behavior in C#.
You will most likely want to keep this a class.
> private Hashtable MyTable = new Hashtable();
>
[quoted text clipped - 12 lines]
>
> MyData DataPoint;
DataPoint is an unassigned reference (null)
Try
MyData DataPoint = new MyData(0,0,0);
> MyData.Add[SomeString,DataPoint];
>
[quoted text clipped - 4 lines]
>
> MyData D = (MyData)DataPoint.Value;
This is probably where it is bombing.
- DataPoint is null
- MyData has no Value members
> D.x = 10;
> }
>
> When I try to access the members of D, I get errors. Why?
> Can't I use a hashtable to store struct or class elements like a C++
> map?
It is generally a bad Idea to have public fields in your classes.
Try making the field private and adding public properties for set/get
functionality (if needed)
Many new C# developers that come from a C++ background trip all over
structs.
They should not be treated as lightweight classes the way they generally
are in C++.
Structs are very different critters from classes in C# and they have
many subtle gotchas.
I recommend sticking with classes until you understand all of the
differences.
Good luck
Bill