This question already has answers here:
With block equivalent in C#?
(19 answers)
Closed 9 years ago.
I am trying to convert simple vba "with statemnt" to C#. ProductRangeA and B are named ranges. Could someone to give a hint?
With ProductRangeA
myRow= .Rows.Count:
myValue= .Value
End With
With ProductRangeB
myRow= .Columns.Count:
myValue= .Value
End With
As HighCore said, there's no C# equivalent to VB's With block. Here's the equivalent C# code:
myRow = ProductRangeA.Rows.Count;
myValue = ProductRangeA.Value;
myRow = ProductRangeB.Columns.Count;
myValue = ProductRangeB.Value;
Since you can't avoid typing ProductRangeA and ProductRangeB twice each, you can reduce typing by using shorter variable names. That can make the code more difficult to understand, of course.
Related
This question already has answers here:
C#7: Underscore ( _ ) & Star ( * ) in Out variable
(6 answers)
Discard feature significance in C# 7.0?
(7 answers)
Closed 3 years ago.
First of it all: my C# Knowledge is depricated because i do other projects in the meantime.
So i come back and see whats up now, but there is one thing i do not understand.
This is now valid in C#
_ = new object();
I try to use a search engine of my choice but u can imagine search for "C# _" isnt very helpful to search for. Can someone explain whats going on here? My idea was this is placeholder to pop the result back on the stack and avoid agressive compiler optimization. But at the end, i have no idea.
This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance
This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
This question already has answers here:
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Objects implicitly instantiated in vb.net?
(2 answers)
Closed 7 years ago.
Recently I've picked an old project in VB.NET and I keep finding things like this that annoy me:
Dim answer as String
answer = Form_Input_Text.Lbl_Answer.Text
Apparently, in VB.NET it is allowed to access stuff which should belong to an instance of a Form as if it is something static. In C# one would need to write this:
string answer = null;
using (Form_Input_Text my_form = new Form_Input_Text())
{
//stuff
answer = my_form.Lbl_Answer.Text;
}
The first bit of code is compiled with no errors, but sometimes I get NullReferenceException as expected.
How do I enforce this behaviour on VB.NET?
This question already exists:
Closed 11 years ago.
Possible Duplicate:
String vs string in C#
I have a test in C# code I'm reading:
if (variable is string)
I am wondering if this is strictly equivalent to:
if (variable is String)
or if some esoteric behavior of C# autoboxing may cause these tests to behave differently.
They are exactly the same - string is an alias for System.String.