473,249 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

Passing Items into a sub

Ok, so I have this sub I wrote, and I create a new instance of a
UserControl:

ctrlAPs tempctrl = new ctrlAPs();

Now, I would like to be able to use this sub I wrote for more than one
UserControl, so I was trying to do something like this:

private void somesub(UserControl sourcectrl)
{
sourcectrl tempctrl = new sourcectrl();
}

But when I do that, I get the following error on compile:
"The type or namespace name 'sourcectrl' could not be found (are you
missing a using directive or an assembly reference?)"

So, I'm obviously not passing this in right. There has to be a way to
do this, as I do it with panels and other controls. Any help is
appreciated.

Aug 28 '07 #1
9 1540
Chris,

You could use Generics, like so:

private void somesub<T>(T sourcectrl) where T : UserControl, new()
{
T tempctrl = new sourcectrl();
}

This will allow you to use the type parameter T and pass in any type
that derives from UserControl as well as has a default, parameterless
constructor.

In the method, you will only be able to call methods that exist on the
UserControl class, nothing that is derived from it.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <co*****@gmail.comwrote in message
news:11********************@g4g2000hsf.googlegroup s.com...
Ok, so I have this sub I wrote, and I create a new instance of a
UserControl:

ctrlAPs tempctrl = new ctrlAPs();

Now, I would like to be able to use this sub I wrote for more than one
UserControl, so I was trying to do something like this:

private void somesub(UserControl sourcectrl)
{
sourcectrl tempctrl = new sourcectrl();
}

But when I do that, I get the following error on compile:
"The type or namespace name 'sourcectrl' could not be found (are you
missing a using directive or an assembly reference?)"

So, I'm obviously not passing this in right. There has to be a way to
do this, as I do it with panels and other controls. Any help is
appreciated.

Aug 28 '07 #2
Based on your example, it looks like generics *might* be the answer,
i.e.

private T somesub<T>() where T : UserControl, new() {
T tempctrl = new T();
// some other code on tempctrl that uses the properties of
UserControl
return tempctrl;
}

The other option is for the caller to create the control locally,
before using your code just to initialize it, in which case you just
need
private void somesub(UserControl control) {
// configure control using the properties of UserControl
}

If not, please clarify what you want to do...

Marc
Aug 28 '07 #3
Hi,

"Chris" <co*****@gmail.comwrote in message
news:11********************@g4g2000hsf.googlegroup s.com...
Ok, so I have this sub I wrote, and I create a new instance of a
UserControl:
sub??? You mean method right? :)
ctrlAPs tempctrl = new ctrlAPs();

Now, I would like to be able to use this sub I wrote for more than one
UserControl, so I was trying to do something like this:

private void somesub(UserControl sourcectrl)
{
sourcectrl tempctrl = new sourcectrl();
}
But when I do that, I get the following error on compile:
"The type or namespace name 'sourcectrl' could not be found (are you
missing a using directive or an assembly reference?)"

So, I'm obviously not passing this in right. There has to be a way to
do this, as I do it with panels and other controls. Any help is
appreciated.
Why are you creating the UserControl inside the method?
What decide which UserControl to create?
How you return the new instance to the calling code?

What if you create the user control outside and pass it to the method?
Aug 28 '07 #4
Yeah, I meant method... grr, still stuck in VB6 mode... Anyways, this
is the whole sub I'm working on:

private void test<T>(Panel targetpanel, string sQuery, string
sQueryParamName, string sParamVal, T sourcectrl) where
T:UserControl,new()
{
string Parameter;
conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=T:\
\RFQMT\\RFQMT Test\\RFQMT Data.mdb;Jet OLEDB:Database
Password=tennis";
OleDbConnection oleconn = new OleDbConnection(conn);
oleconn.Open();

Parameter = txtMemo.Text.ToString();

OleDbCommand cmd = new OleDbCommand(sQuery, oleconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OleDbParameter(sQueryParamName,
sParamVal));
DataTable dt = new DataTable("APs");

OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds, "AP");

DataSource = new DataRowCollectionSocket(ds.AP.Rows);
for (int i = 0; i < DataSource.Count; i++)
{
T tempctrl = new UserControl();
//ctrlAPs tempctrl = new ctrlAPs();
pnlAP.Controls.Add(tempctrl);
tempctrl.Top = i * tempctrl.Height - 2;
tempctrl.Left = 0;

foreach (Control c in ctrlAPs1.Controls)
{
Type ctrlType = c.GetType();
ConstructorInfo cInfo =
ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);

foreach (Binding ctrlBinding in c.DataBindings)
{
string BindingMember =
ctrlBinding.BindingMemberInfo.BindingMember;
string BindingField =
BindingMember.Substring(BindingMember.LastIndexOf( "."));

tempctrl.Controls[c.Name].DataBindings.Clear();

tempctrl.Controls[c.Name].DataBindings.Add(ctrlBinding.PropertyName,
DataSource[i], BindingField);
}
PropertyInfo DSource =
ctrlType.GetProperty("DataSource");
if (DSource != null)
DSource.SetValue(retControl,
DSource.GetValue(c, null), null);
}
ctrlAPs2.Hide();
}
oleconn.Close();
}

Originally, I had the query and usercontrol information hard coded.
It was a 'continuous form' of sorts, like you'd see in an Access
form. Now I had different usercontrols that acted as different
'forms', but I didn't see any need in making one whole method per
control, which is why I'm trying to pass everything into this one.
Basically it clones the control onto a panel and rebinds the controls
in that panel to a dataset. Now like I said, I could pass in the
panel (i.e. which panel on my form I wanted the control copied to),
but the usercontrol I couldn't. That's why my question is, how to
pass in the control information like I can pass in the panel. HTH

On Aug 28, 10:07 am, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
Hi,

"Chris" <coz1...@gmail.comwrote in message

news:11********************@g4g2000hsf.googlegroup s.com...
Ok, so I have this sub I wrote, and I create a new instance of a
UserControl:

sub??? You mean method right? :)
ctrlAPs tempctrl = new ctrlAPs();
Now, I would like to be able to use this sub I wrote for more than one
UserControl, so I was trying to do something like this:
private void somesub(UserControl sourcectrl)
{
sourcectrl tempctrl = new sourcectrl();
}
But when I do that, I get the following error on compile:
"The type or namespace name 'sourcectrl' could not be found (are you
missing a using directive or an assembly reference?)"
So, I'm obviously not passing this in right. There has to be a way to
do this, as I do it with panels and other controls. Any help is
appreciated.

Why are you creating the UserControl inside the method?
What decide which UserControl to create?
How you return the new instance to the calling code?

What if you create the user control outside and pass it to the method?

Aug 28 '07 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Chris,

You could use Generics, like so:

private void somesub<T>(T sourcectrl) where T : UserControl, new()
{
T tempctrl = new sourcectrl();
}

This will allow you to use the type parameter T and pass in any type
that derives from UserControl as well as has a default, parameterless
constructor.

In the method, you will only be able to call methods that exist on the
UserControl class, nothing that is derived from it.
You mean

private void somesub<T>() where T : UserControl, new()
{
T tempctrl = new T();
}

:)
Aug 28 '07 #6
No, I really meant what I typed. I know it doesn't ^do^ anything, but
that's the OP's issue to figure out. I'm assuming the OP has the
prerequisite knowledge to include a return value if needed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"cody" <de********@gmx.dewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
>Chris,

You could use Generics, like so:

private void somesub<T>(T sourcectrl) where T : UserControl, new()
{
T tempctrl = new sourcectrl();
}

This will allow you to use the type parameter T and pass in any type
that derives from UserControl as well as has a default, parameterless
constructor.

In the method, you will only be able to call methods that exist on
the UserControl class, nothing that is derived from it.

You mean

private void somesub<T>() where T : UserControl, new()
{
T tempctrl = new T();
}

:)

Aug 28 '07 #7
Nicholas Paldino [.NET/C# MVP] wrote:
No, I really meant what I typed. I know it doesn't ^do^ anything, but
that's the OP's issue to figure out. I'm assuming the OP has the
prerequisite knowledge to include a return value if needed.
Look again at the correction that was made as it was not about return
values. You created the local variable from the passed in parameter:

T tempctrl = new sourcectrl();

The correction was to create the local variable from the generic type:

T tempctrl = new T();

If you meant to create the local variable from the passed in parameter
then I'll need some education on how that works especially in instances
where sourcectrl is null.
--
Tom Porterfield
Aug 28 '07 #8
Ahh, got it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tom Porterfield" <tp******@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
> No, I really meant what I typed. I know it doesn't ^do^ anything,
but that's the OP's issue to figure out. I'm assuming the OP has the
prerequisite knowledge to include a return value if needed.

Look again at the correction that was made as it was not about return
values. You created the local variable from the passed in parameter:

T tempctrl = new sourcectrl();

The correction was to create the local variable from the generic type:

T tempctrl = new T();

If you meant to create the local variable from the passed in parameter
then I'll need some education on how that works especially in instances
where sourcectrl is null.
--
Tom Porterfield

Aug 28 '07 #9
Thanks folks- Nicholas' suggestion with Marc's / Tom's modification
did the trick!

On Aug 28, 11:47 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Ahh, got it.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Tom Porterfield" <tppor...@mvps.orgwrote in message

news:%2****************@TK2MSFTNGP04.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
No, I really meant what I typed. I know it doesn't ^do^ anything,
but that's the OP's issue to figure out. I'm assuming the OP has the
prerequisite knowledge to include a return value if needed.
Look again at the correction that was made as it was not about return
values. You created the local variable from the passed in parameter:
T tempctrl = new sourcectrl();
The correction was to create the local variable from the generic type:
T tempctrl = new T();
If you meant to create the local variable from the passed in parameter
then I'll need some education on how that works especially in instances
where sourcectrl is null.
--
Tom Porterfield

Aug 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
3
by: Brian Smith | last post by:
I have a .aspx page (we'll call it Form1) with a datagrid on it. The datagrid is populated from a dataset that I manually entered data into (the data isn't in a database). The datagrid on Form1...
2
by: VS_NET_DEV | last post by:
Hi All, There two ways of passing values between pages - 1) Context.Items.Add 2) Context.Handler (cast the page1 class etc) I know for sure #2 does not store the values between requests. How...
8
by: JJ | last post by:
Hi, What's the preferred way to pass variables around to different pages now? Or if my reading servers me right they are retained in memory for the life of the app, correct? How do I access...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
1
by: Vahehoo | last post by:
Hi, I have an ASP .Net e-business site that is built using DNN 2.0. I am having troubles passing my shopping cart items to PayPal. I implemented a total paynow button, but it was not good enough...
1
abehm
by: abehm | last post by:
Hi, I'm trying to pass multiple values through querystrings to another page where i will parse them to individual variables. I have one method to create the querystrings before they pass, but for...
1
BezerkRogue
by: BezerkRogue | last post by:
I have created a page that passes a variables from the start page to a results page that allows operators to edit the data. When I click the submit button, it reruns the page load instruction from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.