DDoSAttack 0 Newbie Poster

the C# compiler requires a class have a constructor, you don't *have* to use it or even type one if you don't need it. The C# compiler will add a blank constructor with no parameters at compile time if you don't type any constructor. Constructors are good for initialising classes to a certain minimum state of readyness if you like, it is dependant on your project and what you are implementing.

You can have more than one constructor, all must have different signatures (different parameter lists) this allows consumers of your class to instantiate an instance of it in different states again this depends on how complex a class you are creating and the problem you are trying to solve.

To add to that...

Think of constructors as quite literally that: A method that constructs the class's insides when it is instantiated.
When a class is first called

ClassA myClass = new ClassA();

the very first thing the compiler does is run the constructor.

What I use them for is to setup my fields and what-not so that everything is ready when I need it. Here is an example...

Class A
{
    private string _myField;

    public A()
        {
            _myField = "Called Constructor!";

            MessageBox.Show(_myField.ToString());
        }
}

When you instiantate the class as I showed above (Class A myClass = new Class A) it will print out "Called Constructor!" when you run it!

What hollystyles is talking about when they mention having more than one constructor …

DDoSAttack 0 Newbie Poster

come on guys help me out i just dont want to do all the programming in visual c# and realise i should of create a database instead on management studio, is there really nobody that can help me

I would fully recommend C#/Visual Studio for what you are describing. It will handle what you want it for with no problems. It will handle all your db needs from within the IDE.

However, if you do decide to use Management Studio then remember that it doesn't handle some functionalities...

Note: SSMSE cannot manage SQL Server Analysis Services, Integration Services, Notification Services, Reporting Services, SQL Server Agent, or SQL Server 2005 Mobile Edition.{MSDN}

DDoSAttack 0 Newbie Poster

Does anyone know of anyplace/anybody that teaches C# at the highschool or college level to get students used to a programming language?

I don't know if you are looking for paid type things but here is a pretty decent place...
http://www.learnvisualstudio.net/

You can also find their stuff for free on MSDN, although I lost the exact links long ago you can cruise around and find good stuff.
http://msdn2.microsoft.com/en-us/vcsharp/default.aspx

Edit:: Almost forgot you can download a free IDE for C# (as well as all the other Visual Studio products) here...
http://msdn.microsoft.com/vstudio/express/visualcsharp/default.aspx

DDoSAttack 0 Newbie Poster

Do you have to use a Panel? If not, then the pictureBox control is more suited for that type of use.

Edit:: Also where exactly are you setting 'm_width' and 'm_heigth' [sic]

mostly because you spelled height wrong but also if you haven't defined them they won't exist hence it has no size.

DDoSAttack 0 Newbie Poster

Nothing is 100% fool proof, it depends on the fool! I would say go with pygmalion's solution, unless "know-it-all" iamthwee produces code.

Of course nothing is 100% foolproof, but I would still stick with iamthewee's solution.

The reason that his solution would work better (or rather more efficiently) is that it would be able to be applied to more variations of user input, i.e. somone copying and pasting text into the textBox. The other way you would have to check the user input twice. Once while they were typing and once after they invoke a completed inputting my input event (i.e. button click, image hover, etc...) to make sure they collected all the input.

iamthewee's solution covers an event like: user types the letters 'D', 'D' then pastes an 'o' then types an 'S'. Pygmalion's solution would only capture the 'DDS' and not the 'o', whereas iamthewee's would.

DDoSAttack 0 Newbie Poster

Ok so I came up with a workaround. I don't like it much. It heavily relies upon setting a fixed amount of "blank" labels to cover up the previously generated labels.

Basically, I created a refresh method that is called for each generate button click. Here's the code...

private void refreshTheForm(int loops)
        {
            int x = 26;
            int y = 58;

            for (int i = 0; i < loops; i++)
            {
                Label labelName = new Label();

                splitContainer1.Panel2.Controls.Add(labelName);

                labelName.Location = new Point(x, y);
                labelName.Name = "labelNum" + i.ToString();
                labelName.Size = new Size(25, 13);
                labelName.Text = ""; // Make the label text empty
                labelName.BringToFront();

                x += 25;
            }
        }
DDoSAttack 0 Newbie Poster

Are you placing the labels adjacent to one another then? Sorrie I don't have my c# compiler at the mo.

Yes, but not directly adjacent to one another.

I thought it was solved (and it sorta was). I now have it so that the text generated is on top of the previously generated text.

However, on a subsequent click, if you have requested an output that is less than the previous time (i.e. it produced and output 5 numbers then you choose 3) the former numbers (the original 2 numbers in the previous example) remain.

Sidenote: I am learning C#/VS 2005. My girlfriend comes up with little ideas and I am making them so that I can have experience. So this is not a critical thing per say.


Here are some screenshots to help visualize what is going on...

-------------------

Screenie #1:
http://www.flickr.com/photos/ddosattack/432761750/

As you can see there is no output yet. It will output 6 numbers as indicated in the upper left numericUpDown control

-------------------

Screenie #2:
http://www.flickr.com/photos/ddosattack/432761760/

So far so good! We dynamically created 6 label controls and output the text.

-------------------

Screenie #3:
http://www.flickr.com/photos/ddosattack/432761780/

AH HAH! As you can see, 3 of the original numbers (in blue) remain! That is no good :sad:
The label and text generation that was required on the click event was successful. It created 3 label controls and assigned …

DDoSAttack 0 Newbie Poster

Whoops!

The code should be...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;


            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Label labelName = new Label();
                Controls.Add(labelName);
                labelName.Size = new Size(25, 13);
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Text = theArray[ir].ToString();

                // Ah the elusive BringToFront() method!!!
                labelName.BringToFront();

                x += 30;
            }
DDoSAttack 0 Newbie Poster

Are you clearing the array before you generate the new numbers?

Yeah, I was. I wasn't at first but that was one of my steps and I caught it then (so it's kinda good that this happened lol)

Anyway, I figured out what to do!!! WOOT!

It was obvious too :eek:

I was forgetting about the BringToFront() Label method.

What was happening is that it would generate the number and display it. But it would do so underneath the existing displayed number, effectivly stacking them one on top of the other.
You add this property to your generated labels then it puts the new one on top, thus displaying it:mrgreen:

So the new code would look like...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;

            Label labelName = new Label();

            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Controls.Add(labelName);

                labelName.AutoSize = true;
                labelName.Size = new Size(13, 13);
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);

                // Ah the elusive BringToFront() method!!!
                labelName.BringToFront();

                // Set the text
                labelName.Text = theArray[ir].ToString();

                x += 30;
            }
DDoSAttack 0 Newbie Poster

_rOckbaer, Thanks for looking!

I don't think that is an issue. It works when I use the first code. Also when I output the first set of numbers (let's say 5 numbers) then change it so that it outputs 7 numbers it will not change the original 5 but show the other 2 numbers.

It is almost like the original numbers that are generated are on top of the subsequent numbers thus not showing them.
So I guess what I need is a way to redraw the form on button click event.

Here is the updated code I am using...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;
            Label labelName = new Label();
            //labelName.Size = new Size(25, 13);
            labelName.Height = 13;
            labelName.Width = 25;

            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Text = theArray[ir].ToString();

                Controls.Add(labelName);
                x += 30;
            }
DDoSAttack 0 Newbie Poster

Ok here is what I got...

After dealing with a bunch of other issues I had an idea. I figured I would try the Add method. This is what I came up with

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;
            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Label labelName = new Label();
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Height = 13;
                labelName.Width = 25;
                labelName.Text = theArray[ir].ToString();

                this.Controls.Add(labelName);
                x += 30;
            }

That worked great, unfortunatly now I have a new problem :sad:

When you click the button again to display the new set of numbers it doesn't display the new set. It only displays the first set of numbers.

I am sure it is something I am missing...
Any ideas?

DDoSAttack 0 Newbie Poster

Hello,

I am fairly new to C# so forgive me if this is a simple question but I cannot seem to find anything on this subject (I am proabably not searching for the correct thing)

How would you go about setting form control properties such as a succession of many label's text properties dynamically after an event trigger such as a button click?

For an example of a snippit of code I am toying with...

/*
             * The array is already filled with 7 random numbers from
             * a method called generateRandom()
             * 
             * This code works just fine.
             * I am trying to figure out how I can do this
             * without having to hard code the labels text output
             */

            int[] theArray;

            rng.generateRandom(out theArray);

            labelNum0.Text = theArray[0].ToString();
            labelNum1.Text = theArray[1].ToString();
            labelNum2.Text = theArray[2].ToString();
            labelNum3.Text = theArray[3].ToString();
            labelNum4.Text = theArray[4].ToString();
            labelNum5.Text = theArray[5].ToString();
            labelNum6.Text = theArray[6].ToString();

Instead of having to hard code the label's output, how would I go about doing it automatically?

Thank you in advance for any insight you can provide!

DDoSAttack 0 Newbie Poster

Thanks I will try this. As to the * 1, because it is coming out of a textbox, this insures that it is a neumeric value. I will let you know how it goes. Your way looks like it will save me a lot.

Ah I see.

Well then in that case you should use the try/catch statment to trap an error if one is present.

I modified the code to trap the error. Try this then...

public partial class Form1 : Form
    {
        /*
         * Fields
         * Here you set the private fields so that they cannot
         * be directly manipulated
         */
        private double _hour1010;
        private double _hour1011;
        private double _hour1020;
        private double _subTotal;

        /*
         * Properties
         * You set these to be manipulated by your program
         */
        public double Hour1010
        {
            get { return _hour1010; }
            set { _hour1010 = value; }
        }
        public double Hour1011
        {
            get { return _hour1011; }
            set { _hour1011 = value; }
        }
        public double Hour1020
        {
            get { return _hour1020; }
            set { _hour1020 = value; }
        }
        public double SubTotal
        {
            get { return _subTotal; }
            set { _subTotal = value; }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // Here we use the double.Parse() to convert
                // the textBox strings to numbers so we can use them
                Hour1010 = double.Parse(textBoxHour1010.Text);
                Hour1011 = double.Parse(textBoxHour1011.Text);
                Hour1020 = double.Parse(textBoxHour1020.Text);
            }
            catch
            {
                // Throw an error here if something didn't parse …
DDoSAttack 0 Newbie Poster

I am new so forgive me if I am making a stupid statment here...

I have no idea what language you are posting there, but in C# I would have written the same thing like this...

/*
      * You can declare and set the fields here
      * You also need to parse the string input
      * I don't know about using the decimal type I am used to using double
      */
     double subtotal10;
     double hour1010 = double.Parse(Hours1010TextBox.Text);
     double hour1011 = double.Parse(Hours1011TextBox.Text);
     double hour1020 = double.Parse(Hours1020TextBox.Text);

     // calculate hours
     // I have no idea why you multiply by 1, doing so results in the
     // same value... i.e. - 1 * 1 = 1
     // So I took it out
     subtotal10 = hour1010 + hour1011 + hour1020;

     // display hours
     // Now you need to take the double value and return it as a string
     // Use the ToString() method for that
     SubHours10TextBox.Text = subtotal10.ToString();

Hope that helps :cheesy:

DDoSAttack 0 Newbie Poster

That is because you are plotting a point right on top of the original point.

I changed this line...

objGraphic.DrawLine(pen, x, y, -x, -y);

Here is the code I used...

int picBoxWidth = pictureBox1.Size.Width;
            int picBoxHeight = pictureBox1.Size.Height;
            int halfWidth = pictureBox1.Size.Width / 2;
            int halfHeight = pictureBox1.Size.Height / 2;

            Graphics objGraphic = this.pictureBox1.CreateGraphics();

            Pen pen = new Pen(Color.Black);

            int b = 2;
            int m = 2;
            for (int x = 0; x < 426; x++)
            {
                int y = m * x + b;
                x = (y - b) / m;
                objGraphic.DrawLine(pen, x, y, -x, -y);
                System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);
            }

            objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
            objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);

Hope that helps :)