6 Replies - 2412 Views - Last Post: 16 February 2011 - 08:46 AM Rate Topic: -----

#1 soltec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 09

Beginner Coder from VB to C#

Posted 15 February 2011 - 05:58 PM

I'm coming from VB to C# and I understand instead of Imports I have to use using. What I have a question about is the opening and closing braces { } between things like this

VB:
 If Statement Then
ElseIf Statement Then
Else
EndIf

Where in C# it's if(!(string.NullorEmpty(Field) etc and then braces. Is there an easy way to know how the braces go?

Also, you have to put ; (semicolon's) at the end of some code and not at other. Is there a way to know which lines of code?

Thank you in advance for any assistance!

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner Coder from VB to C#

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 548
  • Joined: 07-July 10

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:17 AM

The semicolon goes at the end of every line of code. The only ones that don't are the control statements (if, else, while, do, etc).

The braces simply signify a segment of code that goes together. This is perfectly legal.
        private void DoStuff()
        {
            int x = 1;
            {
                int y = x;
            }
            int z = x;
        }
The braces do not effect the code in this case. What it does effect is the scope of y. You cannot say int z = y;. Because y is created inside a brace, y is not available outside the brace. x is still available because it was created before the brace.

You can think of control statements as dealing with only one line.
            if (x == 0)
                x = 1;
            x = 2;

If x's value is 0 at the if line, it will run the next line. You can put the entire statement on one line if (x == 0) x = 1;. It's just easier to read for humans if it is separated. That's why x == 0 doesn't need the semicolon. If you want multiple lines of code to run, you use braces.
            if (x == 0)
            {
                x = 1;
                x = 2;
            }

This will check x's value and run everything inside the braces if x is 0. You can use braces for only one line also. Some believe it helps in readability.

Does this help?
Was This Post Helpful? 3
  • +
  • -

#3 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 13-February 11

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:17 AM

I'm coming from using VB6 myself, and I may have this wrong - as I'm really new, too, but for the if statements - just have the braces after each "if" or "else".

Visually:

If (blah blah)
{
}
else
{
}
else
{
}

that's it. If you want to put a new if line into one of those, just put it inside the appropriate brackets and continue. Visual C# Express (which I use) does a really good job of automatically tabbing your brackets properly so you can keep track of them. That may not be what you're asking, but it's what I got.

For knowing where to put the semicolons, from a beginner it seems true so far to me that if it's a statement, it requires a semicolon, if it's not, it doesn't.

For instance - I notice no semicolons in the lines for creating a class, method, if/else/for, etc - but after each statement within the object, for, loop, method there is a semicolon. Expanding on that - any time a new set of brackets is created, the preceding code does not need a semicolon - and you'll see that the examples above are all examples which are followed by brackets.


I wish I could explain it better, and I could be wrong, but this is how I'm seeing it.

This post has been edited by NantucketSleighride: 16 February 2011 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8963
  • View blog
  • Posts: 33,616
  • Joined: 12-June 08

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:26 AM

I would also bookmark this link:
http://www.harding.e...comparison.html

Quote

VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between VB.NET and C#.

This post has been edited by modi123_1: 16 February 2011 - 08:30 AM

Was This Post Helpful? 3
  • +
  • -

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:29 AM

View PostNantucketSleighride, on 16 February 2011 - 11:17 AM, said:

If (blah blah)
{
}
else
{
}
else
{
}



You would actually want to use an else if instead of 2 else statements. There can only be 1 else per if.
if (x == y)
{
    // do something...
}
else if (x == z)
{
    // do something...
}
else
{
    // do something...
}


Was This Post Helpful? 1
  • +
  • -

#6 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 13-February 11

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:34 AM

View PostKilorn, on 16 February 2011 - 08:29 AM, said:

View PostNantucketSleighride, on 16 February 2011 - 11:17 AM, said:

If (blah blah)
{
}
else
{
}
else
{
}



You would actually want to use an else if instead of 2 else statements. There can only be 1 else per if.
if (x == y)
{
    // do something...
}
else if (x == z)
{
    // do something...
}
else
{
    // do something...
}



Yup - I wasn't really thinking about the language, but the structure of the brackets. Sorry for the confusion.
Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Beginner Coder from VB to C#

Posted 16 February 2011 - 08:46 AM

so you want to transfer from VB.NET to C#? Then you can start by checking this site out:
http://www.developerfusion.com/tools/
there is a tools that can translate from vb.net to c#. this will be an easy way to get a hand at the basic syntax.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1