isn't vb.net supposed to be case insensitive?

J

Jan Nielsen

Hi all
I am learning Asp.Net using Vb.net (VS 2002).
I found the following example in a book:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim count As Integer

If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("Count") = count

Label1.Text = count.ToString()

End Sub



And at first it did not work. The me.viewstate("count") property wasn't
incremented.

I then realized that I have mixed Me.ViewState("count") and
Me.ViewState("Count") . Note the capital C

And the code does not work if I mix capital and non-capital C's.

I can use

If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("Count") = count



or



If Not Me.ViewState("count") Is Nothing Then

count = CInt(Me.ViewState("count")) + 1

End If

Me.ViewState("count") = count



But not



If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("count") = count (Mixed C's)



I thought VB.Net was not case sensitive. Am I wrong?



Best regards



Jan
 
P

Patrice

The VB *language* (i.e. its keywords) is case insensitive.

For string comparison inside your code you can choose if the comparison
should be case insensitive or not.

At last, you can use a collection that uses case sensitive keys (this is
likely what you see here).

Patrice
 
D

DalePres

When you create the ViewState object using only one case, either "Count" or
"count" you can read it with case insensitivity. If you create "Count" then
you can read it as "count" or "Count" or even "COUNT".

As soon as you set a value with a different case, so now you have "count"
and "Count" then it becomes case sensitivity.

This is a Framework behavior and not a VB issue.

Good luck,

DalePres
MCAD, MCDBA, MCSE
 
W

William F. Robertson, Jr.

VB is not case sensitive, but like others have said, the ViewState
collection's keys are case sensitive. The ASP.NET team was nice enough to
make it easy to turn off case sensitivity for the ViewState collection since
that it what seems you are having problems with.

There is an protected virtual (c#) property called ViewStateIgnoresCase that
you can override on your page that will turn off the case sensitive
ViewState collection.

I am unsure about the VB syntax for this, but here is the C# syntax.

protected override bool ViewStateIgnoresCase
{
get { return true; }
}

When the ViewState collection is created, it will be created as a
case-insensitive state bag, and the problem you posted about will go away.

HTH,

bill
 
J

Jan Nielsen

Jan Nielsen said:
Hi all
I am learning Asp.Net using Vb.net (VS 2002).
I found the following example in a book:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim count As Integer

If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("Count") = count

Label1.Text = count.ToString()

End Sub



And at first it did not work. The me.viewstate("count") property wasn't
incremented.

I then realized that I have mixed Me.ViewState("count") and
Me.ViewState("Count") . Note the capital C

And the code does not work if I mix capital and non-capital C's.

I can use

If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("Count") = count



or



If Not Me.ViewState("count") Is Nothing Then

count = CInt(Me.ViewState("count")) + 1

End If

Me.ViewState("count") = count



But not



If Not Me.ViewState("Count") Is Nothing Then

count = CInt(Me.ViewState("Count")) + 1

End If

Me.ViewState("count") = count (Mixed C's)



I thought VB.Net was not case sensitive. Am I wrong?



Best regards



Jan
 
J

Jan Nielsen

Hi William
I tried code like this:
Public Class AutoPostBackForm

Inherits System.Web.UI.Page

......

Dim _viewstate As StateBag

Protected Overrides ReadOnly Property ViewState() As StateBag

Get

Return _viewstate

End Get

End Property

Public Sub New() ' VB constructor

_viewstate = New StateBag(True) ' True indicates that ignorecase is
True

End Sub


And kept the rest of the code as before, but now the counter does not
increment at all.
It seems as a new ViewState object is created every time I click the button
that updates the page because an entirely new Page class is instantiated.
Can you tell me what I am doing wrong?

It seems to me like you are just overriding the ViewState object IgnoreCase
property whereas I am overriding the entire ViewState object. How do you
"get access" to the viewstate property so you can override its IgnoreCase
property?

Jan
 
W

William F. Robertson, Jr.

You should override the Control's ViewStateIgnoresCase Property. This
property is what is used by the control to initializes its ViewState
container. You are correct, you are overriding the ViewState container
itself. There is a little more code that is required when you override the
ViewState accessor.

You want to just override the ViewStateIgnoresCaseProperty and set it to
true.


Public Class AutoPostBackForm
Inherits System.Web.UI.Page

Protected Overrides ReadOnly Property ViewStateIgnoresCase() As Boolean
Get
Return True
End Get
End Property

End Class

For the record, this is what the base ViewState property kinda looks like:

Protected Overridable ReadOnly Property ViewState As StateBag
Get
If (Me._viewState Is Nothing) Then
* Me._viewState = New StateBag(Me.ViewStateIgnoresCase)
If Me.IsTrackingViewState Then
Me._viewState.TrackViewState
End If
End If
Return Me._viewState
End Get
End Property

You will see on the line with the * on it, the control is initializing its
ViewState container with the ViewStateIgnoresCase property you overrode.

HTH,

bill
 
J

Jan Nielsen

Hi Bill
Yes! Now it works.
Thanks a lot for your time and your explanations

Jan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top