how to declare array of struct

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 20
Reputation: shadowrider is an unknown quantity at this point 
Solved Threads: 0
shadowrider's Avatar
shadowrider shadowrider is offline Offline
Newbie Poster

how to declare array of struct

 
0
  #1
Feb 19th, 2007
how to write a code for struct array
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to declare array of struct

 
0
  #2
Feb 19th, 2007
Originally Posted by shadowrider View Post
how to write a code for struct array
google?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 173
Reputation: RwCC is an unknown quantity at this point 
Solved Threads: 4
RwCC's Avatar
RwCC RwCC is offline Offline
Junior Poster

Re: how to declare array of struct

 
0
  #3
Feb 20th, 2007
Would be along these lines:

  1. public struct MyStruct
  2.  
  3. {
  4.  
  5. public string text;
  6.  
  7. public string text2;
  8.  
  9. public int number;
  10.  
  11. }

http://www.codeproject.com/useritems/InterOp.asp
http://www.java2s.com/Code/CSharp/Co...ture/Array.htm

Some basic information
Last edited by RwCC; Feb 20th, 2007 at 8:03 am.
Sir David Healy - Northern Ireland Goal King
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: how to declare array of struct

 
1
  #4
Feb 21st, 2007
Hey Shadowwrider,

<code>
ArrayList ar = new ArrayList();
object[] myarray = new object[2];
</code>

ArrayList comes from the System.Collection name space. I find it very useful, and will show you how to use the array or arraylist. Above I declare both for your consideration. Notice that for the standard array, you need to declare it as an array of objects.

In this example we have a very simple struct:
<code>
public struct MyStruct
{
public int t1;
public string s1;
}
</code>

Here is a Simple example using some Console writing:
<code>
ArrayList ar = new ArrayList();
object[] myarray = new object[2];
// First get an instance of the struct
MyStruct thisjoe;
thisjoe.t1 = 1;
thisjoe.s1 = "Hello";
myarray[0] = thisjoe; // Add to the Array
ar.Add(thisjoe); // OR add to the ArrayList

// Do another one
thisjoe.t1 = 2;
thisjoe.s1 = "World";
ar.Add(thisjoe);
myarray[1] = thisjoe;

// How to get a value ? Lets put one into (x)
int x = ((MyStruct)ar[0]).t1;
Console.WriteLine("X={0}", x);

// Lets go through the ArrayList
foreach (MyStruct myjoe in ar)
{
Console.WriteLine("{0}:{1}", myjoe.s1, myjoe.t1);
}

// Lets go through the standard Array
foreach (MyStruct myjoe in myarray)
{
Console.WriteLine("{0}:{1}", myjoe.s1, myjoe.t1);
}
</code>

Hope this Helps,
Jerry
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: abhisek.verma is an unknown quantity at this point 
Solved Threads: 0
abhisek.verma abhisek.verma is offline Offline
Newbie Poster

Re: how to declare array of struct

 
0
  #5
Feb 26th, 2007
package JavaProject2;
//import java.util.Scanner;
public class Student1
{ static int[][] arr = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};


public static void main(String args[][])
{
for(int i=0;i < 3;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(arr[i][j]);
}
}



}
}






/*
String y;
String[][] NR={};
Scanner raw=new Scanner(System.in);
String stu[]={"Abhishek","Ranjeet","Bibhakar","Pradeep","Rahul","Washim","Siraj","Prity","lovely","Suchitra"};
String var,var1;
int i,j,k=0;
public void Name (String var1)
{
for (i=0;i<stu.length;i++)
var1=var1+var1+"\n";
}

public void fees()
{
for (i=0;i<stu.length;i++)
{ System.out.println(stu[i]);
var=raw.next();
//System.out.println(var);
if (var.matches("y"))
{

NR[i][k]=stu[i]+" Paid"+"\n";
k++;
}
}
for (i=0;i<30;i++)
System.out.println("\n");
for (i=0;i<10;i++)
{
for (j=0;j<1;j++)
System.out.println(NR[i][k]=stu[i]+" Paid"+"\n");
}
//System.out.println(var1);
}
}

*/
http://www.danwed.com
Abhishek Verma
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to declare array of struct

 
0
  #6
Feb 26th, 2007
Originally Posted by abhisek.verma View Post
package JavaProject2;
//import java.util.Scanner;
public class Student1
{ static int[][] arr = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};


public static void main(String args[][])
{
for(int i=0;i < 3;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(arr[i][j]);
}
}



}
}






/*
String y;
String[][] NR={};
Scanner raw=new Scanner(System.in);
String stu[]={"Abhishek","Ranjeet","Bibhakar","Pradeep","Rahul","Washim","Siraj","Prity","lovely","Suchitra"};
String var,var1;
int i,j,k=0;
public void Name (String var1)
{
for (i=0;i<stu.length;i++)
var1=var1+var1+"\n";
}

public void fees()
{
for (i=0;i<stu.length;i++)
{ System.out.println(stu[i]);
var=raw.next();
//System.out.println(var);
if (var.matches("y"))
{

NR[i][k]=stu[i]+" Paid"+"\n";
k++;
}
}
for (i=0;i<30;i++)
System.out.println("\n");
for (i=0;i<10;i++)
{
for (j=0;j<1;j++)
System.out.println(NR[i][k]=stu[i]+" Paid"+"\n");
}
//System.out.println(var1);
}
}

*/
http://www.danwed.com
This isn't java. Next time use code tags.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC