# C#, difficult to learn?



## Dark_Webster (Nov 26, 2008)

Are there many differences from VB .NET to C#? I'm currently learning VB6 in my school (yeah it sucks) but when I'm at home I work with VB .NET 2005.

I only do small programs like lottery number generator, counters, math programs, etc...

Would it be too hard, since i have some bases on VB?


----------



## FordGT90Concept (Nov 26, 2008)

Only the syntax is really different.  C# is like putting C++ in a bottle with VB6 and shaking it up.  It is an odd child to be certain.  It's not really VB but it's not really C++ either.

A Hello World example in VB:

```
Public Class HelloWorld
  Public Shared Sub Main(ByVal args() As String)
    Console.WriteLine("Hello World!")
  End Sub
End Class
```

In C#:

```
namespace MyApp 
{
  public class HelloWorld
  {
    public void Main(string[] args)
    {
       Console.WriteLine("Hello World!")
    }
  }
}
```

Note: Those aren't actually compiled code examples. XD


----------



## Dark_Webster (Nov 26, 2008)

You're saying that the difference is mainly on the syntax of code and variable declaration?

So, I'm going to give it a try . If it's easy to learn, C# will become my second programming language.


----------



## FordGT90Concept (Nov 26, 2008)

In colleges, all they teach anymore is VB.NET and the more advanced classes (programming majors) focus on the transition from VB.NET to C#.NET.  The transition is virtually painless so that's why they do it (all the .NET libraries are directly compatible).  C#.NET is also a good stepping stone to the C++ world of coding although I haven't ventured that far--C# suits me needs.

Yeah, it's mostly sytax differences...but there's a lot of them.


----------



## mdm-adph (Nov 26, 2008)

Well, if you know Java, C# isn't too hard.

Seriously, I took one look at it and went, "Now isn't that cute -- Microsoft's made their own Java.  Aww...."

Can't be any _worse_ than Java, at least.


----------



## FordGT90Concept (Nov 26, 2008)

It's sooooo much better than Java.  Java's libs are e'ffin pitiful.  .swing? .awt?  .?  Moreover, it is murder to perform the simplest task like halt one form while information is input in another.  And Event handling is simply dreadful.  Java == Epic Fail.

J#.NET was what Java needed to be but, because C# is wildly successful while J# isn't, J# got the can.  C# isn't going to die any time soon.  In terms of start to finish in development, I have never seen something so fast.  C# and specifically, the .NET framework, is a godsend.


----------



## Dark_Webster (Nov 26, 2008)

I didn't found it as hard as i thought it would be. Here's an example of what i made:
(Nevermind the name of the objects being in portuguese...)


```
namespace App_Teste
{
    public partial class FormTeste : Form
    {
        public Int64 xis;
        public FormTeste()
        {
            InitializeComponent();
        }

        private void FormTeste_Load(object sender, EventArgs e)
        {

        }

        private void BotãoComeçar_Click(object sender, EventArgs e)
        {
            Temporizador.Enabled = true;
        }

        private void BotãoParar_Click(object sender, EventArgs e)
        {
            Temporizador.Enabled = false;
        }

        private void Temporizador_Tick(object sender, EventArgs e)
        {
            DateTime data;
            data = DateTime.Now;
            LabelDataHora.Text = Convert.ToString(data);
        }

        private void BotãoAlterarCor_Click(object sender, EventArgs e)
        {
            DialogoAlterarCor.ShowDialog();
            this.BackColor = DialogoAlterarCor.Color;
        }

        private void Sair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BotãoRandomizar_Click(object sender, EventArgs e)
        {
            Random randomnumber = new Random();
            xis = randomnumber.Next(1,900);
            LabelNumeroRandomizado.Text = Convert.ToString(xis);
        }


    }
}
```

Well, going to try to convert some of my programs into C#. Thanks for all your replies .


----------



## FordGT90Concept (Nov 26, 2008)

Some pointers...

1) Use "long" instead of Int64.  C# has a lot of keywords that make variable definitions easier and they also stand out individually from direct class references.  Keywords are colored blue while classes are aqua colored.

2) 
	
	



```
DateTime data;
data = DateTime.Now;
```
Can be condensed into a single line:

```
DateTime data = DateTime.Now;
```
And if you are not ever using data besides setting the text, you can just send it straight there:

```
LabelDataHora.Text = DateTime.Now.ToString();
```

3) As seen above, you can almost always use "object.ToString()" rather than "Convert.ToString(object)."  The first does not require a reference to System, the second does.  That is, either "using System;" up at the top or "System.Convert.ToString(object)."  "Using" is the virtually the same as "Imports" in VB.


Looks pretty good overall.


----------



## Kreij (Nov 26, 2008)

Good for you DarkW,

Just so you know the DateTime class has it's own built in conversions (as do many of the other classes).

You could just write ... LabalDataHora.Text = data.ToShortDateString();

or to any of the other string converters built into the class.


----------



## Dark_Webster (Nov 26, 2008)

Oh, then sometimes the code becomes easier? That's nice. Thanks for pointing me out some flaws in my code.


----------



## FordGT90Concept (Nov 26, 2008)

Oh, and another thing that can cause errors if unchecked...

DateTime uses thread-specific culture information.  That is, the DateTime.ToString() behaves differently depending on the culture set (if set to Culture = "neutral" which it is by default).

It needs the following references:

```
using System.Threading;
using System.Globalization;
```

This actually forces a culture (usually place it in a constructor):

```
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
```


----------



## Dark_Webster (Nov 27, 2008)

Just one thing:

How can I convert this loop:

```
Do While Numero5.Text = Numero4.Text And Numero5.Text = Numero3.Text And Numero5.Text = Numero2.Text And Numero5.Text = Numero1.Text
{Insert code here}
Loop
```

Into C#?


----------



## dazed554 (Nov 27, 2008)

```
while( Number5.Text == Number4.Text && Number5.Text == Number3.Text && Number5.Text == Number2.Text && Number5.Text == Number1.Text )
{
     // code
}
```


----------



## Deleted member 3 (Nov 27, 2008)

Better run and hide, if W1z reads your code he will maim you to death for programming in Portuguese. He once put my head under water for two minutes because I had a variable in Dutch.


----------



## FordGT90Concept (Nov 28, 2008)

I know of someone that uses variable names like "lol" and "rofl."  I made it clear to them I didn't like it but I didn't try to maim him.  Maybe I should have to "drive the point home." 


Just like to point out one thing with most C languages...

If you have a single line statement, you usually don't need the curly brackets.  For example:

```
byte i = 0;
while( i < 10)
     i++;
```
Only one line of code though (two lines total)...

It makes the code more readable.


----------



## 3870x2 (Nov 28, 2008)

sharp is much less forgiving than VB...
also sharp is much more powerful in many different ways.


----------



## FordGT90Concept (Nov 28, 2008)

Yeah, when there is an error, it is far less specific as to what is wrong in C#.  A quick search on Google can usually point you in the right direction though.

The only thing I have noticed that C# can do that VB can't is C-style memory pointers.  To do so requires compiling with the /unsafe flag however.  Other than that, VB really isn't that much more versatile.  Both languages can be used in ASP.NET and both can be used in Silverlight.  Both can make Windows Applications, Console Applications, Class Libraries, and Windows Services.  Yet, some knowledge of VB could carry over to author VBA code used in Excel, for example.  Overall, they are quite balanced.


----------

