wildo

Hello; I'm trying to learn more about C# by porting some old C/C++ code. One of the data structures I'm trying to implement correctly is a small dictionary made up of class elements as follows

namespace myProg

{

class dictionary

{

public enum VALUETYPES : byte

{

AE = 0, /* 0 - Entity */

AS, /* 1 - String */

AT, /* 2 - Tag */

... etc

}

class vw_dictentry

{

ushort group;

ushort element;

VALUETYPE VR;

}

vw_dictentry[] dict = new vw_dictentry[] {

{0x0000, 0x0000, AE},

{0x0000, 0x0001, AS},

{0x0000, 0x0002, AT},

... etc

}

Unfortunately when attempting to initialize my VALUETYPES enumerations cannot be assigned. Why not How should this be done. Using enums is preferrable The VALUETYPES name can also be useful to me.

- wildo

C:\fun\sharp\app\dict.cs(699,30): error CS0103: The name 'VR_DATA' does not exist in the current context

C:\fun\sharp\app\dict.cs(700,13): error CS0623: Array initializers can only be used in a variable or field initializer.




Re: Visual C# Language initializing structures in an array with enums

OmegaMan

The problem with porting over code, or learning as mentioned is that one tends to brings paradigms that worked in the old language into the new language. These forums are littered with posts of people saying, "but I could do it in language X but not in .Net!" They tend to believe that .Net should behave the same...it just doesn't. One needs to learn the powers of .Net over the other languages and use the new paradigms.

For example below is using a dictionary object, in the .Net way similar to what your code did. First step is to bring over the enum and create a struct to handle it.


public enum VALUETYPES : byte
{

AE = 0, /* 0 - Entity */
AS, /* 1 - String */
AT, /* 2 - Tag */

}

public struct ClassElement
{
public ClassElement(VALUETYPES _vr, ushort _group, ushort _element)
{
VR = _vr;
Group = _group;
Element = _element;
}

public override string ToString()
{
return string.Format("VR ({0}) Group ({1}) Element ({2})", VR.ToString(), Group, Element);
}

public VALUETYPES VR;
ushort Group;
ushort Element;

}



The next step is to create a dictionary object which will contain our struct, load one and the print it out.


Dictionary<int, ClassElement> Elements = new Dictionary<int, ClassElement>();

ClassElement ce = new ClassElement(VALUETYPES.AE, 1, 1);

Elements.Add(ce.GetHashCode(), ce);

foreach (KeyValuePair<int, ClassElement> item in Elements)
Console.WriteLine("Key ({0}) Value ({1})", item.Key, item.Value.ToString());



The output looks like this


Key (9515624) Value (VR (AE) Group (1) Element (1))





Re: Visual C# Language initializing structures in an array with enums

wildo

Omega Man :

You are reading my mind. The purpose of my exercise is to completely re-write my old code using as much of the power of C# and .Net that I can muster. My only requirement is that all of my code is managed. I hope to greatly reduce my coding effort and I want to proove (to myself mostly) that I can reach the same performance level I already have using c and c++ - or at least demonstrate that my program works just as well. So far I have been greatly impressed. Thanks for your great direction and thanks for the links to the examples.

Next - figure out how to post code.

wildo






Re: Visual C# Language initializing structures in an array with enums

OmegaMan

My background is C to C++ to C# and have done the same learning curve. From C++ I miss the ability to create a list of pointers to functions without having to initialize them as with the Delegates in C#...but other than that I find C# is a natural progression from C/C++ and is quite powerful.

The forums are one of the best places to learn. Keep your posts simple and to one topic and that will get the best responses.

Also when you feel that you have an answer mark the post(s) that are helpful as the answer, so when others search the forums, they might be more inclined to look at a successful post than a non successful one...in the search results the Answered posts are bubbled to the top before the unanswered.

As to how to post code, see this post How to Post Code Samples for some info.