如何在不声明对象的情况下使用类

发布于 2024-10-28 03:32:35 字数 243 浏览 0 评论 0原文

我正在尝试在 VB .NET 中开发一个类,以便管理存储在数据库中且可由用户编辑的语言全球化。

我需要的是知道我需要声明什么样的类才能在不声明新对象的情况下使用它。例如My.Settings的使用方式。

目标之一是在某些项目中,开发人员导入引用,然后直接访问属性。例如:My.CustomLanguage.GetWord("Hello") 而不声明对象。

这可能吗?如果是的话,最好的解决方法是什么?

谢谢。

I'm trying to develop a class in VB .NET in order to manage a language globalization stored in a database and editable by the user.

What I need is to know what kind of class I need to declare in order to use it without declaring a new object. For example, the way My.Settings is used.

One of the goals is that in some project the developer imports the reference and after that access directly to a property. For example: My.CustomLanguage.GetWord("Hello") without declaring objects.

Is this possible? And if it's what is the best way to aproach it?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2024-11-04 03:32:35

您可以将需要访问的每个属性或方法声明为静态,在 VB 中为“共享”

Shared Sub GetSomething()


MySharedClass.GetSomething()

You can declare every property or method that you need to access as static, in VB, "Shared"

Shared Sub GetSomething()


MySharedClass.GetSomething()
前事休说 2024-11-04 03:32:35

我认为你想要一个只有静态方法和字段的类

I think you want a class with only static methods and fields

枫林﹌晚霞¤ 2024-11-04 03:32:35

你需要的是一个单例类。

声明您的类后,添加与您的类具有相同类型的公共静态属性。
这将有所帮助: http://msdn.microsoft.com/en-us/library /ff650316.aspx

What you need is a singleton class.

After declaring your class add a public static property having the same type as your class.
This will help: http://msdn.microsoft.com/en-us/library/ff650316.aspx

苍风燃霜 2024-11-04 03:32:35

首先创建类,因为

Public Class ClsTemplate
Public Shared Sub GridTemplate(ByVal DataGrid As DataGridView)
    DataGrid.AllowUserToAddRows = False
    DataGrid.AllowUserToDeleteRows = False
    DataGrid.AllowUserToOrderColumns = False
    DataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGrid.BorderStyle = BorderStyle.FixedSingle
    DataGrid.BackgroundColor = Color.White
    DataGrid.DefaultCellStyle.BackColor = Color.LightYellow
    DataGrid.AlternatingRowsDefaultCellStyle = Nothing
    DataGrid.ColumnHeadersDefaultCellStyle.BackColor = Color.Aqua
    DataGrid.ColumnHeadersHeight = 20
    DataGrid.ColumnHeadersHeightSizeMode =   DataGridViewColumnHeadersHeightSizeMode.EnableResizing
    DataGrid.EditMode = DataGridViewEditMode.EditProgrammatically
    DataGrid.MultiSelect = False
    DataGrid.ReadOnly = True
    DataGrid.RowHeadersVisible = False
    DataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DataGrid.StandardTab = True
End Sub
End Class

您可以通过表单中的代码直接调用此类

 ClsTemplate.GridTemplate(DataGridView1)

First Make Class as

Public Class ClsTemplate
Public Shared Sub GridTemplate(ByVal DataGrid As DataGridView)
    DataGrid.AllowUserToAddRows = False
    DataGrid.AllowUserToDeleteRows = False
    DataGrid.AllowUserToOrderColumns = False
    DataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGrid.BorderStyle = BorderStyle.FixedSingle
    DataGrid.BackgroundColor = Color.White
    DataGrid.DefaultCellStyle.BackColor = Color.LightYellow
    DataGrid.AlternatingRowsDefaultCellStyle = Nothing
    DataGrid.ColumnHeadersDefaultCellStyle.BackColor = Color.Aqua
    DataGrid.ColumnHeadersHeight = 20
    DataGrid.ColumnHeadersHeightSizeMode =   DataGridViewColumnHeadersHeightSizeMode.EnableResizing
    DataGrid.EditMode = DataGridViewEditMode.EditProgrammatically
    DataGrid.MultiSelect = False
    DataGrid.ReadOnly = True
    DataGrid.RowHeadersVisible = False
    DataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DataGrid.StandardTab = True
End Sub
End Class

you can call this class direct by this code in your form

 ClsTemplate.GridTemplate(DataGridView1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文