|
Simply create the array of athletes and do something like this in the form's load event or constructor or in another method that you call from your Main method (i'll get to how to call the form from there in a minute)
this.label1.Text = "";
Athlete[] myArray = new Athelete[3];
for(int i =0; i < myArray.Length;i++)
{
label1.Text += myArray[i].ToString();
}
To call the form from your Main method you simply have to create the object and retain a reference to it by assigning it to a local variable:
Form1 myForm = new Form1();
Application.Run(myForm);
myForm.SomeMethod();
|
|
|
|
|
I tried to implement your suggestion as follows, and got this error:
An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.
Additional information: Object reference not set to an instance of an object
Do you know why? My form is called Person. Is that the problem?
Thank you.
private void Person_Load(object sender, System.EventArgs e)
{
this.label1.Text = "";
Person[] person = new Person[5];
person[0] = new Person(22, 5.6, 120, "Chase", 5671234, 2000);
person[0].printWindowsData();
#region
Person[] athlete = new Athlete[3];
athlete[1] = new Athlete("Martial Arts");
athlete[1].FirstName = "Kim";
athlete[1].LastName = "Chung";
athlete[1].Age = 21;
athlete[1].Height1 = 5.7;
athlete[1].Weight = 130;
athlete[1].BankName = "Citi";
athlete[1].BankAccount = 212312;
athlete[1].Balance = 300;
;
#endregion
for (int i = 0; i < athlete.Length; i++)
{
label1.Text += athlete[i].ToString();
//athlete[i].printWindowsData();
}
}
class TestClasses
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Person person = new Person();
Application.Run(person);
//person.printWindowsData();
// Console.ReadLine();
}
}
|
|
|
|
|
Now I see the following text in the label: windowsPracticingWithClasses.Athlete, Text: PersonwindowsPracticingWithClasses.Athlete, Text:
PersonwindowsPracticingWithClasses.Athlete, Text:
Person
Do you know what that might mean?
Person is the class name, as well as Athlete, windowsPracticingWithClasses is a namespace. The word "Text" repeats 3 times. And I have 3 elements in the array: Person[] athlete = new Athlete[3]; All 3 are populated.
Just changed the code to this:
for (int i = 0; i < athlete.Length; i++)
label1.Text += athlete[i].Text.ToString();
And now I see "PersonPersonPerson" in the label.
The same happens when I write
label1.Text += athlete[i].Text;
Here is the relevant code again:
Thanks again.
private void Person_Load(object sender, System.EventArgs e)
{
this.label1.Text = "";
Person[] athlete = new Athlete[3];
athlete[0] = new Athlete("Figure Skating");
athlete[0].FirstName = "Sarah";
athlete[0].LastName = "Hughes";
athlete[0].Age = 20;
athlete[0].Height1 = 5.5;
athlete[0].Weight = 120;
athlete[0].BankName = "Chase";
athlete[0].BankAccount = 434324;
athlete[0].Balance = 200;
athlete[1] = new Athlete("Martial Arts");
athlete[1].FirstName = "Kim";
athlete[1].LastName = "Chung";
athlete[1].Age = 21;
athlete[1].Height1 = 5.7;
athlete[1].Weight = 130;
athlete[1].BankName = "Citi";
athlete[1].BankAccount = 212312;
athlete[1].Balance = 300;
athlete[2] = new Athlete("Soccer");
athlete[2].FirstName = "Joe";
athlete[2].LastName = "Tribiani";
athlete[2].Age = 22;
athlete[2].Height1 = 5.6;
athlete[2].Weight = 140;
athlete[2].BankName = "Apple";
athlete[2].BankAccount = 123124;
athlete[2].Balance = 400;
#endregion
for (int i = 0; i < athlete.Length; i++)
label1.Text += athlete[i].ToString();
}
class TestClasses
{
[STAThread]
static void Main()
{
Person person = new Person();
Application.Run(person);
}
}
-- modified at 2:14 Wednesday 25th January, 2006
|
|
|
|
|
Why are you making an array of type Person? Make the array of type Athlete and don't make the type inherit from any forms. The default behaviour of ToString() is to basically convert the type to a string. You'll notice in my original example that I overrode ToString() in the Athlete class to provide something sensical. You should do the same.
public override string ToString()
{
return "This is a proper value that will override the default behaviour of System.Object.ToString()";
}
There is no need for you to create arrays of forms when working with a simple data structure. If you have no need for inheritance then simply your structure so that everything athlete related is in a new class called Athlete, then make an array of this type. Getting rid of the inheritance might help you get a handle on the difference between your UI (the form) and your data structures (the athlete). You should also create your data structures in another file. Add a new class file called Athletes.cs to your solution and put the athlete class in there.
|
|
|
|
|
Hi Garry, I still can't make it work. I change the code to the one below. And I got this exception: An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
:
It points to: person[0].Age = 22; - It all the way on the bottom. I tried so many different things, including putting all the class data in a different file. But first, I got the same exception as above, and second, the class didn't see the label. Thanks again for the help.
public class Person
{
//My instance variables
private string firstName, lastName;
private int age;
private double height, weight;
private string bankName;
private double bankAccount;
private double balance = 0;
private string squareColor;
//Properties
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public double Height1
{
get { return height; }
set { height = value; }
}
public double Weight
{
get { return weight; }
set { weight = value; }
}
public string BankName
{
get { return bankName; }
set { bankName = value; }
}
public double BankAccount
{
get { return bankAccount; }
set { bankAccount = value; }
}
public double Balance
{
get { return balance ; }
set { balance = value; }
}
public string SquareColor
{
get { return squareColor ; }
set { squareColor = value; }
}
//Custom constructor
public Person(int age, double height, double weight, string bankName, double bankAccount, double balance)
{
this.age = age;
this.height = height;
this.weight = weight;
this.bankName = bankName;
this.bankAccount = bankAccount;
this.balance = balance;
}
//Default constructor
public Person()
{
}
//My Methods
public void makeDeposit(int amount)
{
balance+= amount;
}
public void makeWithdrawal(int amount)
{
balance-= amount;
}
}
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 184);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
public class Test
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
Person[] person = new Person[3];
person[0].Age = 22;
}
};
}
|
|
|
|
|
Also label1.Text += myArray[i].ToString(); - returns WindowsApplication1.Person. However when I type label1.Text = athlete[0].Age.ToString(); It shows the age in the label. Do I have to it for each instance or there is a way around it?
|
|
|
|
|
As I said, override ToString() on the Athlete class to return something proper, and make the array an array of Athlete rather than Person. You don't want to have the form anywhere near your data structure.
|
|
|
|
|
Ok. Thank you very much for the help. I am new to C# so tips are very helpfull.
Albert
|
|
|
|
|
I tried to override ToString but it doesn't see label1 in this function. Do you know why? Also I remove the clas Athlete from the program there is only person class, and the form.
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
}
public class Person
{
public Person(int age, double height, double weight, string bankName, double bankAccount, double balance)
{
this.age = age;
this.height = height;
this.weight = weight;
this.bankName = bankName;
this.bankAccount = bankAccount;
this.balance = balance;
}
public override string ToString()
{
Person[] person = new Person[3];
person[0] = new Person();
person[0].Age = 22;
label1. -////// no properties shown
return "";
}
}
-- modified at 22:48 Wednesday 25th January, 2006
|
|
|
|
|
You just want ToString to return a string for that current element. You don't want it to create an array or anything like that.
I've modified your code as an example:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
Person[] arr = new Person[2];
arr[0] = new Person(12, 3.3, 3.3, "test", 3.3, 3.3);
arr[1] = new Person(15, 3.3, 3.3, "test", 3.3, 3.3);
this.label1.Text = "";
for (int i = 0; i < arr.Length; i++)
{
this.label1.Text += arr[i].ToString();
}
}
}
public class Person
{
public int age;
public double height;
public double weight;
public string bankName;
public double bankAccount;
public double balance;
public Person(int age, double height, double weight, string bankName, double bankAccount, double balance)
{
this.age = age;
this.height = height;
this.weight = weight;
this.bankName = bankName;
this.bankAccount = bankAccount;
this.balance = balance;
}
public override string ToString()
{
return this.Age;
}
}
|
|
|
|
|
Great!!! Thank you very much Garry. Now it works just as needed. Thanks for all the help.
Albert
|
|
|
|
|
Hello, HOw do you write a c# program to copy 2 files from one directory to another? I am going to have the files in the same directory as the program. Thanks in advance
|
|
|
|
|
You might want to look into the File.Copy[^] method.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
iam developing chat server /Client architecture and it works very very well on my localhost and even in Lan in cyber of internet . But it doesn't work on the internet i need to connect to someone running my server through knowing his ipaddress and the port my server listening on ?
Please solve this problem with me.
Mess With The Best And Die Like The Rest
|
|
|
|
|
Actually, your problem most likely has nothing to do with your code, but the setup of port forwarding on the your router on the server side. If you have a router anyway. I would suggest digging up the manual on whatever router/firewall/NAT thingy your using and looking up port forwarding.
Basically, you have to have the server on a static IP on the inside of your router. Then configure port forwarding to send packets destined for your server's chat server port on your Internet IP to the inside network IP and port.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
As the first poster mentioned, you'll need to configure any local routing infrastructure to allow inbound traffic, but you will also have to consider inbound/outbound traffic from your ISP's point of view.
Make sure that their hardware doesn't block the ports you are trying to use, and maybe check up to see if they have an isolated "proxy only" environment or something similar. This is when the only outbound connection you are allowed to make is through a proxy server, in which case you may have to to detect you default proxy of your machine (System.Net.WebProxy.GetDefaultProxy()), or create your own proxy object and wrap the packets you're sending out in an HTTP request. Most IRC clients and servers will now send and accept HTTP encapsulated data in this manner. Not sure about other protocols, though.
|
|
|
|
|
I'm a fairly new C# programmer, and not sure how to phrase this question. Perhaps you could point me to a project on this site that'll be close to what I'm looking for. here goes:
I would like to have codefiles (as part of a class or not) that are indicated to a main program by their filepath. Somewhat like how files and functions work in Matlab. For example, let's say I know for a particular condition (True/False) a user of my program will have to write their own C# boolean function. Since this function is to be stored in a separate codefile from the project/solution, my main program will find it by reading in an input XML file from the user. could the code be in the XML? if not, how could I read in their c# file and compile it with the main project at runtime.
Thanks in advance,
Matt
|
|
|
|
|
Hello my friend.
Try to look at System.Xml Namespace it has a full set of classes to manupilate any xml or schema file on this planet.
try first to load the xml file using
<br />
XmlDocument myxmlfile = new XmlDocument("the path for your file");<br />
<br />
//if you need the Help on these classes try searching msdn
http://msdn.microsoft.com
Mess With The Best And Die Like The Rest
|
|
|
|
|
well, there may be something there in the XML class but I don't think so. I use the XML namespace quite a bit, but this is different. I wanna say it's like stored procedures or compiling at runtime but I need some examples that are close.
What I'm actually writing is an optimization program (ala genetic algorithm) and I don't know what the user's objective function or constraints are, but I want him/her to indicate them to the program by the filepath of the custom C# files written by the user. These user functions will be called a lot so I don't want to continue reading it in each time I need to invoke it.
Any ideas?
Matt
|
|
|
|
|
Look at the Reflection name space (and the various load functions available from the Assembly class - at least I think it is on the assembly class, haven't used them for the last year or something like that).
These allows you to load a compiled DLL, find a method, and then call it.
Alternatively simply compile the C# program to an assembly, then call it though reflection (I can't remember the class name, but the C# compiler is available in the framework). Notice that you might have to think a bit about how to handle dependencies this way - after all, the C# code provided might use a DLL you did not anticipate.
|
|
|
|
|
Hi,
I am tryin to simulate a key sequence in a windows form ,I can simulate ctrl + c but I am tryin to simulate a break sequence which is ctrl + pause/break thats where I'm drawing a blank.
any suggestions will be appreciated
|
|
|
|
|
Is there a way to keep the text of a label from wrapping to the next line if it is too long. I would like to just have the text disappear at the end of the label. In other words force it to be only one line and if it doesn't fit, too bad.
Thanks for any suggestions.
Brent
Brent
|
|
|
|
|
Try to use TextBox .The TextBox Control has WordWrap property that can help in your case ...
DevIntelligence.com - My blog for .Net Developers
|
|
|
|
|
Hi!
you can try to override the OnPaint and use the DrawString method of the graphics parameter to redraw the text on your label.
hope that helps!
|
|
|
|
|
If you only need a single line of text/label, you can reduce them to minimum height.
|
|
|
|