473,848 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

baseClass & unknown type object as parameter

hi,

I'm trying to fill a collection using the following 'generic' code:
-----------------
public class baseCollection : System.Collecti ons.CollectionB ase
{
protected void Fill(string strSQL, object oTest)
{
SqlConnection Conn = new SqlConnection(S ettings.sDBConn );
Conn.Open();
SqlCommand cmdContent = new SqlCommand(strS QL, Conn);
SqlDataReader drContent;
drContent = cmdContent.Exec uteReader();
while (drContent.Read ())
{
this.Add(new oTest( drContent.GetIn t32(0) )); ///here is where it
goes wrong
}
drContent.Close ();
Conn.Close();
}
}
-----------------

I have several collection classes that (could) derive from this class.
For example (and I would like to use it in this way, if possible):

public class ContentItemsTes t : baseCollection
{
public ContentItemsTes t(int iType)
{
Fill("SELECT as_ID FROM tblWhhatever" , new myItemClass());
}
}
-------------------------
The "baseCollection .Fill" doesn't know I'm sending a "myItemClas s" as
object, and that's exactly what I want. Unfortunately this doesn't work.
Anyone any ideas?

cheers
Nov 16 '05 #1
4 1814
First, oTest is an instance of an object. New requires a type, not an
instance.

DalePres

"rapataa" <dg@rapataa.fru p> wrote in message
news:c0******** *************** ****@msgid.xeno site.net...
hi,

I'm trying to fill a collection using the following 'generic' code:
-----------------
public class baseCollection : System.Collecti ons.CollectionB ase
{
protected void Fill(string strSQL, object oTest)
{
SqlConnection Conn = new SqlConnection(S ettings.sDBConn );
Conn.Open();
SqlCommand cmdContent = new SqlCommand(strS QL, Conn);
SqlDataReader drContent;
drContent = cmdContent.Exec uteReader();
while (drContent.Read ())
{
this.Add(new oTest( drContent.GetIn t32(0) )); ///here is where
it
goes wrong
}
drContent.Close ();
Conn.Close();
}
}
-----------------

I have several collection classes that (could) derive from this class.
For example (and I would like to use it in this way, if possible):

public class ContentItemsTes t : baseCollection
{
public ContentItemsTes t(int iType)
{
Fill("SELECT as_ID FROM tblWhhatever" , new myItemClass());
}
}
-------------------------
The "baseCollection .Fill" doesn't know I'm sending a "myItemClas s" as
object, and that's exactly what I want. Unfortunately this doesn't work.
Anyone any ideas?

cheers

Nov 16 '05 #2
w
hi-

DalePres is right- you probably wanna say something like:

this.Add(typeof (MyItemClass)oT est);

or assign it earlier:

MyItemClass mIC = (MyItemClass)oT est;

Now all the properties of the class are exposed and you can add whatever you need

ie this.Add(mIC.Na me);

I wasn't able to work out what you were adding to the collection so sorry this is a bad example
First, oTest is an instance of an object. New requires a type, not
an instance.

DalePres

"rapataa" <dg@rapataa.fru p> wrote in message
news:c0******** *************** ****@msgid.xeno site.net...
hi,

I'm trying to fill a collection using the following 'generic' code:
-----------------
public class baseCollection : System.Collecti ons.CollectionB ase
{
protected void Fill(string strSQL, object oTest)
{
SqlConnection Conn = new SqlConnection(S ettings.sDBConn );
Conn.Open();
SqlCommand cmdContent = new SqlCommand(strS QL, Conn);
SqlDataReader drContent;
drContent = cmdContent.Exec uteReader();
while (drContent.Read ())
{
this.Add(new oTest( drContent.GetIn t32(0) )); ///here is where
it
goes wrong
}
drContent.Close ();
Conn.Close();
}
}
-----------------
I have several collection classes that (could) derive from this
class. For example (and I would like to use it in this way, if
possible):

public class ContentItemsTes t : baseCollection
{
public ContentItemsTes t(int iType)
{
Fill("SELECT as_ID FROM tblWhhatever" , new myItemClass());
}
}
-------------------------
The "baseCollection .Fill" doesn't know I'm sending a "myItemClas s" as
object, and that's exactly what I want. Unfortunately this doesn't
work.
Anyone any ideas?
cheers


Nov 16 '05 #3
hmmm, that first construct with typeof won't compile. What were you trying to suggest with that one?

From looking at the code I would guess that the OP needs to use the type name of oTest rather than oTest in the new construct - I assume that it has a constructor that takes an int

But without more details abiout the type of oTest and what the Add method takes its hard to work out what the solution is

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi-

DalePres is right- you probably wanna say something like:

this.Add(typeof (MyItemClass)oT est);

or assign it earlier:

MyItemClass mIC = (MyItemClass)oT est;

Now all the properties of the class are exposed and you can add whatever you need

ie this.Add(mIC.Na me);

I wasn't able to work out what you were adding to the collection so sorry this is a bad example

Nov 16 '05 #4
let's say there's a Plumber object.
The collection object "Plumbers" will be filled based on the Plumbers in
tblPlumber

strSQL = "SELECT ID FROM tblPlumber"

This string is send to the base collection object ( "public class
baseCollection : System.Collecti ons.CollectionB ase" ) together.
The void "Fill" will use the plumber info from the DB to fill the collection
object with objects of type "Plumber". The constructor of Plumber takes an
int.

It's also possible to create a (second) "Add" function in the derived class,
but I think that's butt_ugly.

--------

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:e$******** ******@TK2MSFTN GP09.phx.gbl...
hmmm, that first construct with typeof won't compile. What were you trying to suggest with that one?
From looking at the code I would guess that the OP needs to use the type name of oTest rather than oTest in the new construct - I assume that it has
a constructor that takes an int
But without more details abiout the type of oTest and what the Add method takes its hard to work out what the solution is
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi-

DalePres is right- you probably wanna say something like:

this.Add(typeof (MyItemClass)oT est);

or assign it earlier:

MyItemClass mIC = (MyItemClass)oT est;

Now all the properties of the class are exposed and you can add whatever you need
ie this.Add(mIC.Na me);

I wasn't able to work out what you were adding to the collection so sorry this is a bad example

Nov 16 '05 #5

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

Similar topics

8
1417
by: DKode | last post by:
Ok, here is another question I have that is sort of unrelated to my last posting about composition. I have three Collection classes: EmployeeCollection HourCollection InfractionCollection
11
2459
by: Arpan | last post by:
<% If(False) Then Response.Write("False") Else Response.Write("True") End If %> In the above code, the Else condition will be executed. Why?
5
2662
by: Michel | last post by:
Hi there, What is the best way to serialize unknown data? I have a class that contains a list of parameter objects. The parameter has a value which can be a simple value of a complex class, e.g. class param { object value; }
0
1588
by: Dan Noel | last post by:
I am implementing a DataStore/Settings object that stores objects in XML by serializing the objects in XML and then associating these objects with key names. At some later point, I want to retrieve the object associated with a given keyname, but I am running into trouble deserializing objects into the correct object type. Nearly all of the examples that I have found that use XmlSerializer pass in a type ahead of time, but in my case...
5
3995
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
10
1570
by: Michael Maes | last post by:
Hi, I have a BaseClass from which many Classes Derive. In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties. One of the (Base) Functionalities is to (De)Serialize the Objects. Deserialization is performed in the BaseClass Friend Function PerformDeserialization(ByVal path As String, ByVal user As String) As Object Debug.WriteLine("Settings.BaseSettings.PerformDeserialization")...
15
3544
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
45
2026
by: mdh | last post by:
Hi All, The section is titled Variable-length Argument lists. May I ask a question about the use of "macros". To quote K&R. "The standard header <stdarg.hcontains a set of macro definitions that define how to step through an argument list...... The type va_list is used to declare a variable...in minprintf..ap .......The macro va_start initializes ap to point to the first unnamed argument."
0
9893
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
9738
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
11001
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10722
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
7069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5735
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...
1
4544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3175
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.