473,399 Members | 3,603 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,399 software developers and data experts.

Creating an object that can be treated as a string?

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
8 3248
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Iain Bishop | last post by:
I'm trying to model objects for the following problem: A building site contains assemblies, each of which can contain other assemblies and/or materials. I have modelled this using a Site...
3
by: Edward Diener | last post by:
I am creating a component and I want one of my properties to be an embedded class with its own properties. When the component designer shows this property I want it to be able to expand this...
2
by: CoolPint | last post by:
Standard exception classes in C++ have what() which returns const char * and they have constructors accepting string. Where is that string created and when is the string destroyed? In the...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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
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...

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.