Solved

VB Equivalent of Working C# Code

Posted on 2006-07-11
7
496 Views
Last Modified: 2010-05-18
HELLO!

I CONVERTED A WORKING C# CODE TO VB BUT I GET THIS ERROR WHEN I COMPILE THE VB CODE:
Compiler Error Message: BC30452: Operator '+' is not defined for types 'String' and 'System.Guid'.

THIS IS THE LINE OF CODE IN VB THAT CAUSES THE ERROR:
Line 33:    Dim vsKey As String = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid()

CAN ANYONE OFFER SOME ASSISTANCE WITH THIS PROBLEM?
THANKS IN ADVANCE!

--------------------------------------------------------------------------
THE FOLLOWING IS THE WORKING CODE IN C#
--------------------------------------------------------------------------
public class BaseApplicationPage : BaseClasses.Web.UI.BasePage
    {
        public const string VIEWSTATE_KEY_PREFIX = "VIEWSTATE#";

        public const string VIEWSTATE_KEY_FIELD_NAME = "__VIEWSTATE_KEY";

        public const string VIEWSTATE_INVALID_ERR_MSG = "Invalid viewstate key: ";

        public BaseApplicationPage()
        {

            base.Load += new System.EventHandler(this.Page_Load);

        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.DataBind();
            }
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {

            //generate unique identifier for page instance's session key

            string vsKey = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid();



            //serialize viewstate to string

            System.Web.UI.LosFormatter formatter = new System.Web.UI.LosFormatter();

            System.IO.StringWriter stateWriter = new System.IO.StringWriter();

            formatter.Serialize(stateWriter, viewState);



            //store serialized object to session

            Session[vsKey] = stateWriter.ToString();


            //store unique session key in hidden field

            //RegisterHiddenField(VIEWSTATE_KEY_FIELD_NAME, vsKey);

            ClientScript.RegisterHiddenField(VIEWSTATE_KEY_FIELD_NAME, vsKey);

        }
        protected override object LoadPageStateFromPersistenceMedium()
        {

            //retrieve unique session key from hidden field

            string vsKey = Request.Form[VIEWSTATE_KEY_FIELD_NAME];

            if (!vsKey.StartsWith(VIEWSTATE_KEY_PREFIX))
            {

                throw new System.Exception(VIEWSTATE_INVALID_ERR_MSG + vsKey);

            }



            //deserialize viewstate from string representation stored in session

            System.Web.UI.LosFormatter formatter = new System.Web.UI.LosFormatter();

            object oSessionValue = Session[vsKey];

            object oViewState = null;

            if (oSessionValue != null)
            {
                oViewState = formatter.Deserialize((string)oSessionValue);
            }
            //return viewstate object

            return oViewState;

        }
    } // End class
--------------------------------------------------------------------------
THE FOLLOWING IS THE COVERTED CODE (VB)
--------------------------------------------------------------------------
Public Class BaseApplicationPage
Inherits BaseClasses.Web.UI.BasePage
 Public Const VIEWSTATE_KEY_PREFIX As String = "VIEWSTATE#"
 Public Const VIEWSTATE_KEY_FIELD_NAME As String = "__VIEWSTATE_KEY"
 Public Const VIEWSTATE_INVALID_ERR_MSG As String = "Invalid viewstate key: "

 Public Sub New()
   AddHandler MyBase.Load, AddressOf Me.Page_Load
 End Sub

 Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
   If Not Me.IsPostBack Then
     Me.DataBind
   End If
 End Sub

 Protected Overloads Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
   Dim vsKey As String = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid
   Dim formatter As System.Web.UI.LosFormatter = New System.Web.UI.LosFormatter
   Dim stateWriter As System.IO.StringWriter = New System.IO.StringWriter
   formatter.Serialize(stateWriter, viewState)
   Session(vsKey) = stateWriter.ToString
   ClientScript.RegisterHiddenField(VIEWSTATE_KEY_FIELD_NAME, vsKey)
 End Sub

 Protected Overloads Overrides Function LoadPageStateFromPersistenceMedium() As Object
   Dim vsKey As String = Request.Form(VIEWSTATE_KEY_FIELD_NAME)
   If Not vsKey.StartsWith(VIEWSTATE_KEY_PREFIX) Then
     Throw New System.Exception(VIEWSTATE_INVALID_ERR_MSG + vsKey)
   End If
   Dim formatter As System.Web.UI.LosFormatter = New System.Web.UI.LosFormatter
   Dim oSessionValue As Object = Session(vsKey)
   Dim oViewState As Object = Nothing
   If Not (oSessionValue Is Nothing) Then
     oViewState = formatter.Deserialize(CType(oSessionValue, String))
   End If
   Return oViewState
 End Function
End Class
0
Comment
Question by:JaimeJegonia
  • 3
  • 2
  • 2
7 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 17080142
Hi JaimeJegonia,
----------

in VB you need to replace the + by a & for string concatenation, that goes for every line which adds to strings together like in the error message
not sure if thats all that will error but it will solve the + sign problem :)

----------
bruintje
0
 
LVL 9

Accepted Solution

by:
lojk earned 500 total points
ID: 17080198
Hmmm... Should probably say...

 Dim vsKey As String = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid.Tostring

as the error says

 Operator '+' is not defined for types 'String' and 'System.Guid'

that means you cant 'add' an object of type string to an object of type GUID, you must convert that GUID to a string first (as above)
0
 

Author Comment

by:JaimeJegonia
ID: 17080212
Hello Bruintje,

I've already tried that and it says this error:
Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and 'System.Guid'.

Line 33:    Dim vsKey As String = VIEWSTATE_KEY_PREFIX & "_" & System.Guid.NewGuid()

I also tried changing the + to & but still not working.

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Expert Comment

by:bruintje
ID: 17080242
with the comment of lojk i think you can solve that problem

convert the Guid to string and use the &

Dim vsKey As String = VIEWSTATE_KEY_PREFIX & "_" & System.Guid.NewGuid().ToString()
0
 

Author Comment

by:JaimeJegonia
ID: 17080301
Thanks for the help guys!

Sorry bruintje but I gave all the points to lojk because I saw your second post only when I already clicked the accept button.

I used lokj's code first and it worked:
Dim vsKey As String = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid.Tostring

And I tried these:
Dim vsKey As String = VIEWSTATE_KEY_PREFIX + "_" + System.Guid.NewGuid().Tostring()
Dim vsKey As String = VIEWSTATE_KEY_PREFIX & "_" & System.Guid.NewGuid.Tostring
Dim vsKey As String = VIEWSTATE_KEY_PREFIX & "_" & System.Guid.NewGuid().Tostring()

And it still works! The only thing that matters is the Tostring.
Thanks again. More Power.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17080346
:)

i learned something new here about that + and & which does matter in VB.NET but apparently not in ASP.NET
0
 
LVL 9

Expert Comment

by:lojk
ID: 17080481
as far as i am aware there is no difference when concatenating strings in vb using & or +, I suspect the duplication is more to do with the standardisation of VB to fit in with the CLR.

Im quite old school VB6 so to me its always been '& TestText & MoreText & ' but then every other language seems to use '+' to concatenate.

In *any* .net language (unless your objects are strings) it is basically '+ TestTextObject.ToString + MoreTextObject.ToString +', the reason for this is held within 'Overloading Operators' in the MSDN ;-) (i.e. the capability to 'change' the '+' or '-' operators to operate directly on classes, allowing you to 'add together' rectangles classes with several properties for example)

Just because an object holds or returns a string value doesnt mean that it is a string object (or that it can be implicity converted to a string). VB will attempt to do some implicit conversion of objects to string (like ints and doubles and so on) but you should always specify what you want from where - assume nothing..

Thanks for the points, happy to help...

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now