Passing values new to vb.net

Hi all,

Just trying to get my head around vb.net, done some reading about classes and have a play now.

i have a question, i have this code below, i want it to display the strg string. i can get it to display the strSetupMessage message  by saying Console.WriteLine(SetupGo.strSetupMessage) but for some reason it will not display the other class if i say (SetupGo.strg).

Just having a play and want to know, thank in advance for you help.
 

Module Module1
    Sub Main()
        Dim SetupGo As New Setup


        Dim strtest As String

        Console.WriteLine(SetupGo.strg)
        Console.ReadLine()



    End Sub

End Module

-----

Public Class Setup
    Public strSetupMessage As String
    Public strg As String



    Sub New()

        ' set the defaults:
        strSetupMessage = "White"



       
     
    End Sub



    Sub New(ByVal Value As String)
        strg = "hello"
        strg = Value
    End Sub


End Class
LVL 12
pigmentartsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pigmentartsAuthor Commented:
Sorry for got to say, i know how to get it to display text, but i want to understand why i can not display the other string in the class

  Sub New(ByVal Value As String)
        strg = "hello"
        strg = Value
    End Sub

when i call it in the module.

sorry if this is a easy question but we all have to start somewhere  
0
ptakjaCommented:
If I understand you right, you want the strg to be displayed. With the code you have, strg is never initialized, which is why nothing comes out. Just because you have 2 New methods (subs) doesn't mean one supercedes the other. You need to explicity call one when you create your class:


Module Module1
    Sub Main()
        Dim SetupGo As New Setup("Hi There")   '<<<< This calls your 2nd New method (aka "Constructor")

        Dim strtest As String

        Console.WriteLine(SetupGo.strg)
        Console.ReadLine()

    End Sub

End Module


In this example I am passing the string "Hi There" into your Setup class when I instantiate the class. VB.NET looks at the argument signature (the number of arguments and their data types) and figures out that you want to call the one that accepts 1 string argument.

Now, when you go to write the value by executing

Console.WriteLine(SetupGo.strg)

This will retrieve the value.  Notice though that you set strg = "hello" and then you set it equal to Value which is the value I  passed it.  You will never see the string "hello", unless you pass it in.

Hope that helps.


0
koruzicaCommented:
This is becouse you never initalise that string.

You are using a following constructor:

Dim SetupGo As New Setup

for constructing your class. and there all you set is strg and strSetupMessage stays uninitalised.

So if you create your class this way:

Dim SetupGo As New Setup("someText")

you would have strSetupMessage available but not strg !

If you have any other questions, please ask.

Simon
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ptakjaCommented:
Oh, one other thing, you need to make your New sub's Public or you won't be able to see them...

Public  Sub New()
        ' set the defaults:
        strSetupMessage = "White"
        strg = "New() Called with no arguments"
End Sub

Public Sub New(ByVal Value As String)
        strg = Value
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.