From List<class> to string[] at Once

patrick

Well-known member
Joined
Dec 5, 2021
Messages
259
Programming Experience
1-3
Class:
public  class Info
{
  public string Id;
  public string Name;
  public string Label;
  public string ACC;
}

C#:
List<info> val = new List<info>();

val.Add("Id");
val.Add("Name");
val.Add("Label");
val.Add("ACC");

In class info, Select Name, ACC from List and change it to string[] at Once.

Change List to string[] at Once.
 
Last edited:
I don't understand the question. It's a List<Info>, not a List<string> so how do you expect it to become a string array? You need to explain exactly how you expect to get a string from an Info object. Do you just want one of the property values? If so, which one? Do you want some combination of the two values? If so, what? You need to explain. ALWAYS provide a FULL and CLEAR explanation of the problem.
 
I don't understand the question. It's a List<Info>, not a List<string> so how do you expect it to become a string array? You need to explain exactly how you expect to get a string from an Info object. Do you just want one of the property values? If so, which one? Do you want some combination of the two values? If so, what? You need to explain. ALWAYS provide a FULL and CLEAR explanation of the problem.
I rewrote the question in detail.
 
C#:
public  class Info
{
  public string Id;
  public string Name;
  public string Label;
  public string ACC;
  public string[] ToStringArray() => new[]{Id,Name,Label,ACC};
}

Or in old money public string[] ToStringArray() { return new string[]{Id,Name,Label,ACC}; }
Advise to use properties, not fields, for your public members
 
Last edited:
I rewrote the question in detail.
Please do not go back and back edit your original post. This is a regular chronological forum. It is not StackOverflow where you can, and often are expected to, massage your original question based on responses and comments. It seems like your fellow countryman @Anwind also has that habit of back editing previous posts.
 
@patrick: There is no way for your initial part of your request to even work.

Given the class:
C#:
public  class Info
{
  public string Id;
  public string Name;
  public string Label;
  public string ACC;
}

there is no way that the following will code will compile and run:
C#:
List<info> val = new List<info>();

val.Add("Id");
val.Add("Name");
val.Add("Label");
val.Add("ACC");

On the other hand this would compile and run:
C#:
List<info> val = new List<info>();
val.Add( new Info() { Id = "id", Name = "patrick", Label = "poster", ACC = "aeiou" } );

And then one you do that, then you can use @cjard 's code from post #4 to do something like:
C#:
Info firstInfo = val[0];
string [] stringArray = firstInfo.ToStringArray();
 
Then do:
C#:
val.Add( new Info() { Id = "id", Name = "Name", Label = "Label", ACC = "ACC" } );
 
Back
Top Bottom