| | |
how to declare array of struct
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
Would be along these lines:
http://www.codeproject.com/useritems/InterOp.asp
http://www.java2s.com/Code/CSharp/Co...ture/Array.htm
Some basic information
C# Syntax (Toggle Plain Text)
public struct MyStruct { public string text; public string text2; public int number; }
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
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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
<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
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 0
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
//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
•
•
•
•
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
*Voted best profile in the world*
![]() |
Similar Threads
- 2D dynamic array within struct (C++)
- Array in struct (C#)
- Help with copying an array into a struct (C)
Other Threads in the C# Forum
- Previous Thread: databinding
- Next Thread: circle button
| Thread Tools | Search this Thread |
.net access activedirectory ado.net algorithm array barchart bitmap box broadcast c# check checkbox client combobox contorl control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment development disabled displayingopenforms draganddrop drawing editing editor encryption enum event excel file form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox mandelbrot math mathematics mouseclick mysql operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox rows server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation visualstudio webbrowser windows winforms wpf xml






