Ognacの雑感

木漏れ日々

目次

Blog 利用状況

書庫

ギャラリ

キャストの不便な点

不便と感じる私が変なのかもしれませんが....
VBでのキャストは Ctpeです。
        Dim a As Object = New Integer
        a = 34
        Dim b As Integer = 3
        b = CType(a, Integer)
第二引数は Object of Type が来ます。
Typeとあるから System.Typeと判断して
        Dim a As Object = New Integer
        a = 34
        Dim b As Integer = 3
        Dim T As Type = 3.GetType
        T = Type.GetType("System.Integer")
        b = CType(a, T)
のように、第二引数を変数とする怒られます。 変数で指定することは無理なようです。
ジェネリック指定での
   <of T>
       :;
        b = CType(a, T)
だと当然可能です。
    b  = CType(a, b.gettype) と記述したいのですが、許してくれない。
そこで  Case文を並べて
      select case b.getType
         case  "System.Int16":          b = CType(a,Int16)
         case  "System.Int32":          b = CType(a,Int32)
         case  "System.Int64":          b = CType(a,Int64)
         :::
         :::
      end case
としているのですが、どうも不細工で冗長感が残ります。ジェネリックを使えということでしょうね。
できても良さそうな構文だと思うのですがねぇ。

投稿日時 : 2007年7月24日 10:04

Feedback

# re: キャストの不便な点 2007/07/24 10:36 中博俊

それ以前にdirectcastつかってください。
またどの型かで処理を選択する場合にはtrycastです。
詳細はdirectcastのmsdn見てください。
少なくともvb7からはctypeを使う局面は著しく減っているはずです。

# re: キャストの不便な点 2007/07/24 11:18 Ognac

>それ以前にdirectcastつかってください
directcast()の使用が原則なのは既知なんですが、それだと a=directcast(b,T)で a,bが同じ型なのが前提になります。
dim I as integer =4
dim L as long =45
b= directcast(a,long) '←で実行時にキャストエラーがおこります。
意識的に違う型にキャストしたいのでCTYPE()を使ったりしてます。
strict on の時は
L = I
の上位への暗黙変換もエラーになるので、CTYPE()を使う局面があったりします。 C/C# の (int) などのキャストの動作を意識してます。

うん? これってStrict Onの呪縛に縛られています? 違う言語なので、同等に考えるのは難かもしれません...www)

やりたかったのは 左辺の型が不定の時にキャストしたかったんですが、無理っぽいですね。

# re: キャストの不便な点 2007/07/24 11:41 中博俊

>左辺の型が不定
どういう局面かわからないけど、TryCastでしょう
それよりLong<>Integer変換はCLng, CIntじゃないの?

# re: キャストの不便な点 2007/07/24 12:06 Ognac

Dim S As Short = 0
Dim I As Integer = 0
Dim L As Long = 0
'::
'::
のように異なる型の変数があり(, 同じ値を各々に代入したい場面で)
Dim 元 As Integer = CInt(InputBox("基準値"))
S = 元 'strict on 時に暗黙変換はエラー
S = CShort(元)
I = CInt(元)
L = CLng(元)
と記述するのが正統ですが、汎用的な記述はできないかなっと試行して
S = CType(元, Short)
I = CType(元, Integer)
L = CType(元, Long)
をまず考えたのですが、これだとキーワードの置換でしかないので、無意味
変換先の型は左辺なので左辺の型を右辺で使用できればスッキリした記述ができるのでは..と考えた次第
S = CType(元, S.Gettype)
I = CType(元, I.GetType)
L = CType(元, L.GetType)
これが可能なら左辺の型が変わってもソースを変えずにすむかなぁ..と...www)
そもそもこういう局面を生むような設計が不味いんですよね。

# re: キャストの不便な点 2007/07/24 13:36 まどか

> 変換先の型は左辺なので左辺の型を右辺で使用できれば

これって型推論さえできないので無理ではないでしょうか。
どう転んでも左辺の型がわからないとどうしようもない。
つまり、必ず、Dim 左辺 As 型 と書くはず。
で、左辺と左辺.GetTypeは双方向100%依存しているので、CType(元, 型)と書かざるを得ない。
という感じでしょうか。

依存がまったく無いソースを実現しようとしているように受け取れました。
冷静に考えると、依存ゼロ=コードゼロですよね。>現代の技術では。
#意思を表現できるようになれば。。。

# re: キャストの不便な点 2007/07/24 16:55 Ognac

>どう転んでも左辺の型がわからないとどうしようもない。
>冷静に考えると、依存ゼロ=コードゼロですよね。>現代の技術では。

うっ! そうだ、自己矛盾に陥ってますね。頭山の世界でした。

DIM L as Long =0
DIM T as TYPE = L.getType
dim L8 as long = CTYPE(L,T)

とすれば出来てもよさそうな気になりましたが、錯覚に過ぎないんですね。

# re: キャストの不便な点 2007/07/24 20:59 シャノン

> Dim L8 as T = CTYPE(L,T)

とでも書ければ別ですがね…

# UaDmuFRmftKmdupoAT 2021/07/03 4:57 https://www.blogger.com/profile/060647091882378654

Thanks so much for the blog post. Awesome.

# For the reason that the admin of this web page is working, no doubt very soon it will be famous, due to its quality contents. 2021/07/12 22:17 For the reason that the admin of this web page is

For the reason that the admin of this web page is working,
no doubt very soon it will be famous, due to its quality
contents.

# Hi there! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems finding one? Thanks a lot! 2021/07/18 8:15 Hi there! I know this is kind of off topic but I w

Hi there! I know this is kind of off topic but I was wondering if you
knew where I could get a captcha plugin for
my comment form? I'm using the same blog platform as yours and I'm having
problems finding one? Thanks a lot!

# essay writers block r12gno 2022/09/03 21:13 Charlosmox


Regards, Loads of write ups!
https://definitionessays.com/ writing an analysis essay

# graphing homework d73khe 2023/02/09 7:22 Albertosed


You have made the point!

https://service-essay.com/ paper writings

# dissertation typing services uk e26zpp 2023/02/27 7:49 Robertsaids


Terrific stuff, Regards!
dissertation buy online https://dissertationwritingtops.com/ dissertation editing help

# simple thesis x83yea 2023/03/03 5:45 Josephbried

You actually explained it superbly!
thesis genrator https://writingthesistops.com/ india thesis writing service

# really good college essays z67pec 2023/03/08 7:06 Gregorysaipt


Nicely voiced certainly! !
writing a 5 paragraph essay https://helpmedomyxyzhomework.com best custom writing website https://paperwritingservicecheap.com

# college diversity essay u55xmv 2023/03/10 2:39 Gregorysaipt


Excellent information. With thanks.
how to write an essay for scholarship https://domyhomeworkformecheap.com nyu college application essay https://cheapessaywriteronlineservices.com

# writing analysis essay m292lb 2023/03/10 7:14 EugeneSib


Fine info. With thanks!
phd thesis cover https://helpwithdissertationwriting.com freedom writers summary essay https://buyanessayscheaponline.com

# college essay intro z62rmj 2023/03/10 21:37 EugeneSib


With thanks! I appreciate it.
help 123 essay https://helptowriteanessay.com custom article writing service https://domyhomeworkformecheap.com

# cheap dissertation writing services r18tjp 2023/03/11 0:14 Gregorysaipt


Superb information. Thanks.
thesis examination https://researchproposalforphd.com essay writers service https://customthesiswritingservice.com

# thesis dissertation difference m82ooh 2023/03/11 21:59 Gregorysaipt


Appreciate it. Ample information!
essay on helping others https://buyanessayscheaponline.com does money buy happiness essay https://hireawriterforanessay.com

# help to write an essay a10ybq 2023/03/13 23:43 EugeneSib


With thanks. Quite a lot of tips!
dissertation results https://bestpaperwritingservice.com resume and cover letter writing services https://essaypromaster.com

タイトル
名前
Url
コメント