By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,470 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,470 IT Pros & Developers. It's quick & easy.

Passing more than one variable from utility function

P: 38
Hello,
I have a utility class that do some db queries that are used by independent pages.
From one of the utility functions I would like to return variables back to the aspx page. In old C this was possible with pointers. But pointers is not supported by C# unless you use the /unsafe mode.

I have two questions:
1. How is such cases solved in C# without using pointers?
2. I have configured the Web.config file to use unsafe compilation (see paste below). And that removed the compilation error for the utility class (Utility.cs). But the aspx (TestPage.aspx.cs) page that is to receive the variables back still fails with "CS0214: Pointers and buffers with fixed size can only be used in unsafe mode"

See a paste of my code below. Anybody know why?
Any help is greatly appreciated.
Best regards
Kurt


Utility.cs file:

Using ....
....
public class Utils
{
public unsafe static bool GetValues(Int32 *iValRet1)
{
bool bSuccess = false;
....
*iValRet1 = myReader.GetInt32(0);
....
return bSuccess;
}
}



TestPage.aspx.cs file:
....
if (Utils.GetValues(&iValRet1, &iValRet2) == true)
....



Web.config file:
<system.codedom>
<compilers>
<!-- zero or more compiler elements -->
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" compilerOptions="/unsafe"
warningLevel="1"/>
</compilers>
</system.codedom>
Jun 29 '07 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
You could just return an array. Or a Collection. Or a custom class.
C# is not bound by the rules of C where you have to know a datasize at compile time.

I could make a function:
Expand|Select|Wrap|Line Numbers
  1. public int[] myfunc(int somearg)
  2. {
  3.    //now I can return any number of int's.
  4. }
  5.  
I prefer to use the System.Collections.ArrayList object. For it's ability to dynamically add/remove entries.

If you're going to delv into the world of C#/.NET, it helps to forget a lot of the "rules" c/c++ imposed on you about what you can and cannot do.
Jun 29 '07 #2

P: 38
Thanks Plater, that looks interesting. I would like to try it out, however I need some more meat on the bone please:

Could you please
1. show me how you assign the return values to the array in the utility class?
2. show how you extract the values from in the calling function?

And a last question (hopefully), will it run through the whole function for each value you extract from the class? (Hopefully not?)

Best regards
Kurt
Jun 29 '07 #3

Plater
Expert 5K+
P: 7,872
Well you have a datareader that you wish to get all the values from and send them back as a number of int's yes?
(I am doing this with out Visual Studio at the moment so there might be typos)

With arraylist:
Expand|Select|Wrap|Line Numbers
  1. public ArrayList myfunc(int somearg)
  2. {
  3.    ArrayList retval=new ArrayList();
  4. //get all your db stuff and whatnot
  5.  
  6. //cycle through your datareader, for every value that you gab:
  7. retval.add(value);//where value could be reaplced with like mydatareader.GetInt32(myindex)
  8.    return retval;
  9. }
  10.  
Now for the int[] array you need to know the size at some point.
Expand|Select|Wrap|Line Numbers
  1. public int[]myfunc(int somearg)
  2. {
  3.    int[] retval=new int[0];//make the initial size of the array zero
  4.    //get all your db stuff and whatnot
  5.  
  6.    //your datareader somewhere tells you how many values it has,
  7.    // lets say you assign it to the integer "NumEntries"
  8.    retval=new int[NumEntries]; 
  9.    //retval can now be addressed with retval[someIndex]. 
  10.    //the indexing goes from 0 to (NumEntries-1)
  11.    //cycle through your datareader, for every value that you gab:
  12.    retval[myindex]=AnIntegerValue;
  13.    // Where you use your datareader to grab the value
  14.    return retval;
  15. }
  16.  
I hope this helps. I stopped using datareader when I found DataSet/DataTable and the DataAdapter object that will fill them for you.

The return'd value from each function can be indexed with the [] brackets. The arraylist object will have to have it's values cast as integers.
But you can still use the foreach() loop on the arraylist.
Jun 29 '07 #4

P: 2
use the "out" keyword

public void CalcVars(out int var1, out int var2)
{
var1 = 3;
var2 = 5;
}

public void UseCalcVars()
{
int v1= 10;
int v2= 20;
CalcVars(out v1, out v2);
//At this point, v1 = 3 and v2 = 5
}
Jun 29 '07 #5

P: 38
Thank you Plater,
that worked like a dream!
best regards
Kurt
Jun 29 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.