You must Sign In to post a response.
  • Category: ASP.NET

    What is c# equivalent to DirectCast in VB..?

    Dim intListPrice As Integer = 0
    Dim rowList As New Collection
    Dim intCoutner As Integer = 1

    intListPrice = intListPrice + DirectCast(rowList(intcolCounter), Object())(10)

    What is meaning of above code and what is equivalent code in c#?
  • #761609
    your code is not correct and throws an exception. ('System.IndexOutOfRangeException')
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #761615
    Hey

    Your lines I will make in to c# language as below

    int intListPrice = 0;
    Collection rowList = new Collection();
    int intCoutner = 1;

    intListPrice = intListPrice + (object[])rowList(intcolCounter)(10);

    Regards

    Sridhar Thota.
    If you learn from defeat..
    You havent really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761782
    Hi

    If you take an object of unknown type and try
    to cast it to the desired type, then it is the same
    as C#'s () cast:
    VB:
    Dim o As Object = SomeObject()
    Dim i As Integer = DirectCast(o, Integer)
    C#:
    object o = SomeObject();
    int i = (int)o;

    This is because, if o is typed as a System.Object,
    then the () operation in C# will attempt to unbox
    it. This will fail if the types don't match exactly;
    for instance, if o is a boxed System.Double,
    then (int)o will throw an exception because o must be unboxed as a System.Double before it can be
    converted to a System.Int32

    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761784
    Hi
    KLS

    You can go through below CODE



    int intListPrice = 0;
    Collection rowList = new Collection();
    int intCoutner = 1;

    intListPrice = intListPrice + (object[])rowList(intcolCounter)(10);


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments