将属性构造为字符串而不是(例如)整数的Dis / Advant [英] Dis/Advantages of Constructing Properties as Strings Rather than (e.g) Integer

查看:54
本文介绍了将属性构造为字符串而不是(例如)整数的Dis / Advant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,很抱歉,如果我不知道要问的正确问题,但我已经尝试了各种谷歌,代码项目和MSDN搜索。



我一直在学习VB.Net(VS2010),并且遇到了几个演练,它们展示了构建各种类的属性的非常不同的方法。事实上,在这一篇演练中,作者使用了两种方法进行课堂建设而没有明显的解释。



http://msdn.microsoft.com/en-AU/library/ms171892%28v=vs.100%29.aspx [ ^ ]



这是客户类,他只使用字符串:



Firstly, sorry if I don't know the right question to ask but I've tried various google, Code Project and MSDN searches on this one.

I have been learning VB.Net (VS2010) and have come across a couple of walk-throughs that show very different ways of constructing Properties of various Classes. Indeed in this one walk-through the author uses two approaches to class construction without obvious explanation.

http://msdn.microsoft.com/en-AU/library/ms171892%28v=vs.100%29.aspx[^]

That is for the "Customer" class he uses only strings:

Public Sub New(ByVal customerId As String,
                   ByVal companyName As String,
                   '...
                   ByVal phone As String,
                   ByVal fax As String)
        customerIDValue = customerId
        '... country
        phoneValue = phone
        faxValue = fax
    End Sub





对于Order类,他使用反映数据库字段的实际变量类型:





For the "Order" Class he uses the actual variable type that reflects the database fields:

Public Sub New(ByVal orderid As Integer,
                   ByVal customerID As String,
                 '...
        shipCountryValue = shipCountry
    End Sub



我可以看到像这样的建筑可以节省一些麻烦,例如日期。但是,有人可以帮助解释为什么你会使用一种方法而不是另一种方法。



即使是关于如何构建关于该主题的谷歌/ MSDN搜索的指导也将不胜感激。


I can see that genericising the construction like that could save some hassles with, for example, dates. However, can someone help explain why you would use one approach over another.

Even guidance on how to frame a google/ MSDN search on the topic would be appreciated.

推荐答案

ThePhantomUpvoter是对的(我引用他对问题的评论):这不是一个有效的问题,代码示例与属性无关。但是我想提出一个一般性的注意事项:现在,对于初学者来说,一个绝对的谬误变得非常典型:字符串被视为一种通用数据类型,因此人们尝试使用表示数据的字符串而不是数据本身。即使在有效的情况下,这也是一件非常非常糟糕的事情。它实际上意味着对编程的一般误解。



在你需要整数类型时,使用整数等等。



-SA
ThePhantomUpvoter is right (I refer to his comment to the question): this is not a valid question, and the code sample is irrelevant to properties. But I wan to make one general note: these days, one absolute fallacy became very typical for the beginners: a string is taken as some universal data type, so people try to work with strings representing data, instead of data itself. This is a really, really bad thing, even in the cases when it works. It actually means deep misunderstanding of programming in general.

In you need integer type, use integer, and so on.

—SA


首先,不要通过添加某些解决方案来更新您的问题。



我没有在C#或VB.NET世界中花费大量时间,但我对代码有点困惑。构造函数不一致。它在C#和VB.NET版本之间似乎并不相同......但无论如何,我认为数据类型是它们的方式,因为它们是有意义的(正如你已经在这里听到的那样)。 customerID是我想的字符串,因为它可以包含字符,但orderid是int。电话号码是字符串,因为(1)它们可能需要超过10位数字和(2)它们有时可能包含非数字字符。



您可能可以重写自己的数字例如,这个版本带有一个数字customerID,并没有引起任何人的注意。所有这些似乎只是设计决定(并不罕见)。



P.S.我真的不喜欢VB.NET,认为它是一种不足的语言。如果您自己作为一个随意的决定学习它,我建议使用更现代的语言,如C#,Java或C ++。
First of all, don't update your question by adding something as a solution.

I have not spent a lot of time in either of the C# or VB.NET worlds, but am a bit confused with the code. The constructors are not consistent. It doesn't seem to be the same between the C# and VB.NET versions... but anyway, I think the datatypes are the way they are because they make sense (as you've heard here already). The customerID is a string I suppose because it can contain characters, but the orderid is an int. Phone numbers are string because (1) they can require more than 10 digits and (2) They can sometimes contain non-numeric characters.

You could probably rewrite your own version of this with a numeric customerID for example and not raise any eyebrows. All of this just seems to be design decisions (not unusual).

P.S. I really dislike VB.NET and consider it a deficient language. If you are learning it on your own as an arbitrary decision, I'd suggest a more modern language such as C#, Java or C++.


我的意思是(并且已经改变) - 实际数据库中使用的变量类型和相关的TableAdapter。



我很抱歉说'声明'而不是'构造'(我确实提到我是学习)。但是,我假设,因为这是一个MSDN演练,而不是一些初学者,可能有正当理由可以采用这种通用方法来构建属性。
I meant (and have changed) - the actual variable type as used in the database and the associated TableAdapter.

I'm sorry for saying 'declare' instead of 'construct' (I did mention that I'm learning). However, I had assumed that, since this was an MSDN walk-through, and not some beginner, that there might be valid reasons why one might take such a generic approach to constructing the properties.


这篇关于将属性构造为字符串而不是(例如)整数的Dis / Advant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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