473,804 Members | 3,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Inherit string type, possible?

Hi!

Is it possible to alter the string type?

Basicly what i want to do is to create my own string type. lets call it
sqlString, when i use this
it will always use replace on the string assigned...

Instead of using:

string mystring = "fdfds'fdfd ";
mystring.replac e("'","''");

i want to do automaticly replace when i assign the value to the sqlString
type.

Hmm, hope you get what i want to do...

Regards
Martin
Nov 17 '05 #1
9 12549

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.net> wrote in
message news:uI******** ******@TK2MSFTN GP09.phx.gbl...
Hi!

Is it possible to alter the string type?

No.
Basicly what i want to do is to create my own string type. lets call it
sqlString, when i use this
it will always use replace on the string assigned...

Instead of using:

string mystring = "fdfds'fdfd ";
mystring.replac e("'","''");

i want to do automaticly replace when i assign the value to the sqlString
type.


You would be better off just creating a seperate type that takes a string
via a constructor and does the work and then spits the string out by
overloading ToString(). You could use implicit conversion operators to make
it look closer to waht yo uwant, but you can't change string itself to do
so.

Better yet, just write a utility method that does the work so you can write

string mystring = FormatSqlString ("fdfds'fdfd ");

and be done with it.
Nov 17 '05 #2
you would be better if you use parameterized commands. (whether you call
stored procedures or dynamic sql.). it does this replacements and much more
for you. (it prevents sql injection automatically.) and code maintanance
will be much easer.
Nov 17 '05 #3
Hi Martin.
In C# 3.0 you will be able to extend the string class.
However, I would use SqlParameter as The Crow suggests.

Happy Selecting
- Michael S

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.net> wrote in
message news:uI******** ******@TK2MSFTN GP09.phx.gbl...
Hi!

Is it possible to alter the string type?

Basicly what i want to do is to create my own string type. lets call it
sqlString, when i use this
it will always use replace on the string assigned...

Instead of using:

string mystring = "fdfds'fdfd ";
mystring.replac e("'","''");

i want to do automaticly replace when i assign the value to the sqlString
type.

Hmm, hope you get what i want to do...

Regards
Martin

Nov 17 '05 #4
Michael S wrote:
In C# 3.0 you will be able to extend the string class.
Just to be clear on this, C# 3.0 won't *really* let you extend the
string class. You won't be able to add extra member variables, override
methods etc. What it *will* do is give you some syntactic sugar to call
your own static methods. So:

string x = "x";
x.Foo("wibble") ;

will be compiled into:

string x = "x";
MyClass.Foo(x, "wibble");

Personally I hope that the syntax changes to make it clearer what's
going on. Something like:

x..Foo("wibble" );

I also hope that the way extension methods are "imported" changes to
give much more control (and make things more explicit). I'd be
surprised if that bit doesn't change, to be honest.
However, I would use SqlParameter as The Crow suggests.


Agreed.

Jon

Nov 17 '05 #5
> Personally I hope that the syntax changes to make it clearer what's
going on. Something like:

x..Foo("wibble" );


I wonder if x:Foo("wibble") or x::Foo("wilbble ") could be used here without
any syntactic or semantic trouble
Nov 17 '05 #6

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Personally I hope that the syntax changes to make it clearer what's
going on. Something like:

x..Foo("wibble" );


I wonder if x:Foo("wibble") or x::Foo("wilbble ") could be used here
without any syntactic or semantic trouble


Yuk, that would make it look like C++. =)

Happy Coding
- Michael S
Nov 17 '05 #7
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
Personally I hope that the syntax changes to make it clearer what's
going on. Something like:

x..Foo("wibble" );


I wonder if x:Foo("wibble") or x::Foo("wilbble ") could be used here without
any syntactic or semantic trouble


x::Foo("wibble" ) would suggest a namespace of "x" to me (given the
global::Stuff of 2.0). Other than that it would probably be okay.
x:Foo("wibble") could have difficulties with the ternary operator. How
would you parse:

string y = z==null ? x:Foo("wibble") :"wibble";

(Whitespace could be anywhere in that.)

It's certainly worth trying to think up some more options though. Nick
suggested -> to me at the summit, but that's already used for pointers
in C#.

I suspect that this part of the language spec will stick as it was
(using just ".") but that the way of importing it will change.

I must get round to blogging about the "Evil" class some time...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
> Personally I hope that the syntax changes to make it clearer what's
> going on. Something like:
>
> x..Foo("wibble" );
I wonder if x:Foo("wibble") or x::Foo("wilbble ") could be used here
without
any syntactic or semantic trouble


x::Foo("wibble" ) would suggest a namespace of "x" to me (given the
global::Stuff of 2.0). Other than that it would probably be okay.
x:Foo("wibble") could have difficulties with the ternary operator. How
would you parse:


Forgot about the ternery operator. I so rarely use it. Anyway, I don't know
if :: would cause that much trouble considering how rarely namespacing is
used within C#(although there is the whole C++ issue).
string y = z==null ? x:Foo("wibble") :"wibble";

(Whitespace could be anywhere in that.)

It's certainly worth trying to think up some more options though. Nick
suggested -> to me at the summit, but that's already used for pointers
in C#.
There has to be something, I agree. I considered -> as well but came to the
same conclusion, and => is used in lambda expressions. I don't really like
the double dots since I do that all to often just due to attention dropping.
I suspect that this part of the language spec will stick as it was
(using just ".") but that the way of importing it will change.


Most likely, its still worth some brain time though.
Nov 17 '05 #9
The right choice for you is to add your dynamic values as Parameter objects
to the command's parameters collection.

The main advantage of using parameters over concatenation is that if the
sqlString is provided a string like "select * from table1;insert" (Just an
example...there could be another insert operation after the semi-colon, while
you may have made provision only for a DataReader!), the DB Engine will
attempt to parse it, wasting its time, and you will get to know of it only at
run-time.

Regards,

Jv

"Visual Systems AB (Martin Arvidsson)" wrote:
Hi!

Is it possible to alter the string type?

Basicly what i want to do is to create my own string type. lets call it
sqlString, when i use this
it will always use replace on the string assigned...

Instead of using:

string mystring = "fdfds'fdfd ";
mystring.replac e("'","''");

i want to do automaticly replace when i assign the value to the sqlString
type.

Hmm, hope you get what i want to do...

Regards
Martin

Nov 17 '05 #10

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

Similar topics

4
2037
by: Mike Jansen | last post by:
Does anyone know why if I create a complexType based off another complexType using xsd:extension the attributes don't seem to be inherited? Is this a bug/non-implementation in the .NET Schema editor and the XML validator? Thanks, Mike Jansen
2
9321
by: Microsoft | last post by:
I have Interface defined with custom attribute. My class implements the same interface , but I'm not able to see any attributes derived from interface.( I did set the inherited to true) .
0
1237
by: Janaka | last post by:
I'm using Web Matrix and a custom built DLL which I've placed in my /bin directory for my website on a Windows 2003 server. I'm using the same setup which worked previously on Windows 2000. Any of the classes in the dll which need to be instantiated work correctly. for example the following is fine: public class Tester { public int Add(int x, int y) {return x + y;}
0
1352
by: Fred W | last post by:
I'm attempting to inherit HttpWebRequest. For argument's sake let's say that one string property will be added. The constructor does not take explicit overloads so I'm looking at the static Create method. Is it possible? public class CustomHttpWebRequest : HttpWebRequest { //additional property public string NewValue;
0
1339
by: djus | last post by:
Hello At the begining I'm sorry for my English :( I want my aspx page TestPage.aspx to inherit from MyClass2. MyClass2 inherits from MyMainClass which is subtype of Page: public class MyMainClass : Page public class MyClass2 : MyMainClass
7
1756
by: Frank | last post by:
Hi, a question probably asked before, but I can't find the answers. Base class X, classes A, B and C inherit class X. In class A I do not want to inherit property (or function or method) P1. Possible? How? Thanks in advance Frank
21
5726
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
19
2449
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; //every class may have this statement self.hello = function() {
11
2724
by: Chris | last post by:
Hi, I'm kind of new to the more involved workings of javascript, and am having a few problems. My problem is this: I am working with a CMS which is including various javascrip files in the pages, including files for a calendar (Dynarch). I would like to add some new functionality to the CMS and add some
0
10571
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...
0
10326
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
10317
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
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
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
7615
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
6851
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
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.