Solved

VB Equivalent of Working C# Code

Posted on 2006-07-11
7
499 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

820 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