Results 1 to 7 of 7

Thread: Few C# Questions.

  1. #1
    Lively Member
    Join Date
    Jul 07
    Posts
    69

    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 07:42 AM.

  2. #2
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    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();
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,834

    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.

  4. #4
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    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).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,834

    Re: Few C# Questions.

    Quote 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.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 05
    Location
    Happily misplaced
    Posts
    2,513

    Re: Few C# Questions.

    Quote 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

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,834

    Re: Few C# Questions.

    Quote 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
  •