Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a function returning multiple values 2

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello!!! I want to create a function with 3 arguments of integers but it's necessary to return 3 values too... So, I created a structure FreePlayings that holds an integer for each field variable.

Public Structure FreePlayings
Dim frees1 As Integer
Dim frees2 As Integer
Dim frees3 As Integer
End Structure

Public Function CheckPcPossiblePlayings(ByVal RvrseRd1 As Integer, ByVal RvrseRd2 As Integer, ByVal RvrseSumRd As Integer) As FreePlayings
Dim frees1, frees2, frees3 As Integer
Dim free1, free2, free3 As Boolean
Dim x As New FreePlayings

For j As Byte = 0 To UsrCheckerGates.Length - 1
If UsrCheckerGates(j).pos <> RvrseRd1 And RvrseRd1 <> 0 Then
free1 = True
frees1 += 1
End If
If UsrCheckerGates(j).pos <> RvrseRd2 And RvrseRd2 <> 0 Then
free2 = True
frees2 += 1
End If
If UsrCheckerGates(j).pos <> RvrseSumRd And RvrseSumRd <> 0 Then
free3 = True
frees3 += 1
End If
Next

x.frees1 = frees1
x.frees2 = frees2
x.frees3 = frees3
Return x

End Function

Unfortunately I'm getting error Error 2 'CheckPcPossiblePlayings' cannot expose type 'Module1.FreePlayings' outside the project through class 'Form1'.
It seems like problem with the returning type of the function.

I' m calling the function in that way in my main program
Dim FreePlngs As New FreePlayings

FreePlngs.frees1 = CheckPcPossiblePlayings(RvrseRd1, RvrseRd2, RvrseSumRd).frees1
FreePlngs.frees2 = CheckPcPossiblePlayings(RvrseRd1, RvrseRd2, RvrseSumRd).frees2
FreePlngs.frees3 = CheckPcPossiblePlayings(RvrseRd1, RvrseRd2, RvrseSumRd).frees3

The problem is not with the way I' m calling the function, because I put it inside comments. Any help please??? What can I do??? Any help will be much appreciated. Thank you so much in advanced.
 

This error has to do with the "Public" part of the function definition. If you do not need to reference this function outside of Form1, then make the function "Private". If you do need to reference the function outside of Form1, then check the visibility level on all objects involved: Form1, the function, the structure and Module1.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
==> I want to create a function with 3 arguments of integers but it's necessary to return 3 values too
Pass the parameters by reference instead of by value.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
Public Function CheckPcPossiblePlayings(ByVal RvrseRd1 As Integer, ByVal RvrseRd2 As Integer, ByVal RvrseSumRd As Integer, ByRef frees1 as Integer, ByRef frees2 as Integer, ByRef frees3 as Integer) As FreePlayings

In the main
frees1 = 0
frees2 = 0
frees3 = 0

CheckPcPossiblePlayings(RvrseRd1, RvrseRd2, RvrseSumRd, frees1, frees2, frees3)



--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
Thank you so much Jebenson!!! It worked!!!! Could you explain me why does this happen??? The error was underlying me the returning type of function FreePlayings and I can't understand why does this happen!!! In my current project, my function can be private, so no problem for that, but what about in any other project if I had to have my function public???
 
CajunCenturion do you mean to declare frees1, frees2, frees3 in my main project
dim frees1, frees2, frees3 as integer
frees1=0
frees2=0
frees3=0

So that when I pass the arguments inside the function byref wouldn't it be nessary to return the values, because the variables frees1, frees2, frees3, declared outside the function will be affected through the function calling??? Can that happen to keep their values from CheckPcPossiblePlayings without the need of returning the values inside the program???
 
==> CajunCenturion do you mean to declare frees1, frees2, frees3 in my main project
Yes.

==> So that when I pass the arguments inside the function byref wouldn't it be nessary to return the values, because the variables frees1, frees2, frees3, declared outside the function will be affected through the function calling???
It would not be necessary for the function to return any variables because the subroutine would be using the reference to access the variables from the main project.





--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
Thank you so much, you saved me for not changing a big ammount of code because I'm using variables frees1, frees2, frees3 after the calling of the function, so I don't have to change variable names in the rest of the code. I am giving you a star too, because your solution helps me in a good way too and saves me for a lot of work!!!
 
Thank you, and I'm glad to have helped.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
However, I would like to know, the reason that I had to correct the function as private. I don't understand one thing. The FreePlayings type is declared as public in the Module1. Why the function in the Form1 class should be private when the returning type is public??? I can't understand that. Can anybody explain that to me???
 
The declaration of Module1 needs to be Public as well. The default access level for Modules is "Friend", so if your module is declared as "Module Module1", it by default has the "Friend" modifier. The "Friend" access level only allows access within the same assembly, whereas "Public" allows access outside the assembly. So in the case of your code in the OP, Form1 was trying to expose an object outside of the assembly (your Public Function return value) when the object is declared in a Module that only allows access within the assembly.

For a full review of access levels, check out this link:




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Just a comment on your original code; you're calling the function 3 times but only need to do it once...

Code:
Dim FreePlngs As New FreePlayings = CheckPcPossiblePlayings(RvrseRd1, RvrseRd2, RvrseSumRd)

Is there any purpose to the Boolean variables in your function? I don't see any.

Since the code is similar for each summary value you could ditch the function and use an Extension method and lambda expression...

Code:
FreePlngs.frees1 = If(RvrseRd1 = 0, 0, UserCheckerGates.Count(Function(u) u.Pos <> RvrseRd1))
FreePlngs.frees2 = If(RvrseRd2 = 0, 0, UserCheckerGates.Count(Function(u) u.Pos <> RvrseRd2))
FreePlngs.frees3 = If(RvrseSumRd = 0, 0, UserCheckerGates.Count(Function(u) u.Pos <> RvrseSumRd))
 
Jebenson thank you so much.
If I well understood in my case if I had declared my Moudule instead of Module Module1, Public Module Module1 then I wouldn't have got the error I mentioned above with my function declared as public because the scope of the variables declared in the Module1 would not be friend but global. Right??????
Thank you so much for your explanation. I like when I have a solution to understand everything arround programming!!!!

DaveInIowa, I don't understand your last 3 lines of code.
Yes there is a purpose of the boolean variables, I missed that excuse me, I passed them by references as arguments in my function too, and I finally changed it from function to subrutine, because when I am passing the frees1, frees2, frees3 by ref, and boolean variables too, I can have a direct affection without returning the values. It's the same thing, I just chose this way because it saves me for changing a lot of code about variable's names (frees1, frees2, frees3, free1, free2, free3) after the subrutine is executed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top