Solved

VB Equivalent of Working C# Code

Posted on 2006-07-11
7
498 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Project Software to help the Developer 7 33
VB.Net - KeyPress Event 4 36
cannot connect to sqlserver 8 25
Expression Evaluater 3 24
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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