locked
What is the correct to return a structure from a function RRS feed

  • Question

  • User357616875 posted

    Say I do this

    Public Function CheckWebCamVF() As BusinessProfilesBO.VFCheck

    checkwebcamVF.isactive = true

    return checkwebcamVF

    End Function

    ------or------------------- is this right 

    Public Function CheckWebCamVF() As BusinessProfilesBO.VFCheck

    dim example as new BusinessProfilesBO.VFCheck

    example.isactive = true

    return example

    End Function

    I seem to be getting null referance errors using the first one.

    I am trying to get my code as fast and as correct as possible

    Thanks for you help 

    Graham



    Saturday, November 20, 2010 8:12 PM

Answers

  • User-660870441 posted

    What is the correct to return a structure from a function

    Hi,

    If the structure already exists in the class and we want to use it next time, we can just return it. Otherwise, we always create a new structure and then return it. Here is a very simple sample of using structure.

    Public Class ClassA
    	Private _StructA As StructA
    	Public Function Method1() As StructA
    		Return _StructA
    	End Function
    	Public Function Method2() As StructA
    		Dim result As New StructA()
    		result.Field = "content"
    		Return result
    	End Function
    End Class
    Public Structure StructA
    	Public Field As String
    End Structure

    I hope this helps you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 24, 2010 2:21 AM

All replies

  • User197322208 posted

    I seem to be getting null referance errors using the first one.

    checkwebcamVF.isactive = true

    And who is checkwebcamVF ?

    I am trying to get my code as fast and as correct as possible

    Please make it first correct. Then make some tests to ensure functionality. Then re-factor to see the speed - and re-run tests.

    Sunday, November 21, 2010 12:15 AM
  • User357616875 posted

    Hi I have a function in a class. The class is written to check if what folders are available in a particular business profile.

    In this example the check was to see if the business profile can display a folder page about webcams.

    This is the DAL layer, and I did not put in all the SQL to make the post shorted.

    What I want to know is with the with statement is it correct to use function name as it is defined

    as the structure or do you need to create another instance of the structure and return that ??

    I dont know if this is correct or not, this is why I posted

    I noticed in some example the function is used to set the strucutre and in some cases they use the NEW keyword to set a new instance of the structure and return that

    Graham


    Sunday, November 21, 2010 5:51 AM
  • User-660870441 posted

    What is the correct to return a structure from a function

    Hi,

    If the structure already exists in the class and we want to use it next time, we can just return it. Otherwise, we always create a new structure and then return it. Here is a very simple sample of using structure.

    Public Class ClassA
    	Private _StructA As StructA
    	Public Function Method1() As StructA
    		Return _StructA
    	End Function
    	Public Function Method2() As StructA
    		Dim result As New StructA()
    		result.Field = "content"
    		Return result
    	End Function
    End Class
    Public Structure StructA
    	Public Field As String
    End Structure

    I hope this helps you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 24, 2010 2:21 AM