By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

Creating an object that can be treated as a string?

P: n/a
Is it possible to create an object which can have methods and properties,
but which can also be treated as a string?

I'm trying to create a wrapper around the IIS Request.Form object which
behaves the same as the classic ASP object behaved. This will allow me to
quickly get a large amount of code up and running (I can then tweak it to
not require this wrapper at a more leisurely pace).

When queried directly, this object returns a string containing elements
submitted on a web form, for example:

Debug.Print MyObj.Form
Field1=Value1&Field2=Value2&Checkbox=on

It is also possible to query properties on this same object, however, as
follows:

Debug.Print MyObj.Form.Count
3

Debug.Print MyObj.Form.Item("Field1")
Value1

Is there any way to create an object in VB.net which behaves in the same way
as this?

Many thanks,

--

(O)enone
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You can implement the ToString method for your class and use it to return
whatever string you want.

http://www.devx.com/tips/Tip/14846

Telmo Sampaio

"Oenone" <no***@nowhere.com> wrote in message
news:uC**************@tk2msftngp13.phx.gbl...
Is it possible to create an object which can have methods and properties,
but which can also be treated as a string?

I'm trying to create a wrapper around the IIS Request.Form object which
behaves the same as the classic ASP object behaved. This will allow me to
quickly get a large amount of code up and running (I can then tweak it to
not require this wrapper at a more leisurely pace).

When queried directly, this object returns a string containing elements
submitted on a web form, for example:

Debug.Print MyObj.Form
Field1=Value1&Field2=Value2&Checkbox=on

It is also possible to query properties on this same object, however, as
follows:

Debug.Print MyObj.Form.Count
3

Debug.Print MyObj.Form.Item("Field1")
Value1

Is there any way to create an object in VB.net which behaves in the same way as this?

Many thanks,

--

(O)enone

Nov 20 '05 #2

P: n/a
Hi Telmo,

Thanks for the suggestion -- but I can't get it to work, could you possible
help me a bit more?

The code I have in my class (cut down for brevity) it as follows:

--

(O)enone

Telmo Sampaio wrote:
You can implement the ToString method for your class and use it to
return whatever string you want.

http://www.devx.com/tips/Tip/14846

Telmo Sampaio

"Oenone" <no***@nowhere.com> wrote in message
news:uC**************@tk2msftngp13.phx.gbl...
Is it possible to create an object which can have methods and
properties, but which can also be treated as a string?

I'm trying to create a wrapper around the IIS Request.Form object
which behaves the same as the classic ASP object behaved. This will
allow me to quickly get a large amount of code up and running (I can
then tweak it to not require this wrapper at a more leisurely pace).

When queried directly, this object returns a string containing
elements submitted on a web form, for example:

Debug.Print MyObj.Form
Field1=Value1&Field2=Value2&Checkbox=on

It is also possible to query properties on this same object,
however, as follows:

Debug.Print MyObj.Form.Count
3

Debug.Print MyObj.Form.Item("Field1")
Value1

Is there any way to create an object in VB.net which behaves in the
same way as this?

Many thanks,

--

(O)enone

Nov 20 '05 #3

P: n/a
Hi Telmo,
You can implement the ToString method for your class and use it to
return whatever string you want.


(Sorry if this is a double-post, the previous one sent before I'd finished
writing it).

Thanks for the tip -- but unfortunately I haven't been able to get it to
work. Could you have a look at my code and see if there's something I'm
doing wrong?

The code for my class (cut down quite a bit for brevity) it as follows:
Public Class IISWrapperDictionaryClass

Private myDictionary As
System.Collections.Specialized.NameValueCollection

Public Sub New(ByVal NewDictionary As
System.Collections.Specialized.NameValueCollection )
myDictionary = NewDictionary
End Sub

Default Public ReadOnly Property Item(ByVal ItemId As String) As String
Get
Item = myDictionary.Item(ItemId)
End Get
End Property

Public Function Count() As Integer
Count = myDictionary.Count
End Function

Public Overrides Function ToString() As String
ToString = "Hello world"
End Function

End Class
Everything is working fine -- I can execute the following to find the number
of items:

?IIS.Form.Count
5

(IIS.Form returns an instance of IISWrapperDictionaryClass).

However, if I try:

?IIS.Form

....this is where I was hoping it would return the "Hello world" string. But
instead I get a breakdown of the entire class when I execute in the command
window:

Item: <cannot view indexed property>
myDictionary: {System.Web.HttpValueCollection}

If I try using CStr() then I get an error, "Value of type
'IISWrapperDictionaryClass' cannot be converted to 'String'."

Any suggestions?

--

(O)enone
Nov 20 '05 #4

P: n/a
Oenone,
However, if I try:

?IIS.Form
Try:

? IIS.Form.ToString

Hope this helps
Jay

"Oenone" <no***@nowhere.com> wrote in message
news:us*************@TK2MSFTNGP10.phx.gbl... Hi Telmo,
You can implement the ToString method for your class and use it to
return whatever string you want.
(Sorry if this is a double-post, the previous one sent before I'd finished
writing it).

Thanks for the tip -- but unfortunately I haven't been able to get it to
work. Could you have a look at my code and see if there's something I'm
doing wrong?

The code for my class (cut down quite a bit for brevity) it as follows:
Public Class IISWrapperDictionaryClass

Private myDictionary As
System.Collections.Specialized.NameValueCollection

Public Sub New(ByVal NewDictionary As
System.Collections.Specialized.NameValueCollection )
myDictionary = NewDictionary
End Sub

Default Public ReadOnly Property Item(ByVal ItemId As String) As

String Get
Item = myDictionary.Item(ItemId)
End Get
End Property

Public Function Count() As Integer
Count = myDictionary.Count
End Function

Public Overrides Function ToString() As String
ToString = "Hello world"
End Function

End Class
Everything is working fine -- I can execute the following to find the number of items:

?IIS.Form.Count
5

(IIS.Form returns an instance of IISWrapperDictionaryClass).

However, if I try:

?IIS.Form

...this is where I was hoping it would return the "Hello world" string. But instead I get a breakdown of the entire class when I execute in the command window:

Item: <cannot view indexed property>
myDictionary: {System.Web.HttpValueCollection}

If I try using CStr() then I get an error, "Value of type
'IISWrapperDictionaryClass' cannot be converted to 'String'."

Any suggestions?

--

(O)enone

Nov 20 '05 #5

P: n/a
Hi Jay,

Try:

? IIS.Form.ToString


Thanks for your response -- that works fine, but it isn't what I'm after.
:-) Perhaps I should explain a bit more.

I have a large volume of VB6 DLLs which I want to be able to call this class
thinking that it's the original ASP (not ASP.net) Form object. They
currently all late-bind to the real ASP Form object so providing I can make
the interface act the same they should all carry on working fine (otherwise
I have a couple of hundred DLLs that will all need to be rewritten before
than can work with my VB.net/ASP.net application, which isn't an option I
have available to me).

There are three ways they can access the ASP Form object:

IIS.Form
returns a complete breakdown of the Form content, e.g.:
Field1=Value1&Field2=Value2&Field3=Value3

IIS.Form("Field1")
returns the value of the named Form field, e.g.:
Value1

IIS.Form.Count
(or other properties/methods), calls the appropriate member code,
e.g.:
3

I have the second and third items working but not the first.

I tried defining the Item property so that its parameter (ItemId) was
optional, thinking that might do the trick. But VB told me that "properties
with no required parameters cannot be declared 'Default'", which put an end
to that idea.

Or am I just approaching this wrong? The only other thing I can think of
doing is writing a COM DLL wrapper in VB6 itself (in which I know exactly
how to do this) which builds up the IIS.Form return value when the default
property (Item) is called with no parameters. However I'd really like to
avoid having this second layer of wrappers if I can help it.

My thanks in advance,

--

(O)enone
Nov 20 '05 #6

P: n/a
Oenone,
Thanks for your response -- that works fine, but it isn't what I'm after.
:-) Perhaps I should explain a bit more. I believe I mostly knew what you are after, unfortunately this is what you
get. :-(
I have a large volume of VB6 DLLs which I want to be able to call this class thinking that it's the original ASP (not ASP.net) Form object. They
currently all late-bind to the real ASP Form object so providing I can make the interface act the same they should all carry on working fine (otherwise I have a couple of hundred DLLs that will all need to be rewritten before
than can work with my VB.net/ASP.net application, which isn't an option I
have available to me). Are you writing a VB.NET component or a VB6 component? Is it going to be
used in VB.NET, VB6, or VBScript?
Or am I just approaching this wrong? Your approaching it wrong as Default properties are not supported. Also
VB.NET 2003 has no real support on what you are attempting if VB.NET is the
target (other then adding a "ToWhatever" method).

If you are attempting to write a VB.NET component that exposes a COM default
property to VB6, I would recommend asking "down the hall" in the
microsoft.public.dotnet.framework.interop newsgroup. As what you want really
deals with assigning dispatch Ids & Interop. I believe you can use the
System.Runtime.InteropServices.DispIdAttribute to do what you want, however
I have not actually tried it.

http://msdn.microsoft.com/library/de...classtopic.asp

If you are writing a VB.NET component for use in VB.NET then:
The only other thing I can think of
doing is writing a COM DLL wrapper in VB6 itself (in which I know exactly
how to do this) which builds up the IIS.Form return value when the default
property (Item) is called with no parameters. However I'd really like to
avoid having this second layer of wrappers if I can help it. The default property in VB6 will not be honored in VB.NET only index
properties can be Default in .NET.
The closest VB.NET to VB.NET you may get is an implicit conversion operator,
which is not available in VB.NET 2003. It will be available in VB.NET 2005
(Whidbey) due out sometime in 2005.
Public Class Complex

Private ReadOnly m_real As Double
Private ReadOnly m_imag As Double

Public Sub New(ByVal real As Double)
MyClass.New(real, 0)
End Sub

Public Sub New(ByVal real As Double, ByVal imag As Double)
m_real = real
m_imag = imag
End Sub

' Define conversion from Double to Complex
Public Shared Widening Operator CType(ByVal real As Double) As Complex
Return New Complex(real)
End Operator

' Define conversion from Complex to Double
Public Shared Narrowing Operator CType(ByVal c As Complex) As Double
If c.imag <> 0 Then Throw New InvalidCastException
Return c.m_real
End Operator

End Class

However be careful with have a narrowing operator from your class to a base
type, as you may loose data (narrowing).

Public Shared Narrowing Operator CType(ByVal iis As IIS) As String

However the conversion operator smells like bad code for what you are
asking...

Hope this helps
Jay

"Oenone" <no***@nowhere.com> wrote in message
news:Ok**************@TK2MSFTNGP11.phx.gbl... Hi Jay,

Try:

? IIS.Form.ToString
Thanks for your response -- that works fine, but it isn't what I'm after.
:-) Perhaps I should explain a bit more.

I have a large volume of VB6 DLLs which I want to be able to call this

class thinking that it's the original ASP (not ASP.net) Form object. They
currently all late-bind to the real ASP Form object so providing I can make the interface act the same they should all carry on working fine (otherwise I have a couple of hundred DLLs that will all need to be rewritten before
than can work with my VB.net/ASP.net application, which isn't an option I
have available to me).

There are three ways they can access the ASP Form object:

IIS.Form
returns a complete breakdown of the Form content, e.g.:
Field1=Value1&Field2=Value2&Field3=Value3

IIS.Form("Field1")
returns the value of the named Form field, e.g.:
Value1

IIS.Form.Count
(or other properties/methods), calls the appropriate member code,
e.g.:
3

I have the second and third items working but not the first.

I tried defining the Item property so that its parameter (ItemId) was
optional, thinking that might do the trick. But VB told me that "properties with no required parameters cannot be declared 'Default'", which put an end to that idea.

Or am I just approaching this wrong? The only other thing I can think of
doing is writing a COM DLL wrapper in VB6 itself (in which I know exactly
how to do this) which builds up the IIS.Form return value when the default
property (Item) is called with no parameters. However I'd really like to
avoid having this second layer of wrappers if I can help it.

My thanks in advance,

--

(O)enone

Nov 20 '05 #7

P: n/a
Doh!

I should add:
' Define conversion from Double to Complex
Public Shared Widening Operator CType(ByVal real As Double) As Complex Widening is implicit, as you are taking a smaller object and putting it into
a larger object
You can take a Integer and safely widen it to a Double
' Define conversion from Complex to Double
Public Shared Narrowing Operator CType(ByVal c As Complex) As Double Narrowing is explicit, as you are taking part of larger object and putting
it into a smaller object, you may loose information.
You may loose the fraction taking a Double and putting it into an Integer.

I haven't decided if I would actually leave this in there: If c.imag <> 0 Then Throw New InvalidCastException
As the Narrowing operator may consider to simply truncate the imaginary part
of the Complex number...

Hope this helps
Jay
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl... Oenone,
Thanks for your response -- that works fine, but it isn't what I'm after. :-) Perhaps I should explain a bit more. I believe I mostly knew what you are after, unfortunately this is what you
get. :-(
I have a large volume of VB6 DLLs which I want to be able to call this

class
thinking that it's the original ASP (not ASP.net) Form object. They
currently all late-bind to the real ASP Form object so providing I can

make
the interface act the same they should all carry on working fine

(otherwise
I have a couple of hundred DLLs that will all need to be rewritten before than can work with my VB.net/ASP.net application, which isn't an option I have available to me).

Are you writing a VB.NET component or a VB6 component? Is it going to be
used in VB.NET, VB6, or VBScript?
Or am I just approaching this wrong?

Your approaching it wrong as Default properties are not supported. Also
VB.NET 2003 has no real support on what you are attempting if VB.NET is

the target (other then adding a "ToWhatever" method).

If you are attempting to write a VB.NET component that exposes a COM default property to VB6, I would recommend asking "down the hall" in the
microsoft.public.dotnet.framework.interop newsgroup. As what you want really deals with assigning dispatch Ids & Interop. I believe you can use the
System.Runtime.InteropServices.DispIdAttribute to do what you want, however I have not actually tried it.

http://msdn.microsoft.com/library/de...classtopic.asp
If you are writing a VB.NET component for use in VB.NET then:
The only other thing I can think of
doing is writing a COM DLL wrapper in VB6 itself (in which I know exactly how to do this) which builds up the IIS.Form return value when the default property (Item) is called with no parameters. However I'd really like to
avoid having this second layer of wrappers if I can help it. The default property in VB6 will not be honored in VB.NET only index
properties can be Default in .NET.
The closest VB.NET to VB.NET you may get is an implicit conversion

operator, which is not available in VB.NET 2003. It will be available in VB.NET 2005
(Whidbey) due out sometime in 2005.
Public Class Complex

Private ReadOnly m_real As Double
Private ReadOnly m_imag As Double

Public Sub New(ByVal real As Double)
MyClass.New(real, 0)
End Sub

Public Sub New(ByVal real As Double, ByVal imag As Double)
m_real = real
m_imag = imag
End Sub

' Define conversion from Double to Complex
Public Shared Widening Operator CType(ByVal real As Double) As Complex
Return New Complex(real)
End Operator

' Define conversion from Complex to Double
Public Shared Narrowing Operator CType(ByVal c As Complex) As Double
If c.imag <> 0 Then Throw New InvalidCastException
Return c.m_real
End Operator

End Class

However be careful with have a narrowing operator from your class to a base type, as you may loose data (narrowing).

Public Shared Narrowing Operator CType(ByVal iis As IIS) As String

However the conversion operator smells like bad code for what you are
asking...

Hope this helps
Jay

"Oenone" <no***@nowhere.com> wrote in message
news:Ok**************@TK2MSFTNGP11.phx.gbl...
Hi Jay,

Try:

? IIS.Form.ToString


Thanks for your response -- that works fine, but it isn't what I'm after. :-) Perhaps I should explain a bit more.

I have a large volume of VB6 DLLs which I want to be able to call this

class
thinking that it's the original ASP (not ASP.net) Form object. They
currently all late-bind to the real ASP Form object so providing I can

make
the interface act the same they should all carry on working fine

(otherwise
I have a couple of hundred DLLs that will all need to be rewritten before than can work with my VB.net/ASP.net application, which isn't an option I have available to me).

There are three ways they can access the ASP Form object:

IIS.Form
returns a complete breakdown of the Form content, e.g.:
Field1=Value1&Field2=Value2&Field3=Value3

IIS.Form("Field1")
returns the value of the named Form field, e.g.:
Value1

IIS.Form.Count
(or other properties/methods), calls the appropriate member code, e.g.:
3

I have the second and third items working but not the first.

I tried defining the Item property so that its parameter (ItemId) was
optional, thinking that might do the trick. But VB told me that

"properties
with no required parameters cannot be declared 'Default'", which put an

end
to that idea.

Or am I just approaching this wrong? The only other thing I can think of
doing is writing a COM DLL wrapper in VB6 itself (in which I know exactly how to do this) which builds up the IIS.Form return value when the default property (Item) is called with no parameters. However I'd really like to
avoid having this second layer of wrappers if I can help it.

My thanks in advance,

--

(O)enone


Nov 20 '05 #8

P: n/a
Hi Jay,
I believe I mostly knew what you are after, unfortunately this is
what you get. :-(
Hehe, no problem. :)
I have a large volume of VB6 DLLs which I want to be able to call
this class thinking that it's the original ASP (not ASP.net) Form
object.

Are you writing a VB.NET component or a VB6 component? Is it going to
be used in VB.NET, VB6, or VBScript?
I'm writing a VB.NET component that I want to be used from both VB6 and
VB.NET.

To clarify, this is part of a larger project that is being upgraded from VB6
to VB.NET. The code exposes the an object called IIS which plug-in DLLs use
(late-bound) to communicate with the IIS server. As the objects have changed
from ASP to ASP.NET I'm just trying to simulate the interface that the old
objects used so that both my existing VB6 DLLs can use it (without any
modification) and also my new .NET plugins.

The new plugins aren't such a concern as they need to be (re)written anyway
so can be adjusted for any changes to the objects exposed via the IIS object
as part of the process. But I'm really keep to be able to continue to use
the existing VB6 plug-in DLLs as rewriting them would be unacceptable for
the project.
If you are attempting to write a VB.NET component that exposes a COM
default property to VB6, I would recommend asking "down the hall" in
the microsoft.public.dotnet.framework.interop newsgroup. As what you
want really deals with assigning dispatch Ids & Interop. I believe
you can use the System.Runtime.InteropServices.DispIdAttribute to do
what you want, however I have not actually tried it.


I will indeed ask there and I'll have a look into the InteropServices to see
if I can spot anything useful in there -- many thanks for the suggestions.

I've also got a fallback plan of writing a VB6 DLL that wraps around the
VB.NET-created IIS object. I have this working perfectly on the VB6 side and
so don't envisage any problems getting it communicating to my VB.NET code
either. This means that I'm not sure I can get a working solution, so it's
just a question now of finding the most efficient and maintainable way of
doing it.

--

(O)enone
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.