REF vs OUT

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

REF vs OUT

Post by ic2 »

I use the code below:

Code: Select all

nStatus := ACE2.AdsConnect101( cConnectStr,REF hConnOptions,REF hConn)
...
nStatus:=ACE2.AdsOpenTable101(hConn,cDbfFullPath,Ref hTable)
Code like this gives me multiple warnings as follows:

Warning XS9071 Parameter 3 needs a(n) 'Out' modifier. This modifier was automatically added.

What's suddenly wrong with REF? E.g. hConn, the 3rd paramater in the first line, should be assigned a value to be used in AdsOpenTable. And it works so I basically don't want to change REF to OUT without knowing why (or even if it changes the working of my program)

Dick
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: REF vs OUT

Post by robert »

A REF variable and an OUT variable are almost the same.
The difference is that the compiler checks to see if the REF variable has been assigned a value before the function call.
The value of the REF variable may be changed inside the function call, but that is not necessary.

An OUT variable does not have to be initialized and must be assigned in the function.

VO did not have OUT variables, so that is why in VO we used REF for this.
In the 2 ADS examples, the return value of the function indicates success/failure. The OUT variable is used to return the handle of the connection / table that is opened.


Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: REF vs OUT

Post by ic2 »

Hello Robert,
robert wrote: Tue Oct 01, 2024 8:16 pm A REF variable and an OUT variable are almost the same.
The difference is that the compiler checks to see if the REF variable has been assigned a value before the function call.
An OUT variable does not have to be initialized and must be assigned in the function.
Then it confuses me that I get all these warnings. In the below example I did assign the variables used. So why does the compiler say I need an 'Out' modifier when REF is valid (and almost the same) too?
Also what does 'This modifier was automatically added' actuall mean/do?

Dick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: REF vs OUT

Post by Chris »

Hi Dick,
ic2 wrote: Wed Oct 02, 2024 9:58 am
robert wrote: Tue Oct 01, 2024 8:16 pm A REF variable and an OUT variable are almost the same.
The difference is that the compiler checks to see if the REF variable has been assigned a value before the function call.
An OUT variable does not have to be initialized and must be assigned in the function.
Then it confuses me that I get all these warnings. In the below example I did assign the variables used. So why does the compiler say I need an 'Out' modifier when REF is valid (and almost the same) too?
Also what does 'This modifier was automatically added' actuall mean/do?
It's the method you are calling that is defined to accept an OUT parameter, while you are passing it a parameter using the REF modifier. In c# that would be actually a compiler error, but in X# we have made just a warning, and the X# compiler automatically changes your REF modifier used to OUT, that's what the message says.

REF and OUT (and IN) modifiers are similar, but have different semantics. I will be discussing those in my session in Munich, but here's some good description about them in c# code (but the exact same principles apply in X#):

https://medium.com/c-sharp-programming/ ... 0dc30aad6a
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: REF vs OUT

Post by ic2 »

Hello Chris,

Thanks for the further explanation; it makes sense. What I did is change REF to OUT everywhere I had a warning and these disappeared. I hope the program still works as it did.

Furthermore no doubt there's a good reason for adding OUT which was not present in VO. As REF worked too (in VO) I only changed to OUT to reduce the number of warnings. Not because I think the program now works better ;)

Dick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: REF vs OUT

Post by Chris »

Hi Dick,

In this particular case it does not work better, but just in case in the future you make a coding mistake near that code, the compiler is more likely to realize it and point you to it with another warning, instead of the customer finding it at runtime ;)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: REF vs OUT

Post by ic2 »

Hello Chris,

Of course it makes sense what you write. The problem is that any project I restart working on after a few month or more, never works anymore which is frustrating. I must add to that that all the X# errors/ warnings were either quick to solve and those which were not were solved still quickly thanks to your (Robert and you) excellent support.

The real (hours wasting) problem, as so often, was VS. In my .xaml I noticed an Invalid Markup (instead of a preview) which used to work. Despite the 1st line below, in the 2nd line VS said not to have the faintest idea what wv2 was (straight from the still valid MS sample how to use WebView2 in WPF)....

Code: Select all

xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
<wv2:WebView2 Name = "webView"
I have tried the whole bag of tricks, including updates, repairs, cleans, starting over. One thing especially surprised me: when I wanted to add (even once in a freshly started C# Class library) I could only add a WPF user control. At the very same time, in a full test WPF project with that WebView2 control in a WPF window (exactly the same xaml, working) I could add all WPF controls without problem. Do you happen to have an idea why? I would say that I originally also added the WPF window to the Class library using Add/New item.

As so often, after deciding to import the faulty pages using Add Existing item the error was gone. And...it was also gone in the original project (which I had only renamed since)...

I wonder if someone at Microsoft programs some timer based persistent error which totally freaks the user out for a few hours and after the set wasted time it silently starts working again.

At least now you know the background of my comment :shock:

Dick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: REF vs OUT

Post by Chris »

Hi Dick,

Sorry, I am not familiar with WPF, so I cannot help you with that. Maybe someone else can though.

I also do not understand why MS makes things so complicated. They take even the most straightforward things and turn them into complex monsters. Maybe it's an ongoing bet among their managers, who's team comes up with the most complicated solution to the simplest problem...
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply