473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(UserCon trol 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 1564
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.co m

"Chris" <co*****@gmail. comwrote in message
news:11******** ************@g4 g2000hsf.google groups.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(UserCon trol 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(UserCon trol 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******** ************@g4 g2000hsf.google groups.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(UserCon trol 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,n ew()
{
string Parameter;
conn = "Provider=Micro soft.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.To String();

OleDbCommand cmd = new OleDbCommand(sQ uery, oleconn);
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add(new OleDbParameter( sQueryParamName ,
sParamVal));
DataTable dt = new DataTable("APs" );

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

DataSource = new DataRowCollecti onSocket(ds.AP. Rows);
for (int i = 0; i < DataSource.Coun t; 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.Contro ls)
{
Type ctrlType = c.GetType();
ConstructorInfo cInfo =
ctrlType.GetCon structor(Type.E mptyTypes);
Control retControl = (Control)cInfo. Invoke(null);

foreach (Binding ctrlBinding in c.DataBindings)
{
string BindingMember =
ctrlBinding.Bin dingMemberInfo. BindingMember;
string BindingField =
BindingMember.S ubstring(Bindin gMember.LastInd exOf("."));

tempctrl.Contro ls[c.Name].DataBindings.C lear();

tempctrl.Contro ls[c.Name].DataBindings.A dd(ctrlBinding. PropertyName,
DataSource[i], BindingField);
}
PropertyInfo DSource =
ctrlType.GetPro perty("DataSour ce");
if (DSource != null)
DSource.SetValu e(retControl,
DSource.GetValu e(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******** ************@g4 g2000hsf.google groups.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(UserCon trol 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.co m

"cody" <de********@gmx .dewrote in message
news:%2******** ********@TK2MSF TNGP06.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.co m

"Tom Porterfield" <tp******@mvps. orgwrote in message
news:%2******** ********@TK2MSF TNGP04.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.guar d.caspershouse. comwrote:
Ahh, got it.

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

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

news:%2******** ********@TK2MSF TNGP04.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
3591
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 in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
7
2852
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 parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
3
12660
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 also has a button column on it. I have another .aspx page (Form2) with an unpopulated datagrid on it. I want to populate it with all the columns from the row where the button was clicked on Form1. I know how to do what I want with the datagrid
2
2006
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 about #1. Does it store values between requests? i.e. The Context.Items. collection A)Is it like the Session variables?
8
2104
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 these variables if in a different page than the one variable was created in? Thanks, JJ
4
9861
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 based on their data and some other data as well. i planned on passing it by reference into a function that modified their values. but it gave me the error "you cannot pass indexers and properties by reference". The only way i know of doing it is...
5
1901
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 main class whenever it is needed: class shop_main { var $prices = null; function &get_prices() {
1
2096
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 for my customer. I found some PayPal ASP.Net controls to be used with .net studio. Basically, I want the user to be able to shop on my website, add items to my shopping cart and at the checkout to be taken to the PayPal website. I would like...
1
1333
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 some reason nothing is being passed. Can anyone help me? protected void btnNext2_Click(object sender, EventArgs e) { if (MessageBox.Show("Proceed?", "Test Maintenance", MessageBoxButtons.YesNo) == DialogResult.Yes) ...
1
1239
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 the edit page and crashes instead of passing the variables to the final page. Here is the cmdSubmit code. Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click Dim strShift = lblShift...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.