Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# using { brackets

Posted on 2012-03-20
5
Medium Priority
?
244 Views
Last Modified: 2012-03-20
Hi

I am a VB.net developer starting C#
I just wanted a guideline on how the { brackets work. I noticed that they autogenerate
ig I double click on a button. Can someone give me a few guidelines on using them in stand alone procedures etc - in relation to VB.net
0
Comment
Question by:Murray Brown
5 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 1000 total points
ID: 37742265
Brackets are basically block delimiters

They say, there is body to be executed here.
So

if  whateve
{ dosomething;
   dosomethingelse;
   dosomemore;
}
else
{ dothisinstead;
   andthins;
}

same for whiles
same for select statement which is called switch()
save for  procedure and function bodies.

int square(int a) {
      return a * a;
}

You can sometimes write a one line If where there is only one statement and avoid {}, but
it is more readable to just use them.  Of course some of the above is just pseudo code.
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 1000 total points
ID: 37742300
VB
if x then
    do
    d2
    d3
    d4
else
    do5
    do 6
end if

C
if (x)
{
    do;
    d2;
    d3;
    d4;
}
else
{
    do5;
    do6;
}
//notice, no end if in c#

c languages allow multiple line spans without a line continuation char
vb.net
dim s as string="this is some text" & _
                            "with more text on the second line"

c
string s =new string("this is some text
                                     with more text on the second line");

//that's why c/C++/C# need the ;

Well something like that. I don't program in C#.. Hope this helps.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 500 total points
ID: 37742311
{} or curly brackets can be seen as a Begin and End statement that surround blocks of code so as shown below they surround the code for a class, and a method/function and surround the code for say a foreach loop

class Program
{
    static int[] _array = { 1, 2 };

    static void Main()
    {
	Good();
    }

    static void Good()
    {
	foreach (int element in _array)
	{
	    Console.WriteLine(element);
	    Console.WriteLine(true);
	}
    }
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 37742344
Hopefully you won't encounter it often, but be aware that you can also have brackets not associated with anything.

e.g

{
    int x = 4;
}

Open in new window


In this way, you have created a block (scoping) where x only exists within the brackets. This usage would be a way for you to show in your code that x is only used in this one spot. It's not a very common usage, AFAIK, but if you run across it in usage, at least you'll be aware. The closest thing in VB that I can think of would be a With block, if you structured it where the target of the With was acquired in the With declaration.

e.g.

With oCmd.OpenRecordset(...)
    ...
End With

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 37742431
Thanks very much
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question