Re-Using class names...

This is a dumb little question but is there any particular problem with re-using class instance names?  Example:

Dim MyClass1 as New Class1
Class1.DoStomeStuff()
Class1 = Nothing

RunSmoreCodeAndWhatever()

Dim MyClass1 as New Class1
class1.DoStomeOtherStuff()
class1 = Nothing

The reason I ask is that in VB.Net, as above, you'll notice autocomplete doesn't capitalize anything after the second instantiation with the same name (class1), leading me to believe this may not be a good thing to do.

Is there any problem with this method or is VB.Net just being picky?
Thanks!
MIKEVAsked:
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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can't declare MyClass1 again in the same block of code.  This will result in a "Local variable 'MyClass1' is already declared in the current block" error.

The reason Intellisense isn't working is because you are using the name of the class (Class1) instead of the name of your declared variable (MyClass1).  So your code:

    Dim MyClass1 as New Class1
    Class1.DoStomeStuff()
    Class1 = Nothing

should actually be:

    Dim MyClass1 as New Class1
    MyClass1.DoStomeStuff()
    MyClass1 = Nothing

The only things that will show up using this syntax:

    Class1.Something()

are those things declared as Shared in Class1.  Shared things in a class do not require an instance of the class:

    Public Class Class1

        Public Shared Sub DoSharedStuff()
            ' Some Shared Stuff..
            MsgBox("No instance of Class1 required to call DoSharedStuff()")
        End Sub

        Public Sub DoSomeStuff()
            ' Some Stuff...
            MsgBox("DoSomeStuff()")
        End Sub

        Public Sub DoSomeOtherStuff()
            ' Some Other Stuff...
            MsgBox("DoSomeOtherStuff()")
        End Sub
   
    End Class

The following code demonstrates that DoSharedStuff() can be called by using the class name directly without an instance of Class1 being created.  The other subs must be invoked by using an instance of Class1 created with the New keyword:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Class1.DoSharedStuff()

        Dim MyClass1 As New Class1
        MyClass1.DoSomeStuff()
        MyClass1.DoSomeOtherStuff()
        myClass1 = Nothing
    End Sub

Hope this clears things up a bit,

Idle_Mind
0
Arthur_WoodCommented:
change your code like this:
FROM:
Dim MyClass1 as New Class1
Class1.DoStomeStuff()
Class1 = Nothing

RunSmoreCodeAndWhatever()

Dim MyClass1 as New Class1
class1.DoStomeOtherStuff()
class1 = Nothing


to:

Dim MyClass1 as New Class1
MyClass1.DoStomeStuff()
MyClass1 = Nothing

RunSmoreCodeAndWhatever()

MyClass1 = New Class1
MyClass1.DoStomeOtherStuff()
MyClass1 = Nothing

as wriiten, you were NOT making any use of the MyClass1 object that you were creating.

AW
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
MIKEVAuthor Commented:
First, I'm a dumbass.  My example should have read:

Dim MyClass1 as New Class1
MyClass1.DoStomeStuff()
MyClass1 = Nothing

RunSmoreCodeAndWhatever()

Dim MyClass1 as New Class1
myclass1.DoStomeOtherStuff()
myclass1 = Nothing

You're both right, that's dumb, my fault.  Let me re-ask. :)  I'm re-using the name MyClass1 in the same block.  Is that a problem as long as the previous instance has been set to Nothing?

Sorry for the confusion, I've been looking at this stuff too long today.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Joe_GriffithCommented:
Arthur_Wood is correct.. this may make it a little clearer:

Dim MyObject as Class1          ' <-- one dim statement (without the New key word)

MyObject = New Class1          ' <-- new object
MyObject .DoStomeStuff()
MyObject = Nothing

RunSmoreCodeAndWhatever()

MyObject = New Class1          ' <-- new object
MyObject .DoStomeOtherStuff()
MyObject = Nothing
0
MIKEVAuthor Commented:
Oh!  I get it!  Sorry Arthur, that went right by me.  That's what I was looking for.  Thanks! :)
0
Arthur_WoodCommented:
glad to be of assistance.

AW
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.