Results 1 to 11 of 11

Thread: Reference to a non-shared member

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Reference to a non-shared member

    what do you mean by this?
    im very new in this thing
    VB Code:
    1. Dim s As String
    2.         s = Format(txtamt.Text, "Standard")
    3. the format has a blue line. Error: Reference to a non-shared member requires an object reference:

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Have a close look at your code again. The code you posted works OK.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    sorry my fault..I imports a class that has a format method.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    If your Format method is not shared, but only Public, you can access it only if you create an instance of your class.
    For example, let's call 'I' the instance and 'MyC' your class

    VB Code:
    1. Dim I as new MyC
    2.  
    3.  Dim s As String
    4.  
    5.         s = I.Format(txtamt.Text, "Standard")
    Live long and prosper (Mr. Spock)

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by mar_zim
    sorry my fault..I imports a class that has a format method.
    I don't think you should use a VB.NET function name as a method name. It is confusing to say the least
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    or you can do this:
    VB Code:
    1. dim s as string
    2. s = s.Format(txtamt.Text, "Standard")

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you are using the Format method of the string object then you don't even need an instance of string, since Format is a shared function.
    VB Code:
    1. Dim s As String=String.Format(txtamt.Text, "Standard")

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Once again the doubt of my ability to understand english....is your class' Format method (Taxes is perfectly right, this choice make a lot of confusion) shared as Edneeis says or it isn't as your error message seems to say?
    I'm very confused. I think it's better if I remain in stand by on this thread!
    Live long and prosper (Mr. Spock)

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Edneeis
    If you are using the Format method of the string object then you don't even need an instance of string, since Format is a shared function.
    VB Code:
    1. Dim s As String=String.Format(txtamt.Text, "Standard")
    For my education please.

    Surely you have created s as an instance of string so why do you say you don't need it?

    Knowing you I expect you will come up with a simple explanation I am missing
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The instance of string (s) in this case is only to hold the result but is not required to use the Format function. Perhaps this is a better example:
    VB Code:
    1. Msgbox(String.Format(txtamt.Text, "Standard"))

    Since Format is a shared method of the String object you don't need to make an instance of string to use the Format method of the string object. Although you may want an instance variable to store the results of the function.

    Also String.Format performs an argument style replace. Here are some examples:
    VB Code:
    1. Dim name As String="Edneeis"
    2. Msgbox(String.Format("Your name is {0}!",name))
    3. 'result: [b]Your name is Edneeis![/b]
    4.  
    5. Msgbox(String.Format("Your name is {0} and today is {1:MM/dd/yy}!",name, Date.Today))
    6. 'result: [b]Your name is Edneeis and today is 08/06/04![/b]
    7. 'notice how I added formatting to the date in the format string
    8.  
    9. Msgbox(String.Format("Your name is {0}!{2}Today is {1:MM/dd/yy}!{2}{2}This is a line down.",name, Date.Today, ControlChars.NewLine))
    10. 'result: [b]Your name is Edneeis!
    11. 'Today is 08/06/04!
    12. '
    13. 'This is a line down.[/b]

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Edneeis,

    Many thanks. My education is proceeding slowly.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width