C
C#•2mo ago
Shock

Out Parameter Question

I think this should be simple, but I am coming up blank on a method call with an out parameter. (The example below is basic but my actual code requires the parameters) File1.Cs
class Example
{
public void Position(int row, int col, out string txt)
{
StringBuilder Data = new StringBuilder(20);
UInt64 f = (UInt64)(row * col);
txt=Data.ToString();
return f;
}
}
class Example
{
public void Position(int row, int col, out string txt)
{
StringBuilder Data = new StringBuilder(20);
UInt64 f = (UInt64)(row * col);
txt=Data.ToString();
return f;
}
}
If I am trying to call the Position Method in File2.Cs
namespace Example
{
class Example2
{
public void Call()
{
Example p = new Example();
p.Position(1,2,???);
namespace Example
{
class Example2
{
public void Call()
{
Example p = new Example();
p.Position(1,2,???);
What do I put for the last parameter on p.Position?
6 Replies
Angius
Angius•2mo ago
out string s for example Or
string s;
p.Position(1, 2, out s);
string s;
p.Position(1, 2, out s);
or
p.Position(1, 2, out var s);
p.Position(1, 2, out var s);
s can be any name Alternatively, in this particular example... just return that string, no need for an out parameter They're mostly used when you need a "second return" Like in case of .TryParse() methods that return a boolean denoting success, and use the out param to return the parsed value
Shock
ShockOP•2mo ago
Thank you so much. In my actual usage, I'm forced to return the out as a string variable because it's passed to parse depending on the values it contains. I was blanking hard on that one
Angius
Angius•2mo ago
Not sure what you mean here I'm not aware of anything that would force you into having an out parameter Unless you need to assign this method to some Action<int, int, string> But I don't think out params work with delegates
Shock
ShockOP•2mo ago
Ahh - I see. I left a lot of context out in my example, but in my program I'm using a custom dll wrapper that I'm unable to adjust the parameters that are requried to be passed. By nature of usage, there should alwyas be a string output else we won't even get to call this method
Angius
Angius•2mo ago
Ah, gotcha That's a weird design on that dll's part, but needs must
Shock
ShockOP•2mo ago
Oh god you have no clue 💀
Want results from more Discord servers?
Add your server