out parameter declaration

W

William Stacey

Here an example I just posted earlier. Notice the out uint. In your case
you would use a bool.
Call the first like so:

uint num;
if ( IsUInt("mystring", out num) )
{
//do something with num.
}

===
public static bool IsUInt(string str, out uint val)
{
val = 0;
if ( str == null )
return false;
char [] cArray = str.ToCharArray();
return Bits.IsUInt(cArray, out val);
}

public static bool IsUInt(char[] cArray, out uint val)
{
val = 0;
if ( cArray == null || cArray.Length == 0 )
return false;

if ( cArray.Length > 10 )
return false;

long numPlace = 1;
long num = 0;
byte b = 0;
for (int i = cArray.Length - 1; i >= 0; i--)
{
if (cArray > 57 || cArray < 48)
return false;
b = (byte)(cArray - 48);
num = num + ( b * numPlace );
numPlace = numPlace * 10;
}

if ( num > uint.MaxValue )
return false;

val = (uint)num;
return true;
}

public static bool IsInt(string str, out int val)
{
val = 0;
if ( str == null )
return false;
char [] cArray = str.ToCharArray();
return Bits.IsInt(cArray, out val);
}

public static bool IsInt(char[] cArray, out int val)
{
val = 0;
if ( cArray == null || cArray.Length == 0 )
return false;

if ( cArray.Length > 11 ) //10 max numerics plus one minus sign.
return false;

long numPlace = 1;
long num = 0;
byte b = 0;
int signed = 1;

if ( cArray[0] == '-' )
{
signed = -1;
cArray[0] = '0';
if ( cArray.Length == 1 )
return false;
}

for (int i = cArray.Length - 1; i >= 0; i--)
{
if (cArray > 57 || cArray < 48)
return false;
b = (byte)(cArray - 48);
num = num + ( b * numPlace );
numPlace = numPlace * 10;
}

num = num * signed;

if ( num > int.MaxValue || num < int.MinValue )
return false;

val = (int)num;
return true;
}
 
C

Coy

Hi,

I'm trying to smuggle a bool result back to my ASP.NET layer
from my data layer, via my business layer. I thought I'd just
declare a bool in the web ui code and pass it in a call declared
for an 'out bool' in the business layer. The business layer would
then just pass in on to in a call declared for an 'out bool' in the
data layer. I thought wrong. What are the rules for this?

I keep getting "cannot convert from 'bool' to 'out bool'." I'm not
clear on how to declare the parameter, because it seems I'm just
migrating the compiler error from one layer to another as I try
different combinations. The only thing I'm clear on so far is that
I can't use 'out' or 'ref' outside of a method signature to try and
'cast' my way out of the compiler error. I'm not convinced that
'out' can span more than a single call. I'm also not happy
having to make more than a single assignment to the argument
which I'd like to exclusively do at the data layer. In other words
why can't an 'out' just work like a reference does in C++?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top