So you know VBA but for some unknow reason you need a cut to the case guide to understand C# quickly.
First thing to understand is the VBA has both subroutines and functions, most other programming languages just have functions.
In VBA the subtle difference between a subroutine and a function is that a function returns a value, whereas a subroutines does not.
In C style programming languages such as C#, Javascript, C, C++, Java etc the job of subroutines are peformed by functions (often contained in classes).
First some syntax comparisons…
VBA - Subroutine Sub TestRoutine() Dim szFirstName As String, szMessage As String Dim i As Integer szFirstName = "Bob" For i = 10 To 25 If i < 20 Then szMessage = szFirstName & " is a Teenager." Else szMessage = szFirstName & " has grown up." End If MsgBox szMessage Next End Sub VBA - Function ' We use the call statement here because this particular function ' is not going to return a value or message. call function TestRoutine() as integer Dim szFirstName As String, szMessage As String Dim i As Integer szFirstName = "Bob" For i = 10 To 25 If i < 20 Then szMessage = szFirstName & " is a Teenager." Else szMessage = szFirstName & " has grown up." End If MsgBox szMessage Next End function
The above two examples show two vba routines that do the same thing, one being a subroutine and the other being a function. However with C# you only have functions.
Also very important to note that a string is not a basic datatype as in vba but rather a class (which becomes an object) built up from individual ASCII characters, with functions and properties attached.
Strings in C# are immutable (get used to this term you are going to see it a lot) that means you cannot change a string once it is assigned to a variable. And if it appears to work that is because the .Net Framework is returning an new object to the same variable name.
So the above vba code will have to be changed for C#. In the following block we show a workaround.
void doSomeThing() { string szFirstName="Bob"; // Here we declare and assign at the same time for(i=0;i<26;i++){ if(i<20) // Note use of + for MessageBox.Show(szFirstName + " is a Teenager."); // concatenation rather else // than & MessageBox.Show(szFirstName + " has grown up."); } }
Points to note
Lets look at the if statement in the doSomeThing routine.
if(i<20) MessageBox.Show(szFirstName + " is a Teenager."); else MessageBox.Show(szFirstName + " has grown up.");
Notice that there are no {} braces there, this is because there is only one line of code between each "if" statement. The following situation demonstrates the need for scope braces.
if(i<20){ doSomething(); doSomethingElse(); nowDoThis(); } else JustDoThisInstead();
In the above snippit we needed the brace for the first half of the "if" statement, because there were mulitple lines and the braces tell C# (and all c style languages) that this "if" statement is more complex than a single line. The following is also a valid "if" statement..
if(j==10) processNumber10(); OtherNonIfRelatedStuff();
In the above situation when the code reaches the "if" statement it will test to see if j is equals 10, if so it will execute processNumber10() and then execute OtherNonIfRelatedStuff() and if j does not equal 10 then it will just execute OtherNonIfRelatedStuff()
Note do not do this....
if(j==10);
That semicolan changes the whole meaning of the "if" statement.
The code would now read if j equals 10
then do nothing
then execute processNumber10()
then execute OtherNonIfRelatedStuff();
Other Gotcha's
= and == have different meanings, in VBA they are both represented by = however the "is equal to" boolean logic test is represented by "==" if(someValue=10) will represent a bug in your program because someValue will always equal 10 because you have just assigned 10 to it!
= This means assign to iTopValue=20; // Assign 20 to iTopValue iTopValue==20 // TopValue is equal to 20 if top value happens to be // 19 the result is false if (iTopValue==20){ // do this if 20 } else { // do this if not 20 }
All code is case sensitive, if you make a mistake here it should be easy to find as code will not compile
Hope this is useful if this article generates any interest I will write more in the future.
Leave a Reply