在父构造函数中实例化类变量。 [英] Instantiating class variables in parent constructor.

查看:64
本文介绍了在父构造函数中实例化类变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们(加尔斯!),


我有2个班级,TypesafeConstant和颜色。 "颜色"从TypesafeConstant继承

,并且不添加任何新功能。所有颜色

是实例化一些类变量,它们是

" Color"的公共实例。


什么是TypesafeConstant确实是创建一个共享列表,并将每个

声明添加到该列表中。然后,我可以使用

字符串查询该列表(例如Red)以返回Color.Red。


现在,如果我取出继承,并且只有颜色。实例化

本身,一切正常,但继承Parse

函数错误带有NullReferenceException,这是由

引起的TypesafeConstant.list是一个空引用。


我有什么想法可以重新编写它以使其工作吗?


哦,只是要说,我想要使用这种技术很多,因此

继承 - 我不想每次都要重写整个

TypesafeConstant! !


非常感谢,


RB。

************ *代码*******************

公共类TypesafeConstant

私人sShortType


受保护的共享列表为ArrayList


受保护的子新(ByVal sShortType为字符串)

Me.sShortType = sShortType

如果列表什么都没有那么

list =新的ArrayList()

结束如果

list.Add(Me)

End Sub


Public ReadOnly物业价值()作为字符串

获取

返回sShortType

结束获取

结束物业


公共共享函数Parse(ByVal sValue As String)As

TypesafeConstant

Dim o As TypesafeConstant

For Each o在list.ToArray(GetType(TypesafeConstant))

如果o.Value.ToUpper = sValue.ToUpper那么

返回o

结束如果

下一页

结束功能

结束类

公共类颜色

继承TypesafeConstant


公共共享红色为颜色=新颜色(红色)

公共共享绿色颜色=新颜色(绿色)

公共共享BlueAs颜色=新颜色(蓝色)


私有子新(ByVal sShort As String,ByVal sLong As String)

MyBase.New(sShort,sLong)

End Sub

结束班

Hi guys (and gals!),

I''ve got 2 classes, "TypesafeConstant" and "Color". "Color" inherits
from "TypesafeConstant", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".

What "TypesafeConstant" does is to create a shared list, and add every
declaration of itself to that list. I can then query that list with a
string (e.g. "Red") to return Color.Red.

Now, if I take out the inheritance, and just have "Color" instantiating
itself, everything works fine, but with the inheritance the Parse
function errors with a "NullReferenceException", which is caused by
TypesafeConstant.list being a null reference.

Any ideas how I could re-write this to make it work?

Oh, and just to say, I want to use this technique a lot, hence the
inheritance - I don''t want to have to re-write the entire
TypesafeConstant each time!!

Many Thanks,

RB.
************* CODE *******************
Public Class TypesafeConstant
Private sShortType

Protected Shared list As ArrayList

Protected Sub New(ByVal sShortType As String)
Me.sShortType = sShortType
If list Is Nothing Then
list = New ArrayList()
End If
list.Add(Me)
End Sub

Public ReadOnly Property Value() As String
Get
Return sShortType
End Get
End Property

Public Shared Function Parse(ByVal sValue As String) As
TypesafeConstant
Dim o As TypesafeConstant
For Each o In list.ToArray(GetType(TypesafeConstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstant

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")

Private Sub New(ByVal sShort As String, ByVal sLong As String)
MyBase.New(sShort, sLong)
End Sub
End Class

推荐答案

您可以尝试调用您的constructo rs的参数数量正确。

-

特里

" RB"写道:
You might try calling your constructors with the correct number of arguments.
--
Terry
"RB" wrote:

嗨伙计们(和加尔斯!),


我有2个班级,TypesafeConstant和颜色。 "颜色"从TypesafeConstant继承

,并且不添加任何新功能。所有颜色

是实例化一些类变量,它们是

" Color"的公共实例。


什么是TypesafeConstant确实是创建一个共享列表,并将每个

声明添加到该列表中。然后,我可以使用

字符串查询该列表(例如Red)以返回Color.Red。


现在,如果我取出继承,并且只有颜色。实例化

本身,一切正常,但继承Parse

函数错误带有NullReferenceException,这是由

引起的TypesafeConstant.list是一个空引用。


我有什么想法可以重新编写它以使其工作吗?


哦,只是要说,我想要使用这种技术很多,因此

继承 - 我不想每次都要重写整个

TypesafeConstant! !


非常感谢,


RB。


******** *****代码*******************

公共类TypesafeConstant

私人sShortType


受保护的共享列表为ArrayList


受保护的子新(ByVal sShortType为字符串)

Me.sShortType = sShortType
如果列表什么都没有那么

list =新的ArrayList()

结束如果

li st.Add(Me)

End Sub


Public ReadOnly属性值()为字符串

获取

返回sShortType

结束获取

结束财产


公共共享功能解析(ByVal sValue As String)As

TypesafeConstant

Dim o As TypesafeConstant

For each o In list.ToArray(GetType(TypesafeConstant))

如果o .Value.ToUpper = sValue.ToUpper然后

返回o

结束如果

下一页

结束功能

结束类


公共类颜色

继承TypesafeConstant


公共共享红色作为颜色=新颜色(红色)

公共共享GreenAs颜色=新颜色(绿色)

公共共享BlueAs颜色=新颜色(蓝色) )


Private Sub New(B yVal sShort As String,ByVal sLong As String)

MyBase.New(sShort,sLong)

End Sub

End Class
Hi guys (and gals!),

I''ve got 2 classes, "TypesafeConstant" and "Color". "Color" inherits
from "TypesafeConstant", and adds no new functionality. All "Color" does
is to instantiate some class variables which are public instances of
"Color".

What "TypesafeConstant" does is to create a shared list, and add every
declaration of itself to that list. I can then query that list with a
string (e.g. "Red") to return Color.Red.

Now, if I take out the inheritance, and just have "Color" instantiating
itself, everything works fine, but with the inheritance the Parse
function errors with a "NullReferenceException", which is caused by
TypesafeConstant.list being a null reference.

Any ideas how I could re-write this to make it work?

Oh, and just to say, I want to use this technique a lot, hence the
inheritance - I don''t want to have to re-write the entire
TypesafeConstant each time!!

Many Thanks,

RB.
************* CODE *******************
Public Class TypesafeConstant
Private sShortType

Protected Shared list As ArrayList

Protected Sub New(ByVal sShortType As String)
Me.sShortType = sShortType
If list Is Nothing Then
list = New ArrayList()
End If
list.Add(Me)
End Sub

Public ReadOnly Property Value() As String
Get
Return sShortType
End Get
End Property

Public Shared Function Parse(ByVal sValue As String) As
TypesafeConstant
Dim o As TypesafeConstant
For Each o In list.ToArray(GetType(TypesafeConstant))
If o.Value.ToUpper = sValue.ToUpper Then
Return o
End If
Next
End Function
End Class
Public Class Color
Inherits TypesafeConstant

Public Shared Red As Color = New Color("Red")
Public Shared GreenAs Color = New Color("Green")
Public Shared BlueAs Color = New Color("Blue")

Private Sub New(ByVal sShort As String, ByVal sLong As String)
MyBase.New(sShort, sLong)
End Sub
End Class


Terry写道:
Terry wrote:

您可以尝试使用正确数量的参数调用构造函数。
You might try calling your constructors with the correct number of arguments.



对不起,我的不好。我正在尝试简化新闻组的代码。


当然,Color的代码应该是读取


Private Sub New(ByVal sShort As String)

MyBase.New(sShort)

End Sub


干杯,


RB。

Sorry, my bad. I was trying to simplify the code for the newsgroup.

The code for Color should, of course, read

Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Cheers,

RB.


如果你真的展示你希望如何使用颜色类可能会更好。

因为构造函数是私有,你如何创建一个实例?也许颜色

应该有一个共享子初始化,代码如下:

如果列表什么都没有那么

红色=新颜色(红色 ;)

.....等

结束如果

-

Terry

RB写道:
It may be better if you actually show how you hoped to use the color class.
Since the constructor is private, how do you create an instance? Maybe Color
should have a shared sub Initialize with code something like:
If list is nothing then
Red = New Color("Red")
.....etc
end if
--
Terry
"RB" wrote:

Terry写道:
Terry wrote:

您可以尝试使用正确数量的参数调用构造函数。
You might try calling your constructors with the correct number of arguments.



对不起,我的不好。我正在尝试简化新闻组的代码。


当然,Color的代码应该是读取


Private Sub New(ByVal sShort As String)

MyBase.New(sShort)

End Sub


干杯,


RB。


Sorry, my bad. I was trying to simplify the code for the newsgroup.

The code for Color should, of course, read

Private Sub New(ByVal sShort As String)
MyBase.New(sShort)
End Sub

Cheers,

RB.


这篇关于在父构造函数中实例化类变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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