Parameter passing question

B

Blip

The title of this is part of my problem. I'm a hobbiest programmer -
playing w/ c# now for a little more than a year. The generic problem
that stops me most often has to do with passing parameters between
windows forms & their related classes. The number of "cannot resolve
symbol" whatever drives me crazy at times.

I'm fairly comfortable w/ private/public/etc modifiers -- my confusion
seems to me more basic than that. I have a terrible time populating
controls on a form w/ info from the realted class -- seems like I have
to jump through all sorts of hoops to get it to work(sometimes).

Are there some general rules of thumb for how experienced folks go
about doing this?

for example:

namespace Tester

public class Netstuff {

public static bool GetMyAdaptersInfo(out IP_ADAPTER_INFO info)
{
int outBufLen = 0;
IntPtr pAdapterInfo = IntPtr.Zero;
int ret = 0;
try
{
ret = GetAdaptersInfo(pAdapterInfo, ref outBufLen);
// Allocate enough memory to hold the IP_ADAPTER_INFO
data.
pAdapterInfo =
CustomMarshal.AllocHLocal((uint)outBufLen); //uint

ret = GetAdaptersInfo(pAdapterInfo, ref outBufLen);
// ret will be zero if successful
if (ret != 0)
{
// The call to GetAdaptersInfo failed.
info = null;
return false;
}
else
{
// Success! Create a new instance of the
IP_ADAPTER_INFO structure with the data from the memory buffer.
info = new IP_ADAPTER_INFO(pAdapterInfo);
}
}
finally
{
// Free up any allocated memory.
CustomMarshal.FreeHLocal(pAdapterInfo);
}
// Call to GetAdaptersInfo was successful so return true.
return true;
}
}

Form1:

namespace Tester

public Netstuff n; {


public Form1() {

invoker += new Netstuff.SpeedUpdateHandler(speed);
n = new Netstuff(this, invoker);
n.SpeedUpdate += new
InitializeComponent();
}

private void buttonDisplayStats_Click(object sender, EventArgs
e)
{

bool b = Netstuff.GetMyAdaptersInfo(out n.info); <---
if (b)
frm2.PrintToTextbox(n.info);

}
}

The arrow marks the only way I know to move things from the Netstuff
class to controls on my form. My understanding (which is wrong) is
that I should be able to use "n" - the instance of Netstuff to access
members & properties of the Netstuff class, but that rarely works.

For example, I can't get at pAdapterInfo from Form1 (as n.
pAdapterInfo) What the heck am I conceptually missing? And thanks for
taking the time toi read through & answer this.

Tom
 
K

KWienhold

The title of this is part of my problem. I'm a hobbiest programmer -
playing w/ c# now for a little more than a year. The generic problem
that stops me most often has to do with passing parameters between
windows forms & their related classes. The number of "cannot resolve
symbol" whatever drives me crazy at times.

I'm fairly comfortable w/ private/public/etc modifiers -- my confusion
seems to me more basic than that. I have a terrible time populating
controls on a form w/ info from the realted class -- seems like I have
to jump through all sorts of hoops to get it to work(sometimes).

Are there some general rules of thumb for how experienced folks go
about doing this?

for example:

namespace Tester

public class Netstuff {

public static bool GetMyAdaptersInfo(out IP_ADAPTER_INFO info)
{
int outBufLen = 0;
IntPtr pAdapterInfo = IntPtr.Zero;
int ret = 0;
try
{
ret = GetAdaptersInfo(pAdapterInfo, ref outBufLen);
// Allocate enough memory to hold the IP_ADAPTER_INFO
data.
pAdapterInfo =
CustomMarshal.AllocHLocal((uint)outBufLen); //uint

ret = GetAdaptersInfo(pAdapterInfo, ref outBufLen);
// ret will be zero if successful
if (ret != 0)
{
// The call to GetAdaptersInfo failed.
info = null;
return false;
}
else
{
// Success! Create a new instance of the
IP_ADAPTER_INFO structure with the data from the memory buffer.
info = new IP_ADAPTER_INFO(pAdapterInfo);
}
}
finally
{
// Free up any allocated memory.
CustomMarshal.FreeHLocal(pAdapterInfo);
}
// Call to GetAdaptersInfo was successful so return true.
return true;
}

}

Form1:

namespace Tester

public Netstuff n; {

public Form1() {

invoker += new Netstuff.SpeedUpdateHandler(speed);
n = new Netstuff(this, invoker);
n.SpeedUpdate += new
InitializeComponent();

}

private void buttonDisplayStats_Click(object sender, EventArgs
e)
{

bool b = Netstuff.GetMyAdaptersInfo(out n.info); <---
if (b)
frm2.PrintToTextbox(n.info);

}

}

The arrow marks the only way I know to move things from the Netstuff
class to controls on my form. My understanding (which is wrong) is
that I should be able to use "n" - the instance of Netstuff to access
members & properties of the Netstuff class, but that rarely works.

For example, I can't get at pAdapterInfo from Form1 (as n.
pAdapterInfo) What the heck am I conceptually missing? And thanks for
taking the time toi read through & answer this.

Tom

Unfortunately the code you posted is not quite complete, so I'm not
sure I actually understand your problem... The only pAdapterInfo I can
see is defined inside the GetMyAdaptersInfo-Function, so it will only
be available inside that function, you can't access it from the
outside.
If you want to be able to access it from the outside it would have to
be a public (or internal) field of the class, or a private field with
a property to provide access to it.
However, since GetMyAdaptersInfo is static, the field would have to be
static as well.
In addition, having a field/property that is accessible from the
outside, but only becomes initialized once another function is called
may not be the best design decision in this case.
If you could elaborate on your problem and maybe post an example that
highlights it, it would probably be easier to give general advice,
without getting hung up on the specifics of the code.

hth,
Kevin Wienhold
 
H

Hans Kesting

Blip wrote :
The title of this is part of my problem. I'm a hobbiest programmer -
playing w/ c# now for a little more than a year. The generic problem
that stops me most often has to do with passing parameters between
windows forms & their related classes. The number of "cannot resolve
symbol" whatever drives me crazy at times.

I'm fairly comfortable w/ private/public/etc modifiers -- my confusion
seems to me more basic than that. I have a terrible time populating
controls on a form w/ info from the realted class -- seems like I have
to jump through all sorts of hoops to get it to work(sometimes).

Are there some general rules of thumb for how experienced folks go
about doing this?

for example:

namespace Tester

public class Netstuff {

public static bool GetMyAdaptersInfo(out IP_ADAPTER_INFO info)
{ *snip*

return true;
}
}

Form1:

namespace Tester

public Netstuff n; {


public Form1() {

invoker += new Netstuff.SpeedUpdateHandler(speed);
n = new Netstuff(this, invoker);
n.SpeedUpdate += new
InitializeComponent();
}

private void buttonDisplayStats_Click(object sender, EventArgs
e)
{

bool b = Netstuff.GetMyAdaptersInfo(out n.info); <---
if (b)
frm2.PrintToTextbox(n.info);

}
}

I think you would be better off if GetMyAdaptersInfo was not static.
That way you don't need the "out" parameter but you can set the field
"info" directly.

You have not shown what "info" is in your Netstuff. I'm guessing it's a
property instead of a "field". You cannot use a property as
out-parameter, because (under the covers) it is really a function and
the parameter needs some address to store the out-value.

Hans Kesting
 

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