array of struct

G

Guest

I am translating C++ to C# coding. I have given array of struct in C++, is
there any good alternatives for "array of struct" in c#? I hope anyone can
help. Thank you very much.
 
C

Chris Priede

Hi,
I am translating C++ to C# coding. I have given array of
struct in C++, is there any good alternatives for "array
of struct" in c#?

There is nothing that prevents you from having just that -- an array of
struct -- in C#, in case you were under impression that couldn't be done.

If you are asking whether there is a better way, then an excerpt from the
original C++ code would be helpful.
 
G

Guest

MyStruct[] structArray = new MyStruct[] {mystruct1, mystruct2, ....};

--is this what you are referring to? You could use an ArrayList, a Hashtable,
-- it all depends what the business logic is that you want.
Peter
 
G

Guest

Hi,

Thanks for your reply. I want to implement list of struct.

using System;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class MrcPunct
{
public struct PunctSf
{
public char code;
public string punct;
public PunctSf(char co, string pun)
{
code = co;
punct = pun;
}
}

public List<PunctSf> sf100 = new List<PunctSf>{new PunctSf('b', ","),new
PunctSf('c', ",")};
}

Can you tell me what's wrong in this code? I can't even get it compile.
When I compile, I got this error.

"A new expression requires () or [] after type"

in this line
public List<PunctSf> sf100 = new List<PunctSf>{new PunctSf('b', ","),new
PunctSf('c', ",")};

I hope anyone can give me suggestion. thank you very much.

Mel
 
A

Alexander Kolliopoulos

An array is not the same as a generic list. You cannot use { } when you
instantiate a list.
 
G

Guest

Hi Alex,

How can I declare the struct list in the same time I can initialise it?

Hope you can answer my question. Thank you

Mel
 
G

Guest

Mel said:
Hi Alex,

How can I declare the struct list in the same time I can initialise it?
<snip>

List<PunctSf> sf100 = new List<PunctSf>(
new PunctSf[] {
new PunctSf('b', ","),
new PunctSf('c', ",")
});

Untested but should work. Note that this will construct an array first,
then provide that array to the List<T> constructor to use for
initializing the list with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top