473,657 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code re-use, types, and casting question

In the interest of code re-use, I would like to place some code in a utility
class to be used by other classes. The problem is that this code requires
the following snippet:

for(i=0; i < tbl.Rows.Count; i++)
{
myDataRow = tbl.Rows[i];
company.someNes tedClass sc = new company.someNes tedClass();
for(j=0; j < tbl.Columns.Cou nt; j++)
{

sc.someProperty = Convert.ToBoole an(myDataRow.It emArray[0]);
sc.anotherPrope rty = Convert.ToBoole an(myDataRow.It emArray[1]);
}
myHash.Add(some Key, sc);
}

how can I make company.someNes tedClass sc = new company.someNes tedClass();
into something more generic/variable/unspecific?

Also, can you cast to a cast variable of some sort, so that you don't have
to know, until runtime, what you want to cast to?

As it is, I have to put this code in every class I wish to apply it to.

Thanks in advance.


Nov 15 '05 #1
4 1247
What is:

company.someNes tedClass

Is it a class the end user has created and your utility class is the
'container' for it? If so, this would be a time for generics - which have
only been purposed to be added to c# but as of now - have not.

Hope this helps.

"ITnerd" <no**@nunyabidn ess.com> wrote in message
news:mv******** ************@co mcast.com...
In the interest of code re-use, I would like to place some code in a utility class to be used by other classes. The problem is that this code requires
the following snippet:

for(i=0; i < tbl.Rows.Count; i++)
{
myDataRow = tbl.Rows[i];
company.someNes tedClass sc = new company.someNes tedClass();
for(j=0; j < tbl.Columns.Cou nt; j++)
{

sc.someProperty = Convert.ToBoole an(myDataRow.It emArray[0]);
sc.anotherPrope rty = Convert.ToBoole an(myDataRow.It emArray[1]);
}
myHash.Add(some Key, sc);
}

how can I make company.someNes tedClass sc = new company.someNes tedClass();
into something more generic/variable/unspecific?

Also, can you cast to a cast variable of some sort, so that you don't have
to know, until runtime, what you want to cast to?

As it is, I have to put this code in every class I wish to apply it to.

Thanks in advance.

Nov 15 '05 #2
company.someNes tedClass is the class that I am adding to each of my main
classes (company, employee, etc.) that holds the code below.
someNestedClass has all the same properties no matter which top level class
I add it to (hardcoded, not dynamically added or anything), and the snippet
below is inside a function in someNestedClass .

If by "generics" you mean "templates" as in C++...yes, that is probably what
I need. I didn't think templates at first because I'm not a C++
programmer...I just sort of wished on my own and of course, I am not unique
in my desire, I see.

Maybe through reflection I could build someNestedClass dynamically? Would I
still run into the same problem?

"michael" <mp********@sbc global.net> wrote in message
news:#Q******** ******@TK2MSFTN GP09.phx.gbl...
What is:

company.someNes tedClass

Is it a class the end user has created and your utility class is the
'container' for it? If so, this would be a time for generics - which have
only been purposed to be added to c# but as of now - have not.

Hope this helps.

"ITnerd" <no**@nunyabidn ess.com> wrote in message
news:mv******** ************@co mcast.com...
In the interest of code re-use, I would like to place some code in a

utility
class to be used by other classes. The problem is that this code requires the following snippet:

for(i=0; i < tbl.Rows.Count; i++)
{
myDataRow = tbl.Rows[i];
company.someNes tedClass sc = new company.someNes tedClass();
for(j=0; j < tbl.Columns.Cou nt; j++)
{

sc.someProperty = Convert.ToBoole an(myDataRow.It emArray[0]);
sc.anotherPrope rty = Convert.ToBoole an(myDataRow.It emArray[1]);
}
myHash.Add(some Key, sc);
}

how can I make company.someNes tedClass sc = new company.someNes tedClass(); into something more generic/variable/unspecific?

Also, can you cast to a cast variable of some sort, so that you don't have to know, until runtime, what you want to cast to?

As it is, I have to put this code in every class I wish to apply it to.

Thanks in advance.


Nov 15 '05 #3
M
I'm guessing no one answered because the answer is "use templates" and C# doesn't offer them yet?
ITnerd wrote:
company.someNes tedClass is the class that I am adding to each of my main
classes (company, employee, etc.) that holds the code below.
someNestedClass has all the same properties no matter which top level class
I add it to (hardcoded, not dynamically added or anything), and the snippet
below is inside a function in someNestedClass .

If by "generics" you mean "templates" as in C++...yes, that is probably what
I need. I didn't think templates at first because I'm not a C++
programmer...I just sort of wished on my own and of course, I am not unique
in my desire, I see.

Maybe through reflection I could build someNestedClass dynamically? Would I
still run into the same problem?

"michael" <mp********@sbc global.net> wrote in message
news:#Q******** ******@TK2MSFTN GP09.phx.gbl...
What is:

company.someN estedClass

Is it a class the end user has created and your utility class is the
'container' for it? If so, this would be a time for generics - which have
only been purposed to be added to c# but as of now - have not.

Hope this helps.

"ITnerd" <no**@nunyabidn ess.com> wrote in message
news:mv****** **************@ comcast.com...
In the interest of code re-use, I would like to place some code in a


utility
class to be used by other classes. The problem is that this code
requires
the following snippet:

for(i=0; i < tbl.Rows.Count; i++)
{
myDataRow = tbl.Rows[i];
company.someNes tedClass sc = new company.someNes tedClass();
for(j=0; j < tbl.Columns.Cou nt; j++)
{

sc.someProperty = Convert.ToBoole an(myDataRow.It emArray[0]);
sc.anotherPrope rty = Convert.ToBoole an(myDataRow.It emArray[1]);
}
myHash.Add(some Key, sc);
}

how can I make company.someNes tedClass sc = new
company.someNes tedClass();
into something more generic/variable/unspecific?

Also, can you cast to a cast variable of some sort, so that you don't
have
to know, until runtime, what you want to cast to?

As it is, I have to put this code in every class I wish to apply it to.

Thanks in advance.




Nov 15 '05 #4
I am not certain if I understand the issue here correctly so please feel
free to tell me I am missing the point. It seems to me that there are two
separate issues involved. One is easy to solve, the other is a bit more
complex.

First the easy one: This code needs to work with an arbitrary set of
classes, but needs to treat them as if they were the same kind of class
(i.e. perform the same operations on the same members of each class). If
you have control of the implementation each of these arbitrary classes, why
not do one of the following? Define an interface containing someProperty
and anotherProperty and then have each class to be handled by this code
implement that interface. Alternately, define a class that implements these
members and then derive all you other classes from it. This is more
limiting but even better code reuse, if all the classes have a common
implementation for the common members .

The second problem I see is the need to instantiate new instances of the
arbitrary class within the utility function. Since you have no way of
knowing ahead of time what class will need to be instantiated, this is an
apparent issue. To solve it, I would add a GetNewInstance( ) method to my
interface above. Then I would pass into this routine an exemplary instance
of the class it needs to be creating. Each time a new instance of the class
is needed, it calls GetNewInstance on the exemplar instance. In this way
GetNewInstance is acting as a call back function. The GetNewInstance( )
method of the exemplar will always create a new instance of the correct
class but should return it under the guise of the common interface. There
are variations on this solution. Is myHash handed into the routine from
outside? If so, a set of custom hashtable classes that implement the
GetNewInstance member or equivalent might be better way of implementing the
call back function.

Anyway, I hope this helps.

--Ken

Does this help? It
"ITnerd" <no**@nunyabidn ess.com> wrote in message
news:mv******** ************@co mcast.com...
In the interest of code re-use, I would like to place some code in a utility class to be used by other classes. The problem is that this code requires
the following snippet:

for(i=0; i < tbl.Rows.Count; i++)
{
myDataRow = tbl.Rows[i];
company.someNes tedClass sc = new company.someNes tedClass();
for(j=0; j < tbl.Columns.Cou nt; j++)
{

sc.someProperty = Convert.ToBoole an(myDataRow.It emArray[0]);
sc.anotherPrope rty = Convert.ToBoole an(myDataRow.It emArray[1]);
}
myHash.Add(some Key, sc);
}

how can I make company.someNes tedClass sc = new company.someNes tedClass();
into something more generic/variable/unspecific?

Also, can you cast to a cast variable of some sort, so that you don't have
to know, until runtime, what you want to cast to?

As it is, I have to put this code in every class I wish to apply it to.

Thanks in advance.

Nov 15 '05 #5

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

Similar topics

0
1951
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead of escaping the \n's. A repr function that output triple-quoted strings with explicit (non-escaped) linebreaks would be perfect.
4
2595
by: Irmen de Jong | last post by:
Hello, I don't understand why the following doesn't work. What I want to do is dynamically import some generated Python code and I'm doing this using compile and exec'ing it in the dict of a new empty module object. That works okay, but as soon as the generated code tries do perform certain imports, it fails! Certain other imports succeed. Consider this example code:
4
1593
by: Noen | last post by:
Im developing a game where the players will program their equipment with python. Are there any ways to run insecure code? I dont want the clients to mess with the server-code through their own code, or even DOS the box by using up too much memory. Here is some examples of how the equipment should be programmed: --- # Proxmity explosive example import cpu
1
2877
by: Brian Beck | last post by:
Hi. I'm having some problems with code based directly on the following httplib documentation code: http://www.zvon.org/other/python/doc21/lib/httplib-examples.html I've included the code and traceback at the end of this post. The odd thing is, using DEPRECATED FUNCTIONS to perform the same function works fine!
1
3751
by: Amit Kela | last post by:
Hey, I am getting the following error when I try to delete the contents of a recordset under a condition statement. Is there any way I can delete them without running into this error? I need to delete the contents in the recordset 'rs' to delete the items from the shopping cart once the order is placed. Thanks, Amit Error Type: ADODB.Recordset (0x800A0CB3)
3
1403
by: Wayne... | last post by:
I'm trying to make a sort of source code libary for some customers of commonly used code to save them a bit of time the problem I have is that although I can get the asp code into a field of an access database it will not be called back as text.... an example of what I am using is below... all I am after is basically what lots of other major source code sites do and that is diplay the code as text so the user can cut and paste it into...
5
2077
by: Otto Wyss | last post by:
I've written an application which is intended to act as a code sample "how to code applications with a well designed GUI". Since this code is intended to also act as a sample for beginners it should not contain any quirks or unusual constructs or be misleading or else. It should be as clean and as readable as possible. It simple should be how you would code. I therefore ask for a public code review. Just criticize, make annotations,...
2
29806
by: Mark Anderson | last post by:
Problem in short: user is moving (clicking a link) from my page before some JS code is run (to write a cookie). The code does not run (in Body's onLoad event) until the page loads as there are a number of images which can take a while to load on a slow connection. Using fewer/different graphics is not an option! The code writing a cookie the is called in the Body's onLoad event: <body onLoad="setLast();">
16
3100
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
1
1525
by: ahammad | last post by:
Hello, I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied properly, I had to remove some of the things that are not part of the data, but are simply headings. Here is what I mean: *Heading <CODE1> = <COMMAND1> <CODE2> = <COMMAND2> The program reads in each line, and separates <CODE1> from <COMMAND1> at...
0
8392
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
8732
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
8503
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
7324
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
6163
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
5632
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
4151
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...
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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.