|
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.
|
|
|
|
|
This was good enough... Changing the TextAlign property to TopLeft and setting the height small enough to not show the next line worked for what I need it for.
Using text boxes just doesn't work and look the same as using labels. And I don't have the time to create my own custom controls right now.
Thanks
Brent
|
|
|
|
|
public class NewMonitorDetails<br />
{<br />
<br />
public static int MonNumber;<br />
public static string EDID_Info; <br />
<br />
public NewMonitorDetails(){}<br />
}
I have the above class...I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want.
How do I do this. Thanks.
|
|
|
|
|
Manu_81 wrote: I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want
First, if you declare the fields as static you will only get one for all instances of the class. So, if you want to create multiple instances of the class you cannot create these as static for the purpose you want.
Second, declaring a field as public is bad form. You should declare them as private or protected and access them through properties. Read this article if you want to know why you should make the fields private and access them through a public property[^]
Finally, you can make the array global by adding it as a static field to the class (because you only want one instance of the array made available globally across your application (right?)
So your class will look something like this:
public class NewMonitorDetails
{
private static ArrayList newMonitorDetailsArray = new ArrayList();
private int monNumber;
private string edid_Info;
public NewMonitorDetails()
{
}
public int MonNumber
{
get { return this.monNumber; }
set { this.monNumber = value; }
}
public string EDID_Info
{
get { return this.edid_Info; }
set { this.edid_Info = value; }
}
public static ArrayList NewMonitorDetailsArray
{
get { return NewMonitorDetails.newMonitorDetailsArray; }
}
}
Does this help?
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
-- modified at 4:19 Tuesday 24th January, 2006
|
|
|
|
|
Thanks a lot for your reply Colin. Its really helpful as I am new to obj. oriented...
Got what you r saying.
Now how do I set and retrieve values to this class.
What I am doing exactly is I get the Monitor number and EDID info for each active Monitors connected from dll. So I want store this pair of values for each monitors in some global data structure so that I can use it where ever I want in my app...
Thanks again.
|
|
|
|