Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the with() construct not included in C#, when it is really cool in VB.NET?

I am C# developer. I really love the curly brace because I came from C, C++ and Java background. However, I also like the other programming languages of the .NET Family such as VB.NET. Switching back and forth between C# and VB.NET is not really that big of deal if you have been programming for a while in .NET. That is very common approach in the company where I work. As C# guy, I really like the XML literal and with keywords provided by the VB.NET compiler. I wish Microsoft had included those features in C# also.

I am just curious , what other developer has to say about it!

like image 692
Shiva Avatar asked Jan 09 '09 21:01

Shiva


People also ask

How can I include a standard C++ header file in my C code?

If you are including a C header file that isn't provided by the system, you may need to wrap the #include line in an extern "C" { /*... */ } construct. This tells the C++ compiler that the functions declared in the header file are C functions.

How do you fix undefined reference error in C?

c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.

Why is include used in C?

The #include directive tells the C preprocessor to include the contents of the file specified in the input stream to the compiler and then continue with the rest of the original file. Header files typically contain variable and function declarations along with macro definitions.

Why do we need extern in C?

Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.


2 Answers

Personally I don't like WITH when it's used after construction - if you need to do several things with an object after it's initialized, usually that behaviour should be encapsulated in the type itself. If you really want to do something like WITH, it's only a matter of declaring a short variable and optionally introducing a new scope.

However, it is useful to be able to compactly initialize an object with several properties - which is precisely why C# 3 allows you to write:

MyObject x = new MyObject { Name="Fred", Age=20, Salary=15000 };

There are limitations to this (which the optional and named parameters in C# 4 will help to overcome) but it's better than it was without leading to potential messes/ambiguities.

(On the XML literal front, I'm again with the C# team - XML is a very specific technology to put into a language. If they could come up with a generalised form which happened to create XML but could be used to create other trees too, that would be nice - just as query expressions aren't directly tied to IEnumerable or IQueryable.)

like image 183
Jon Skeet Avatar answered Oct 01 '22 10:10

Jon Skeet


You can replace VB.Net's With by creating a quick single-letter variable name. It's actually less code, since With also requires an End With later on.

For example, one thing I used to need to do fairly often was iterate over the rows in a datatable for a control/break style report.

In vb.net, that might look like this:

Dim CurCustomerName As String 
Dim CustomerSalesTotal As Decimal

Dim ds As DataSet = GetReportData()
With ds.Tables(0).Rows
   Dim i As Integer = 0
   While i < .Count
       ''//{
       CurCustomerName = .Item(i)("CustName")
       CustomerSalesTotal = 0
       PrintHeaderLine(CurCustomerName)

       While i < .Count AndAlso CurCustomerName = .Item(i)("CustName")
            ''//{
            PrintItemLine(.Item(i)("OrderTotal"))
            CustomerSalesTotal += .Item(i)("OrderTotal")

            i+= 1
       End While ''//}
       PrintSubTotalLine(CustomerSalesTotal)
   End While ''//}
End With

The C# would look like this:

string CurCustomerName;
Decimal CustomerSalesTotal;

DataSet ds = GetReportData();
DataRowCollection r = ds.Tables[0].Rows;
int i=0;
while (i<r.Count)
{
    CurCustomerName = r[i]["CustName"];
    CustomerSalesTotal = 0;
    PrintHeaderLine(CurCustomerName);

    while (i<r.Count && CurCustomerName == r[i]["CustName"])
    {
        PrintItemLine(r[i]["OrderTotal"]);
        CustomerSalesTotal += r[i]["OrderTotal"];

        i++;
    }
    PrintSubTotalLine(CustomerSalesTotal);
}

The thing to notice here is that the C# version actually needed less typing, because the VB couldn't combine WITH with the array index, and had to go through the .Item property for certain things. It's not a big deal here, but imagine if the report had 20 fields instead of 2 and had to break on 3 items instead of 1.

Of course, you could use the technique demonstrated in C# for VB as well. But the main thing to note is that WITH doesn't really give you much.

like image 26
Joel Coehoorn Avatar answered Oct 01 '22 08:10

Joel Coehoorn