1

i wanna make a callLog with struct in C# so i remember in C we had such a thing:

struct contact
{
string name;
string phone;
}contact_q[200]

and for filling the contacts's information we had such a thing:

while(i<200)
{
 scanf("%s",student_q[i].name)
}

so in C# we don't have that declaration:"contact-q[200]" at the end of struct and i could not handle a loop for filling name and phone because we don't have such a thing:

for (int i = 0; i < 10; i++)
            {
                Contacts contact[i]=new Contacts();
            }

it has error with : contact[i]

so help me

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Navid
  • 11
  • 1
  • 6

3 Answers3

1

For a solution in C# you've been really close to the correct syntax. The problematic part in your attempt was the assignment in the loop.

Do the following and your code should compile and work:

Contacts[] contacts = new Contacts[10];
for (int i = 0; i < contacts.Length; i++)
{
     contacts[i] = new Contacts();
}

Please note that the name of your struct (Contacts) is somewhat unfortunate. It represents a single contact but its name implies it'd contain multiple contacts. So, I'd rename you C#-struct from Contacts to Contact (as you have done in your C++ sample, too).

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • 1
    @Navid If you feel this answer helped solved your issue, please mark it as 'accepted' by clicking the green check mark. This will help the community to keep the focus on unanswered questions. – Lahiru Jayaratne Jun 22 '16 at 03:52
0

In C# first declare the array

 fixed Contacts contact[10];  // fixed array

or

 Contacts contract[] = new Contracts[10];  // dynamic array

then in the loop

 contact[i]=new Contacts();

Your memory of C is slightly wrong BTW it was actually

typedef struct contact
{
  string name;
  string phone;
} contact_q[200]

The typedef is important -- is is creating a type (contact_q)

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • would u plz explain more? – Navid Jun 18 '16 at 11:05
  • 1
    @Navid -- nothing to explain, this is how you declare and use arrays in C++ Look at any basic C++ textbook on arrays. – Hogan Jun 18 '16 at 11:06
  • i tried it but is has error here : Contacts contact[10]; – Navid Jun 18 '16 at 11:09
  • errors:Error 4 Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. C:\Users\NAVID-PC\Desktop\CallLog\CallLog\Program.cs 20 24 CallLog – Navid Jun 18 '16 at 11:11
  • Error 5 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Users\NAVID-PC\Desktop\CallLog\CallLog\Program.cs 20 25 CallLog – Navid Jun 18 '16 at 11:12
  • @Hogan I think you mean C#, not C++ in your first comment. – juanchopanza Jun 18 '16 at 11:15
  • @juanchopanza - yes I did. – Hogan Jun 18 '16 at 11:16
  • 1
    Just to be clear: The C `typedef` declares the *type* `contact_q` to be an ***array*** of 200 `struct contact`. It does *not* define any variable. This is different form the OP's snippet which defines `contact_q` to actually *be* the array *variable*. – alk Jun 18 '16 at 12:11
  • @alk I was under the impression the OP was saying the struct snippet was C code not C# code. – Hogan Jun 18 '16 at 13:01
  • I am as well referering to C only, to your C snippet, as well as the OP's. – alk Jun 18 '16 at 13:05
  • @alk -- oh I see.. you are explaining how C and C# are different. Of course you are correct. – Hogan Jun 18 '16 at 20:47
0

You want an array of structs. What probably works is

struct contact {
  string name;
  string phone;
}

struct contact contact_q[200];

and then simply assigning

contact_q[i].name=...

or with your while loop. You can also typedef:

typedef struct contact {
  string name;
  string phone;
} contact_it;

contact_it contact_q[200];

See, e.g.,

How do you make an array of structs in C?

Community
  • 1
  • 1