Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Equivalent of Working C# Code

Posted on 2006-07-11
7
Medium Priority
?
507 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with 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.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

715 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