943,166 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 56624
  • C# RSS
Feb 19th, 2007
-1

how to declare array of struct

Expand Post »
how to write a code for struct array
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shadowrider is offline Offline
20 posts
since Feb 2007
Feb 19th, 2007
0

Re: how to declare array of struct

how to write a code for struct array
google?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 20th, 2007
0

Re: how to declare array of struct

Would be along these lines:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 70
Solved Threads: 4
Junior Poster
RwCC is offline Offline
173 posts
since Jan 2006
Feb 21st, 2007
2

Re: how to declare array of struct

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Feb 26th, 2007
0

Re: how to declare array of struct

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhisek.verma is offline Offline
3 posts
since Feb 2007
Feb 26th, 2007
0

Re: how to declare array of struct

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: databinding
Next Thread in C# Forum Timeline: circle button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC