473,396 Members | 1,809 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,396 software developers and data experts.

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.replace("'","''");

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 12527

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> wrote in
message news:uI**************@TK2MSFTNGP09.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.replace("'","''");

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**************@TK2MSFTNGP09.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.replace("'","''");

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****************@TK2MSFTNGP12.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.replace("'","''");

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
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...
2
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
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...
0
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...
0
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...
7
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....
21
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...
19
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; ...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.