Page 1 of 1

Some C# Questions

Posted: Fri Jul 13, 2007 12:39 pm
by WaywornMmmmm
I have some more questions.
Is there a way to have c# to check if a variable is a perfect square?

Also, is there a way to add a new form to my project which will be an MDIContainer for the original and any new forms I may add?

Also, this has been edited so some responses don't flow with these questions.

Thanks for any help.

Posted: Fri Jul 13, 2007 2:18 pm
by [cc]z@nd!
i've never seen a lick of C# code in my life, but in C++ it would look something like this:

Code: Select all

#include <iostream> /*for input/output stream support. we need this to use cin for the input.*/

int main()
{
    int input; //the int variable the user's input will be stored in.
    std::cout << "\nenter the numbers: "; //the output to tell the user what to do
    std::cin >> input; //cin takes what they type in and stores it in input

    /*note: when cin encounters a space, it stops reading from the stream. for a single number, that shouldn't be a problem, but it causes issues when you get into strings...*/

    return(0); //end the program, and return 0 (everything's fine)
}

Posted: Fri Jul 13, 2007 2:27 pm
by OwnZ joO

Code: Select all

int variable = 0; // initialize your variable
Convert.ToInt32(TextBox1.Text); // textbox.text returns a string, so convert it to an int

Posted: Fri Jul 13, 2007 2:30 pm
by Prey

Code: Select all

int num1; // For storing the integer (whole number)

if (int.TryParse(textBox1.Text, out num1))
{

// If we're here that means the text could be converted to an integer, and so num1 will now be that variable integer

}
else
{

// Couldn't convert

}
I can't think of anything better than that for doing what you want.

Posted: Fri Jul 13, 2007 3:41 pm
by WaywornMmmmm
I tried your's prey, but I get an error saying int doesn't contain a deffinition for TryParse.

I also tried OwnZ joO's but I don't really understand it.

Remeber that I am very new to this so explanation in great detail will help.

Edit: After searching google for a while, I found a solution that works for me.

Now I have a new question. Is it possible to make the datatype of a variable that is a square root not a double?

Basicly, can I get somthing like this

Code: Select all

int variable = (Math.Sqrt(otherVariable));

Posted: Fri Jul 13, 2007 6:07 pm
by dos mes

Code: Select all

int variable = (Math.Sqrt(otherVariable));
To:

Code: Select all

int variable = (int)Math.Sqrt(yourDouble);
There is also the "Convert.To...()" function which is useful for many different conversions.

Posted: Fri Jul 13, 2007 6:50 pm
by WaywornMmmmm
Thank you so much.

I haven't seen you around here in a while dos. Doom told me you died. :oops:

Posted: Sat Jul 14, 2007 2:52 am
by Prey
Image

Me wonders what C# you are using :/

Posted: Sat Jul 14, 2007 4:08 pm
by LuxuriousMeat
If you can't get TryParse to work do this:

Code: Select all

int num = 0;
try
{
    // parse the int
    num = int.Parse(textBox1.Text);
}
catch
{
    // if it reaches here the text cant be parsed to an int
}

Posted: Mon Jul 16, 2007 10:18 am
by WaywornMmmmm
I updated my first post with more questions. Please help.

Re: Some C# Questions

Posted: Mon Jul 16, 2007 10:27 am
by Prey
WaywornMmmmm wrote:Is there a way to have c# to check if a variable is a perfect square?
Not sure what your asking here, but the 'Math.' does have quite a few useful functions which may be what your looking for.
WaywornMmmmm wrote:Also, is there a way to add a new form to my project which will be an MDIContainer for the original and any new forms I may add?
You can make a form a MDIContainer by enabing the 'IsMdiContainer' in the forms properties, then to add a form to the container:

Code: Select all

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.MdiParent = this;
            form2.Show();
        }

Re: Some C# Questions

Posted: Mon Jul 16, 2007 11:18 am
by WaywornMmmmm
Prey wrote:
WaywornMmmmm wrote:Is there a way to have c# to check if a variable is a perfect square?
Not sure what your asking here, but the 'Math.' does have quite a few useful functions which may be what your looking for.
What I am trying to do is have C# check to see if my variable is a perfect square (4,9,16,25,36, etc.) and if not display a message box with that number. I know how to do the latter, but I am not sure how to do the first part.

Also, when I try to make the form I added to my project an MDIContainer with your code, I get an error saying that my form can't be both and MDIParent and an MDIContainer

What I did was add my form, set its Is MdiContainer property to true, then went the the load event of my original form and entered in the code you gave me.
:?

Posted: Mon Jul 16, 2007 12:41 pm
by dos mes
For your square root problem here's a possible solution.(note: its sloppy and there's probably better ways to do this but it works...)

Code: Select all

double d = # you want to check;
long i = (long)Math.Sqrt(d); // you want to convert the sqrt to a value that does not hold decimals. ie: integers, longs, etc...
if ((i * i) == d)
{
    // it's a square #
}
else
{
    // it's not a square #
}

Re: Some C# Questions

Posted: Mon Jul 16, 2007 1:46 pm
by Prey
For a MdiContainer
1) Go to the properties of the form you wish to be a container.
2) Enable 'IsMdiContainer'.
3) Now go into the containers load event. Assuming the container form is 'Form1' and the form you want to add is 'Form2'; the code i posted above will work just fine.

As for finding out if the number is a perfect square, dos mes' way would work, but here is a faster alternative:

Code: Select all

double d = 13; // The squared num
double sqrt = Math.Sqrt(d); // The square root of the num

if (sqrt == Math.Round(sqrt))
{
   // No rounding was done, thus the number is a whole
}
else
{
   // Rounding was done, thus the number wasn't a whole
}

Posted: Mon Jul 16, 2007 2:18 pm
by WaywornMmmmm
When I input your code into the load event for the MdiContainer, with the form names and coding exactly the same:

Code: Select all

Form2 form2 = new Form2(); 
            form2.MdiParent = this; 
            form2.Show(); 
Form2 loads when I test it, but Form1 doesn't. I've used this code once before when the MdiContainer was the first form in my app, and it worked. I don't think this code works when the MdiContainer is added to the app.

Posted: Mon Jul 16, 2007 2:29 pm
by Prey
So what are you trying to say? You trying to add Form1 to the MdiContainer too?

Code: Select all

            Form1 form1 = new Form1(); 
            form1.MdiParent = this; 
            form1.Show(); 
Remember that the 'this' is the current form, and so the current form must be the MdiContainer