创建新的“我” [英] Create new "me"

查看:46
本文介绍了创建新的“我”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




而不是这样....

公共类Form1

公共共享子CreateAndShow()

Dim f As New Form1

f.Show()

End Sub

End Class


我想通过做类似的事情来使它变得通用......


公共类Form1

公共共享子CreateAndShow( )

Dim f As New Me

f.Show()

End Sub

End Class

....但是我不接受,也不是Dim f As New TypeOf(Me)。


我想把CreateAndShow放在一个基本形式然后继承

它仍然可以使用CreateAndShow方法。


有什么建议吗?坦克! :o)


MOJO

解决方案

这是一个好主意,所以我尝试解决它练习。好的

新闻是有效的。


坏消息是我已经学习.NET大约一周了,所以我'' m

相当尴尬地告诉你我是怎么做到的。但是这里是:


Dim s1 As String = Me.ToString

''s1的格式为WindowsApplication1.Form1,Text:Form1

''现在删除那个不是类名的部分。

Dim i1 As Integer = InStr(s1,",")

如果i1 0那么s1 = Mid(s1,1,i1-1)

''注意:不能使用上面的左(s1,i1-1) - 由于某种原因VB认为

我正试图

''访问Me.Left而不是! < grrr>

Dim objNewForm As Object =

Activator.CreateInstance(Type.GetType(s1))

Dim frm As Form = DirectCast (objNewForm,表格)

frm.Show()


好的,我还做了更多,这个版本是远没那么尴尬了!

享受:


DirectCast(Activator.CreateInstance(Type.GetType(M e.GetType.ToString)),

表格。显示()


te * *****@hotmail.com 写道:


如果i1 0则s1 = Mid(s1,1,i1-1)

''注意:上面不能使用Left(s1,i1-1) - 出于某种原因,VB认为

我正在尝试

''访问Me.Left! <哎呀>



您可以使用以下行:


进口VB = Microsoft.VisualBasic

$ b代码前的$ b,以便您可以使用VB.Left(s1,i1-1)。这告诉你

你想用VB作为Microsoft.VisualBasic的缩写。


另外,如果你使用Option Strict On,它会告诉你当事情不匹配

他们应该的方式。


HTH


Andrew


Hi,

Instead of doing this....
Public Class Form1
Public Shared Sub CreateAndShow()
Dim f As New Form1
f.Show()
End Sub
End Class

I would like to make it generic by doing something like...

Public Class Form1
Public Shared Sub CreateAndShow()
Dim f As New Me
f.Show()
End Sub
End Class

.... but "Me" is not accepted, neither is "Dim f As New TypeOf(Me)".

I would like to put the CreateAndShow in a base form and then inherit from
it and still be able to use the CreateAndShow method.

Any suggestions? TANKS!!! :o)

M O J O

解决方案

That''s a good idea, so I tried solving it as an exercise. The good
news is that it works.

The bad news is that I''ve been learning .NET for about a week, so I''m
rather embarassed to show you how I did it. But here goes:

Dim s1 As String = Me.ToString
'' s1 is in the format "WindowsApplication1.Form1, Text: Form1"
'' Now remove the part that isn''t the class name.
Dim i1 As Integer = InStr(s1, ",")
If i1 0 Then s1 = Mid(s1, 1, i1 - 1)
'' Note: Can''t use Left(s1, i1-1) above - for some reason VB thinks
I''m trying to
'' access Me.Left instead! <grrr>
Dim objNewForm As Object =
Activator.CreateInstance(Type.GetType(s1))
Dim frm As Form = DirectCast(objNewForm, Form)
frm.Show()


Ok, I worked on it some more, and this version is far less embarrasing!
Enjoy:

DirectCast(Activator.CreateInstance(Type.GetType(M e.GetType.ToString)),
Form).Show()


te******@hotmail.com wrote:

If i1 0 Then s1 = Mid(s1, 1, i1 - 1)
'' Note: Can''t use Left(s1, i1-1) above - for some reason VB thinks
I''m trying to
'' access Me.Left instead! <grrr>

You can use the line:

Imports VB = Microsoft.VisualBasic

before your code so that you can use VB.Left(s1, i1-1). That tells it that
you want to use VB as an abbreviation for Microsoft.VisualBasic.

Also, if you use Option Strict On, it will tell you when things don''t match
up the way they should.

HTH

Andrew


这篇关于创建新的“我”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆