C# Newbie

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

In VB i can do the following.....

With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With


Is there an equivalent in C# in ASP.NET or do I have to use a for.....next
knowing the number of rows in the dataset and repeat the whole
Dataset.table(0).fieldname dot notation?


Thanks
 
Hi Craig,

There is no 'with' construct in C#, you have two alternatives:
a) As you say, repeating the DataSetThoughtsForm1.tblYourThoughts[0]
notation each time.
b) introducing a local variable and then using it, like this:

Thought thought = DataSetThoughtsForm1.tblYourThoughts[0];
thought.ProfileID = Session["UserID"];
thought.ThoughtName = txtThoughtName.Text;
thought.ThoughtText = txtThoughtText.Text;

Regards - Octavio
 
The VB.NET "With" directive doesn't iterate over a datatable for you,
all it does is save some typing. In VB.NET you would still need a
for xx or for each loop, as you do in C#.

No, C# does not provide the equivalent helper directive. But, then
C# also takes about 30% less typing of words in general to accomplish the
same
things, so there's your tradeoff.
Happy coding! And welcome to C#.
Peter
 
Peter,
No, C# does not provide the equivalent helper directive. But, then
C# also takes about 30% less typing of words in general to accomplish the
same
things, so there's your tradeoff.
Happy coding! And welcome to C#.
Peter

My experience is completly the opposite way.

Can you give me from the two samples bellow how things can be written
shorter in C# than in VB.Net. I am forever curious because of your statement
that I do something wrong in C#.

Public Datagrid1 As New Datagrid
DataGrid1.Datasource = ds

There are some statements that needs more characters. However, by instance
the property is completly constructed by the IDE in VBNet and therefore my
expirience is that therefore as well that mostly less typing is needed for
those than in C#.

(In addition, most statements are not declarations).

However probably I do something wrong.

Cor
 
Craig said:
In VB i can do the following.....

With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With


Is there an equivalent in C# in ASP.NET or do I have to use a for.....next
knowing the number of rows in the dataset and repeat the whole
Dataset.table(0).fieldname dot notation?


Thanks
You have the foreach command :

foreach(Thoughts in DataSetThoughtsForm1) // Thoughts is the type of the
items of your array
{
Thoughts.ProfileID = Session("UserID")
Thoughts.ThoughtName = txtThoughtName.Text
Thoughts.ThoughtText = txtThoughtText.Text
}
 
Craig,

Because of the more answers, see the answer from Octavio.

The dot in VBNet with the 'With' is nothing more than an address for the
object you are referencing to. I don't like that because even this bellow in
VBNet is shorter writting than with the 'With'

dim myDataTable as New DataTable
dim m as DataTable
m.Name = "MyName"

Which is with the 'With'

dim myDataTable as New DataTable
With myDataTable
.Name = "MyName"
End with

The code created by above is exactly the same for both.

The equivalent in C# is than (However Octavio showed you that already)
DataTable myDataTable = new DataTable;
DataTable m = myDataTable;
m.Name = "MyName";

Where the created code is again almost the same as above.

I hope this helps,

Cor
 
I think he refers to the need to end code blocks with words instead of brackets. Overall there may not be fewer characters in C# compared to VB.Net as C# needs more special characters to distinguish some parts of code from others, like ; ( )
 
Craig said:
In VB i can do the following.....

With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With


Is there an equivalent in C# in ASP.NET or do I have to use a for.....next
knowing the number of rows in the dataset and repeat the whole
Dataset.table(0).fieldname dot notation?

No, but the C# syntax is much better IMO in other ways ; if you don't
want to do this you could use a temporary object variable to do it.
 
I must have had a long day. I mixed up 2 things

I was looking for the equivalents in C#


1) Dim myRow As DataRow
For Each myRow In DataSetGroupSQL1.tblProfileGroups.Rows
'Test for ALL group
If myRow("GroupName") = "ALL" Then blnGroupALL = True
Next


2) With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With
 
Cor,
let me try to be more specific since I never intended to stir up another
flame war here:
In general, if you take the source code file for a class written in C# and
the same class written in VB.NET, the C# file will be smaller.
Peter
 
Peter,
let me try to be more specific since I never intended to stir up another
flame war here:

You were the one who told something. I never read any bad thing written
about C# in the language.vb newsgroup. So I don't see the reason for your
statement than misqualifying a good program language without to know what
you wrote. This is for me a kind of misleadig, I think that this is not good
in a newsgroup.
In general, if you take the source code file for a class written in C# and
the same class written in VB.NET, the C# file will be smaller.

I assume that you are right in that, especially the properties take a lot of
words in VBNet. However, there are other places where C# takes a lot of more
characters (as Morten as well correctly showed).

The amount of characters is not a measurepoint for typing (and that was what
you told not the profit of 100bytes in a source file). By instance the
properties in a class are almost completely typed by the IDE in VBNet
automatically.

Therefore I think that this is a bad decision item for C# or VBNet. In my
opinion are there better items to make the decision for C# instead of VBNet.

Just my thought,

Cor
 
Ok, well I've never said that word count was a reason to select a
programming language. I did imply that I feel that C# is a more compact
programming language, since it was designed from scratch with the .NET
platform in mind.

There is a very large number of highly professional VB.NET programmers out
there. One of the distinguishing facets of their "Quality Code" programming
is that they always set Option Strict and Option Explicit to "On" (which
both are, unfortunately, set to "Off" by default).

The other thing they tend to do is avoid references to namespaces with
"VisualBasic" in their name, since (up to now) these assemblies are not
marked "CLS-Compliant". Although, in .NET 2.0, they are marked so.

Otherwise, unless you intend to perform operator overloading or work with
pointer arithmetic for speed, there is very little difference in the
resultant IL no matter what your programming language preference is.

Regarding newgroup posts, I have always felt that one's professional opinion
is welcome. Other posters (such as you) are always free to disagree or post
their differing opinions. That's what free intercourse and exchange of ideas
is all about.

Cheers,
Peter
 
<all it does is save some typing. >

I was always taught - and have read it in the news groups - that using
the "with" statement produces more efficient run-time code.

Jeff
 
Jeff said:
<all it does is save some typing. >

I was always taught - and have read it in the news groups - that using
the "with" statement produces more efficient run-time code.

It produces more efficient runtime code than evaluating a property
several times, but that's only the same as saying that:

SomeObject x = foo.Bar.Baz;
x.Text = "Hello";
x.Height = 20;
x.Width = 30;

is more efficient than
foo.Bar.Baz.Text = "Hello";
foo.Bar.Baz.Height = 20;
foo.Bar.Baz.Width = 30;

Basically, the first example is exactly what "with" does.
 
Peter,

The other thing they tend to do is avoid references to namespaces with
"VisualBasic" in their name, since (up to now) these assemblies are not
marked "CLS-Compliant". Although, in .NET 2.0, they are marked so.

It is completely CLS Compliant in all VS net versions.

It is in VBNet almost impossible to create non CLS Compliant code. In C#
that is very easy. By instance this is completly CLS incompliant.

public string MyString;
public string myString;

While it is correct C# code.

The use of the microsoft namespace gives in most cases more optimized
endcode than only using the single system Net namespace. (I call a nop no
code).

Cor
 
Peter,

Are you really trying to tell that you are right with showing a bug, while
in C# it is standard without to be a bug.

Not that it has any importance for me, however you are telling again bad
things from VB.Net which seems to come completely from your personal mind,
and in the same moment showing that you have not much knowledge of VB.Net.

You like probably this kind of flaming messages, therefore this is my last
in this thread the value of it is now showed enough.

Cor
 
Cor,

I was not passing judgement in any way, I was simply pointing out a
known fact. Often developers don't quite understand the real meaning
of what "CLS-Compliant" is.

I think what may have happened here is that you are simply reading "too
much" into the discussion. I myself started out programming in VB
(Version 2 to be specific!) and have made a lot of money as a developer
over the years with VB, including some with VB.NET. So I think I can
safely say that I do have some familiarity with the language.

And I agree this is not the place for such flame-type discourse. I
switched to C# as my primary .NET language in 2000 when the first beta
of .NET 1.0 was handed out, and I haven't looked back since. As I
mentioned before, there are many, many highly professional programmers
who prefer to work in VB.NET.

Peter
 
Cor Ligthert said:
Are you really trying to tell that you are right with showing a bug, while
in C# it is standard without to be a bug.

He mentioned one problem with referring to the Microsoft.VisualBasic.*
assemblies. You apparently misunderstood that as claiming that VB.NET
itself wasn't CLS-compliant, when he had never claimed that at all.
Not that it has any importance for me, however you are telling again bad
things from VB.Net which seems to come completely from your personal mind,
and in the same moment showing that you have not much knowledge of VB.Net.

I fail to see how it can be "completely from [Peter's] personal mind"
when he referred to a KB article which absolutely confirmed what he had
previously written.
You like probably this kind of flaming messages, therefore this is my last
in this thread the value of it is now showed enough.

There really was *no* flaming involved there. If you think that's
flaming, you should see a *real* flame war.
 
Jon,
Thanks for clarifying. Now that we have this succesfully relegated to
its place in ancient history, I want to try and answer Craig's actual
"rephrased" question from above, rewriting his two VB.NET samples:

1) Dim myRow As DataRow
For Each myRow In DataSetGroupSQL1.tblProfileGroups.Rows
'Test for ALL group
If myRow("GroupName") = "ALL" Then blnGroupALL = True
Next

C# example:

foreach(DataRow myRow in DateSetGroupSQL1.tblProfileGroups.Rows)
{
if( (string)myRow["GroupName"]=="ALL") blnGroupALL=true;
}


2) With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With

C# Example:
DataRow dr= DataSetThoughtsForm1.tblYourThoughts.Rows[0] ; not
sure but looks like you want DataRow "0" here
dr["ProfileID"] = (int)Session["UserID"] ; // assumes it is type
int stored in Session
dr["ThoughtName"] = txtThoughtName.Text ;
dr["ThoughtText"] = txtThoughtText.Text ;

-- Craig, I haven't run this code but it should be "close".
Peter
 
Back
Top