locked
How to set a variable from within a function RRS feed

  • Question

  • User249648003 posted

    I am trying to set the value of a global variable from within a function that I am defining as follows:

    Dim X as string

    Protected Function GMap1_ServerEvent(ByVal s As Object, ByVal e As GAjaxServerEventOtherArgs) As String

    xxxxxxxx

    X="Y"

    End Function

    Is this possible and if so how?

    Wednesday, October 21, 2009 10:57 PM

Answers

  • User-952121411 posted

    I am trying to set the value of a global variable
     

    If you are speaking of setting a global value within VB.NET for an ASP.NET web application (which it should be since this is the forum we are on), then you could change your declaration to the following:

    Public X as string

    The 'Private' keyword in the declaration statement specifies that the elements can be accessed only from within the same module, class, or structure.

    The 'Dim' keyword in the declaration statement specifies that the element can onlty be accessed within the scope of the individual method for which it was defined.

    Now, it goes beyond this for ASP.NET.  If you want this global value to persist postbacks on the web, then you will need to save the value off in something like a session variable.  As you probably know, the web has no state, which means each user interaction with the server (button click, page redirect, etc) is independent of the last call, and for one values in varibales (public or private) are not maintained.  If you need to persist your value from postbacks, then you could add your value to session as well and access it later as needed.  Take a look below:

    Public x As String
    
    '......code, etc
    
    X="Y"
    
    'Store the value of the variable 'X' in a new session variable named 'MyValue'
    Session.Add("MyValue", X)
    

     

    Then later on when needed, you can pull this value back out of session:

    'Pull the value from the 'MyValue' session variable and place it in the Public variable 'X'
    'Cast value to appropriate data type; in this case a String
    X = DirectCast(Session("MyValue"), String)


    If you need more information on saving values in Session state, take a look to the following:

    ASP.NET Session State:

    http://msdn.microsoft.com/en-us/library/ms972429.aspx

    For more information on Public, Private, etc. take a look to the following:

    Access Levels in Visual Basic:

    http://msdn.microsoft.com/en-us/library/76453kax.aspx

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2009 8:43 AM

All replies

  • User-678059101 posted

    declare the X variable as Public. then your problem may solve.


    Marked as 'Answer' if above tip works.


    Regards

    CVPrasad



    Thursday, October 22, 2009 5:35 AM
  • User-952121411 posted

    I am trying to set the value of a global variable
     

    If you are speaking of setting a global value within VB.NET for an ASP.NET web application (which it should be since this is the forum we are on), then you could change your declaration to the following:

    Public X as string

    The 'Private' keyword in the declaration statement specifies that the elements can be accessed only from within the same module, class, or structure.

    The 'Dim' keyword in the declaration statement specifies that the element can onlty be accessed within the scope of the individual method for which it was defined.

    Now, it goes beyond this for ASP.NET.  If you want this global value to persist postbacks on the web, then you will need to save the value off in something like a session variable.  As you probably know, the web has no state, which means each user interaction with the server (button click, page redirect, etc) is independent of the last call, and for one values in varibales (public or private) are not maintained.  If you need to persist your value from postbacks, then you could add your value to session as well and access it later as needed.  Take a look below:

    Public x As String
    
    '......code, etc
    
    X="Y"
    
    'Store the value of the variable 'X' in a new session variable named 'MyValue'
    Session.Add("MyValue", X)
    

     

    Then later on when needed, you can pull this value back out of session:

    'Pull the value from the 'MyValue' session variable and place it in the Public variable 'X'
    'Cast value to appropriate data type; in this case a String
    X = DirectCast(Session("MyValue"), String)


    If you need more information on saving values in Session state, take a look to the following:

    ASP.NET Session State:

    http://msdn.microsoft.com/en-us/library/ms972429.aspx

    For more information on Public, Private, etc. take a look to the following:

    Access Levels in Visual Basic:

    http://msdn.microsoft.com/en-us/library/76453kax.aspx

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2009 8:43 AM
  • User397347636 posted

    "Dim" just means "this is a variable declaration".  It can be omitted and usually is omitted for class-level fields.

    When used by itself at the class level, the implicit access modifier "Private" is assumed.  When used by itself at the structure level, the implicit access modifier "Public" is assumed.

    e.g., the following are all 'Private' class fields:

    Private Foo As String

    Private Dim Foo As String

    Dim Foo As String  ' usually this form is only used for method-level variables, but is also valid for class-level fields

     

    Thursday, October 22, 2009 10:50 AM
  • User249648003 posted

    Thanks for the help but this still does not work. I admit that I am not a very knowledgeable programmer. I think a function is supposed to return a string value, but I am not sure if I can do anything else in the function.

    Here is my function:

     

    Public Function GMap1_MarkerClick(ByVal s As Object, ByVal e As GAjaxServerEventArgs) As String

    Return String.Format("alert('MarkerClick: {0}')", e.point.ToString())

    Session.Add("slat", e.point.lat.ToString())

    Session.Add("slng", e.point.lng.ToString())

    End Function

    I am trying to retrieve e.point.lat.ToString() and e.point.lng.ToString() on the on my DetailsView1_ItemInserting event like this:

    e.Values("ForcedLat") = DirectCast(Session("slat"), String)

    e.Values("ForcedLng") = DirectCast(Session("slng"), String)

    I am using the above alert function to make sure the my MarkerClick function is firing, and it is. The client side event that I am trying to capture is the movement of a marker on a google map.

     

     

     


    Thursday, October 22, 2009 10:50 PM
  • User249648003 posted

    I reversed the order of statements in my function and it worked!

    Thank you. 

    Friday, October 23, 2009 1:47 AM