Solved

VB Equivalent of Working C# Code

Posted on 2006-07-11
7
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

628 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