Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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!
0
MIKEV
Asked:
MIKEV
2 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now