Connecting Tech Pros Worldwide Help | Site Map

Another NameTable Question

Colin Savage
Guest
 
Posts: n/a
#1: Nov 11 '05
I am trying to work out the best way to use the NameTable class in my C#
application.

I am assuming that getting/adding a string to the nametable has the same
overheads as comparing a string normally, so I have created a class which
holds references to the atomized strings.
Is there a better way to do this? simple example below

//Class to provide "string constants"
private class MyStrings
{
public String stringA;
public String stringB;

public MyStrings(NameTable nt)
{
stringA = nt.Add("A");
stringB = nt.Add("B");
}
}

//Class to do the work
public class MyAppClass
{
private MyStrings myStrings;

//assume strings was created once somewhere else
public MyAppClass(MyStrings strings)
{
myStrings = strings
}

public object factoryMethod(XmlReader reader)
{
//reader was created using the same NameTable used with MyStrings
if (reader.LocalName == strings.stringA) return new ClassA();
if (reader.LocalName == strings.stringB) return new ClassB();
}
}

Thanks
Colin


Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 11 '05

re: Another NameTable Question


Colin Savage wrote:
[color=blue]
> I am trying to work out the best way to use the NameTable class in my C#
> application.
>
> I am assuming that getting/adding a string to the nametable has the same
> overheads as comparing a string normally, so I have created a class which
> holds references to the atomized strings.
> Is there a better way to do this? simple example below[/color]
Looks as the best as possible to me. Preloaded atomized string and
reader built on the same NameTable - I believe nothing more can be done.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

SQL Server Development Team [MSFT]
Guest
 
Posts: n/a
#3: Nov 11 '05

re: Another NameTable Question


Since the XmlReader object and the MyStrings object in your code share the
same nametable you can use a object reference comparison instead of String
comparison.
Something like,
if (( (object)reader.LocalName ) == ( (object)strings.stringA )) return new
ClassA();

Thanks
Srikanth.


--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"Colin Savage" <sav912@hotmail.com> wrote in message
news:bm5s86$n1t$1@ctb-nnrp2.saix.net...[color=blue]
> I am trying to work out the best way to use the NameTable class in my C#
> application.
>
> I am assuming that getting/adding a string to the nametable has the same
> overheads as comparing a string normally, so I have created a class which
> holds references to the atomized strings.
> Is there a better way to do this? simple example below
>
> //Class to provide "string constants"
> private class MyStrings
> {
> public String stringA;
> public String stringB;
>
> public MyStrings(NameTable nt)
> {
> stringA = nt.Add("A");
> stringB = nt.Add("B");
> }
> }
>
> //Class to do the work
> public class MyAppClass
> {
> private MyStrings myStrings;
>
> //assume strings was created once somewhere else
> public MyAppClass(MyStrings strings)
> {
> myStrings = strings
> }
>
> public object factoryMethod(XmlReader reader)
> {
> //reader was created using the same NameTable used with MyStrings
> if (reader.LocalName == strings.stringA) return new ClassA();
> if (reader.LocalName == strings.stringB) return new ClassB();
>
> }
>
> Thanks
> Colin
>
>[/color]


Oleg Tkachenko
Guest
 
Posts: n/a
#4: Nov 11 '05

re: Another NameTable Question


SQL Server Development Team [MSFT] wrote:
[color=blue]
> Since the XmlReader object and the MyStrings object in your code share the
> same nametable you can use a object reference comparison instead of String
> comparison.
> Something like,
> if (( (object)reader.LocalName ) == ( (object)strings.stringA )) return new
> ClassA();[/color]

Which is exactly the same as
if (reader.LocalName == strings.stringA ) return new ClassA();

because the first thing String comparison does is above object comparison.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Closed Thread