|
-
Jan 19th, 2008, 08:29 AM
#1
Thread Starter
Lively Member
Few C# Questions.
Well im from VB and im new to C# i just have a few questions.
1. How is the If's else's etc done in C#? In VB it would be like
If Textbox1.Text = "Hello" Then
Form1.Textbox2.Text = "Hi!"
Else
Form1.Textbox2.Text = "Bye"
End If
2. How are varibles done? In VB it would be
Dim R As String
R = "Radio"
Form1.Textbox1.Text = R
3. Hows would the syntax be for adding 2 textbox's values togetha (if they both had integers in them.
In VB its
TextBox1 = int(Textbox2.Text + int(Textbox3.Text))
4. How do i hide a form and show another one. In VB its like
Form1.Hide
Form2.Show
Please if you know please post. It would help me alot. Thanks.
Last edited by xEnt; Jan 19th, 2008 at 08:42 AM.
-
Jan 19th, 2008, 11:00 AM
#2
Re: Few C# Questions.
One thing you should know is that C# won't create a default form instance automatically like VB, so anywhere you're using "Form1", you'll need to refer to an instantiated form instance.
e.g.,
Form1 Form1Instance = new Form1();
1. (the braces are optional for single statement cases)
if (Textbox1.Text == "Hello")
{
Form1Instance.Textbox2.Text = "Hi!";
}
else
{
Form1Instance.Textbox2.Text = "Bye";
}
2.
string R = "Radio"; //or you could have this on 2 lines if you want
Form1Instance.Textbox1.Text = R;
3.
//note that textbox doesn't have a default property in .NET, so you need to specify ".Text":
TextBox1.Text = (int.Parse(Textbox2.Text)).ToString() + (int.Parse(Textbox3.Text)).ToString();
4.
Form1Instance.Hide();
Form2Instance.Show();
-
Jan 19th, 2008, 11:45 AM
#3
Re: Few C# Questions.
Note also that C# forms don't have default instances as they do in VB. It's a shame they exist in VB, but in C# you have to do the "right and correct" thing and create an object of your form type explicitly before you can call Show or Hide on that instance. You cannot simply use the type name to access the default instance.
-
Jan 19th, 2008, 11:50 AM
#4
Re: Few C# Questions.
Absolutely - default form instances cause way more problems than the marginal benefit they are supposed to supply to novice programmers (and I don't see how making it easier to misunderstand things can be a benefit, but that's a question for the MS VB team).
-
Jan 19th, 2008, 11:56 AM
#5
Re: Few C# Questions.
 Originally Posted by David Anton
Absolutely - default form instances cause way more problems than the marginal benefit they are supposed to supply to novice programmers (and I don't see how making it easier to misunderstand things can be a benefit, but that's a question for the MS VB team).
It's my opinion, not based on any specific evidence, that it was an acquiescence to people who wanted VB.NET to be more like VB6. Everyone has to learn how to create objects with a constructor anyway, or they'll never get anywhere in VB.NET, so what is really gained by saving that one line of code? Default instances are like Option Strict Off by default: it causes more problems than it solves.
-
Jan 20th, 2008, 09:05 AM
#6
Re: Few C# Questions.
 Originally Posted by jmcilhinney
Note also that C# forms don't have default instances as they do in VB. It's a shame they exist in VB, but in C# you have to do the "right and correct" thing and create an object of your form type explicitly before you can call Show or Hide on that instance. You cannot simply use the type name to access the default instance.
Come on... I love the 'My' object. even though I haven't written a VB line for about a year but come onnnnnn
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jan 20th, 2008, 06:18 PM
#7
Re: Few C# Questions.
 Originally Posted by ComputerJy
Come on... I love the 'My' object. even though I haven't written a VB line for about a year but come onnnnnn
Not that my post was really relevant to the topic of this thread but this has no relation to the topic of my post. Firstly, 'My' is a namespace, not an object. Secondly, I didn't make any reference to the 'My' namespace. 'My' is good. The principle behind default form instances is good but they don't actually do anyone any good.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|