-2

I am fairly new to C# and VB coding. I need to create .Net framework describing chemical reaction as an extension so that I can reuse it in HYSYS. There are tutorials from 2004 which has a tutorial implementation in VB. I was trying to convert the structure from VB to C# but I am confused at With....End With implementation. Can you describe whats going on here as there is no assignment relations?

Ref Code lines is below and the tutorial pdf link is https://sites.ualberta.ca/CMENG/che312/F06ChE416/HysysDocs/AspenHYSYSCustomizationGuide.pdf (chapter 3).

With hyContainer
    .SetReactionPropertyState rpReactants, vsCalculated
    .SetReactionPropertyState rpStoichiometricCoefficients, vsCalculated
    .SetReactionPropertyState rpMinTemperature, vsCalculated
    .SetReactionPropertyState rpMaxTemperature, vsCalculated
    .SetReactionPropertyState rpReactionBasis, vsCalculated
    .SetReactionPropertyState rpReactionPhase, vsCalculated
    .SetReactionPropertyState rpBaseReactant, vsCalculated
    .SetReactionPropertyState rpBasisConversion, vsCalculated
    .SetReactionPropertyState rpRateConversion, vsCalculated
End With
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • My questions is what ".SetReactionPropertyState rpReactants, vsCalculated" does this mean? It does not seem like an assignment. Is it defining new properties of the object? – Umesh Pandey Feb 07 '19 at 14:11
  • 2
    With no parentheses around the arguments, that code looks more like VBA than VB.NET. – Andrew Morton Feb 07 '19 at 14:12
  • @UmeshPandey parentheses are optional – the_lotus Feb 07 '19 at 14:12
  • @UmeshPandey If you follow the duplicate link which shows you the [equivalent C# code](https://stackoverflow.com/a/1175335/11683), does that not tell you what it does? – GSerg Feb 07 '19 at 14:14
  • @GSerg to be fair, the code in your link isn't equivalent to his code. The code in your link constructs a class, and then assigns its properties (which you can do with an object initializer in C#). This code is calling a bunch of methods on an object (which you cannot do with an object initializer in C#). – canton7 Feb 07 '19 at 14:17
  • Thanks all for the quick responses. As I am new in both C# and VB, lack of parentheses and use of space got me confused. The answer by canton7 illustrates that pretty much. Anyway, since I am more from MATLAB and Python background, having space and no parentheses did not make any sense to me. Thanks once again to all. – Umesh Pandey Feb 07 '19 at 14:18
  • @canton7 Use the part that is above the `Or in C# 3.0:` line. – GSerg Feb 07 '19 at 14:18
  • 1
    @GSerg still, that's assigning a property rather than calling a method. Given that he didn't know that his VB was calling methods, that could be confusing. – canton7 Feb 07 '19 at 14:19
  • @the_lotus Parentheses are optional only when there are no arguments. When there are, presence or absence of parentheses [is significant](https://stackoverflow.com/a/10262247/11683), and inserting them where there should not be any is a [compile time error](https://stackoverflow.com/q/15040060/11683). All that is given the OP's code is actually VB6/VBA, which it evidently is; in VB.NET the rules [are different](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/sub-statement#calling-a-sub-procedure). – GSerg Feb 07 '19 at 14:27
  • Anyway, I implemented like canton7 suggested, which I believe is calling a method rather than assigning property. It seems that I will get building error with error code "The name 'vsCalculated' does not exist in the current context". In the demo program written in VB, they do not define 'vsCalculated' anywhere. So does it make any sense that it is calling the method? – Umesh Pandey Feb 07 '19 at 14:29
  • @UmeshPandey It makes sense because calling a method has nothing to do with whether a variable is declared. If it is not declared in the code itself, then there must be a reference to a library that declares it. – GSerg Feb 07 '19 at 14:31
  • `vsCalculated` should be coming from the type library. In this case, it's a member of `VariableStatus_enum`; depending on which version of Visual Studio you use, it may give you a light bulb option to add the appropriate namespace prefix or `using` / `Imports` statement. Similarly, the "rp" prefixed items are also enum members, `ReactionProperty_enum`, and will need similar handling. – Craig Feb 11 '19 at 19:59
  • It should be coming from HYSYS reference but when I tried to compile it I could not compile. I might need to specify using statement but I really do not have any idea what kind of library is associated with HYSYS. Anyway thanks all for the support. I got chance to learn a lot of useful coding tips here. – Umesh Pandey Feb 12 '19 at 20:09

1 Answers1

2

The equivalent C# is:

hyContainer.SetReactionPropertyState(rpReactants, vsCalculated);
hyContainer.SetReactionPropertyState(rpStoichiometricCoefficients, vsCalculated);
// etc

C# doesn't have an equivalent of with.

canton7
  • 37,633
  • 3
  • 64
  • 77