Converting usage of VB6 err object to C#












0















Below is my vb6 code how can i convert this into c#, Actually i'm getting confusion in that handling of err in if condition.



Function FilterDuplicates(Arr As Variant) As Long
Dim col As Collection, Index As Long, dups As Long
Set col = New Collection
On Error Resume Next
For Index = LBound(Arr) To UBound(Arr)
col.Add 0, CStr(Arr(Index))
If err Then
Arr(Index) = Empty
dups = dups + 1
err.Clear
ElseIf dups Then
Arr(Index - dups) = Arr(Index)
Arr(Index) = Empty
End If
Next
FilterDuplicates = dups End Function


Below is my C# Code which i tried and here actually i'm unable to handle this If condition part.



        private long FilterDuplicates(string Arr)
{
Collection col = new Collection();
long Index=0;
long dups =0;
try
{
for (Index = Arr.GetLowerBound(0); Index <= Arr.GetUpperBound(0); Index++)
{
col.Add(0, Conversion.Str(Arr[Index]));
if (Information.Err)
{
Arr[Index] = null;
dups += 1;
err.Clear;
}
else if (dups != 0)
{
Arr[Index - dups] = Arr[Index];
Arr[Index] = null;
}
}
return dups;
}
catch
{

}
}









share|improve this question

























  • i have uploaded my C# code also please have a loop of that @JohnB

    – Mohan
    Nov 15 '18 at 6:37











  • It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

    – John
    Nov 15 '18 at 6:38











  • sure you can handle an error and log it or whatever, then use the contine.

    – JohnB
    Nov 15 '18 at 6:40













  • I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

    – Mohan
    Nov 15 '18 at 6:43






  • 1





    What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

    – Dmitry Bychenko
    Nov 15 '18 at 7:17
















0















Below is my vb6 code how can i convert this into c#, Actually i'm getting confusion in that handling of err in if condition.



Function FilterDuplicates(Arr As Variant) As Long
Dim col As Collection, Index As Long, dups As Long
Set col = New Collection
On Error Resume Next
For Index = LBound(Arr) To UBound(Arr)
col.Add 0, CStr(Arr(Index))
If err Then
Arr(Index) = Empty
dups = dups + 1
err.Clear
ElseIf dups Then
Arr(Index - dups) = Arr(Index)
Arr(Index) = Empty
End If
Next
FilterDuplicates = dups End Function


Below is my C# Code which i tried and here actually i'm unable to handle this If condition part.



        private long FilterDuplicates(string Arr)
{
Collection col = new Collection();
long Index=0;
long dups =0;
try
{
for (Index = Arr.GetLowerBound(0); Index <= Arr.GetUpperBound(0); Index++)
{
col.Add(0, Conversion.Str(Arr[Index]));
if (Information.Err)
{
Arr[Index] = null;
dups += 1;
err.Clear;
}
else if (dups != 0)
{
Arr[Index - dups] = Arr[Index];
Arr[Index] = null;
}
}
return dups;
}
catch
{

}
}









share|improve this question

























  • i have uploaded my C# code also please have a loop of that @JohnB

    – Mohan
    Nov 15 '18 at 6:37











  • It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

    – John
    Nov 15 '18 at 6:38











  • sure you can handle an error and log it or whatever, then use the contine.

    – JohnB
    Nov 15 '18 at 6:40













  • I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

    – Mohan
    Nov 15 '18 at 6:43






  • 1





    What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

    – Dmitry Bychenko
    Nov 15 '18 at 7:17














0












0








0








Below is my vb6 code how can i convert this into c#, Actually i'm getting confusion in that handling of err in if condition.



Function FilterDuplicates(Arr As Variant) As Long
Dim col As Collection, Index As Long, dups As Long
Set col = New Collection
On Error Resume Next
For Index = LBound(Arr) To UBound(Arr)
col.Add 0, CStr(Arr(Index))
If err Then
Arr(Index) = Empty
dups = dups + 1
err.Clear
ElseIf dups Then
Arr(Index - dups) = Arr(Index)
Arr(Index) = Empty
End If
Next
FilterDuplicates = dups End Function


Below is my C# Code which i tried and here actually i'm unable to handle this If condition part.



        private long FilterDuplicates(string Arr)
{
Collection col = new Collection();
long Index=0;
long dups =0;
try
{
for (Index = Arr.GetLowerBound(0); Index <= Arr.GetUpperBound(0); Index++)
{
col.Add(0, Conversion.Str(Arr[Index]));
if (Information.Err)
{
Arr[Index] = null;
dups += 1;
err.Clear;
}
else if (dups != 0)
{
Arr[Index - dups] = Arr[Index];
Arr[Index] = null;
}
}
return dups;
}
catch
{

}
}









share|improve this question
















Below is my vb6 code how can i convert this into c#, Actually i'm getting confusion in that handling of err in if condition.



Function FilterDuplicates(Arr As Variant) As Long
Dim col As Collection, Index As Long, dups As Long
Set col = New Collection
On Error Resume Next
For Index = LBound(Arr) To UBound(Arr)
col.Add 0, CStr(Arr(Index))
If err Then
Arr(Index) = Empty
dups = dups + 1
err.Clear
ElseIf dups Then
Arr(Index - dups) = Arr(Index)
Arr(Index) = Empty
End If
Next
FilterDuplicates = dups End Function


Below is my C# Code which i tried and here actually i'm unable to handle this If condition part.



        private long FilterDuplicates(string Arr)
{
Collection col = new Collection();
long Index=0;
long dups =0;
try
{
for (Index = Arr.GetLowerBound(0); Index <= Arr.GetUpperBound(0); Index++)
{
col.Add(0, Conversion.Str(Arr[Index]));
if (Information.Err)
{
Arr[Index] = null;
dups += 1;
err.Clear;
}
else if (dups != 0)
{
Arr[Index - dups] = Arr[Index];
Arr[Index] = null;
}
}
return dups;
}
catch
{

}
}






c# vb6 migration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:16









DaveInCaz

3,27631940




3,27631940










asked Nov 15 '18 at 6:29









MohanMohan

217




217













  • i have uploaded my C# code also please have a loop of that @JohnB

    – Mohan
    Nov 15 '18 at 6:37











  • It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

    – John
    Nov 15 '18 at 6:38











  • sure you can handle an error and log it or whatever, then use the contine.

    – JohnB
    Nov 15 '18 at 6:40













  • I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

    – Mohan
    Nov 15 '18 at 6:43






  • 1





    What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

    – Dmitry Bychenko
    Nov 15 '18 at 7:17



















  • i have uploaded my C# code also please have a loop of that @JohnB

    – Mohan
    Nov 15 '18 at 6:37











  • It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

    – John
    Nov 15 '18 at 6:38











  • sure you can handle an error and log it or whatever, then use the contine.

    – JohnB
    Nov 15 '18 at 6:40













  • I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

    – Mohan
    Nov 15 '18 at 6:43






  • 1





    What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

    – Dmitry Bychenko
    Nov 15 '18 at 7:17

















i have uploaded my C# code also please have a loop of that @JohnB

– Mohan
Nov 15 '18 at 6:37





i have uploaded my C# code also please have a loop of that @JohnB

– Mohan
Nov 15 '18 at 6:37













It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

– John
Nov 15 '18 at 6:38





It's usually best to be specific about the exception types you intend to catch. See the docs and more docs for some examples.

– John
Nov 15 '18 at 6:38













sure you can handle an error and log it or whatever, then use the contine.

– JohnB
Nov 15 '18 at 6:40







sure you can handle an error and log it or whatever, then use the contine.

– JohnB
Nov 15 '18 at 6:40















I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

– Mohan
Nov 15 '18 at 6:43





I don't know how to handle the exception with a for loop, actually i want to increase the count of dups and have to make a error handler clear so it will run until for loop

– Mohan
Nov 15 '18 at 6:43




1




1





What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

– Dmitry Bychenko
Nov 15 '18 at 7:17





What is the initial routine suppose to do? Often it's by far easier to implement a method than convert it: e.g. to count duplicates Arr.Length() - Arr.Distinct().Count();, get rid of duplicates: Arr.Distinct.ToArray(); etc.

– Dmitry Bychenko
Nov 15 '18 at 7:17












1 Answer
1






active

oldest

votes


















2














Often it's easier to implement the routine than convert it. It seems, you want




  1. Change each duplcate (i.e. second, third etc value) into null

  2. Count all such changes


If it's your case, you can try



 private static long FilterDuplicates(string Arr) {
HashSet<string> appeared = new HashSet<string>();

long result = 0;

for (int i = 0; i < Arr.Length; ++i)
if (!appeared.Add(Arr[i])) {
result += 1;

Arr[i] = null;
}

return result;
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313645%2fconverting-usage-of-vb6-err-object-to-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Often it's easier to implement the routine than convert it. It seems, you want




    1. Change each duplcate (i.e. second, third etc value) into null

    2. Count all such changes


    If it's your case, you can try



     private static long FilterDuplicates(string Arr) {
    HashSet<string> appeared = new HashSet<string>();

    long result = 0;

    for (int i = 0; i < Arr.Length; ++i)
    if (!appeared.Add(Arr[i])) {
    result += 1;

    Arr[i] = null;
    }

    return result;
    }





    share|improve this answer






























      2














      Often it's easier to implement the routine than convert it. It seems, you want




      1. Change each duplcate (i.e. second, third etc value) into null

      2. Count all such changes


      If it's your case, you can try



       private static long FilterDuplicates(string Arr) {
      HashSet<string> appeared = new HashSet<string>();

      long result = 0;

      for (int i = 0; i < Arr.Length; ++i)
      if (!appeared.Add(Arr[i])) {
      result += 1;

      Arr[i] = null;
      }

      return result;
      }





      share|improve this answer




























        2












        2








        2







        Often it's easier to implement the routine than convert it. It seems, you want




        1. Change each duplcate (i.e. second, third etc value) into null

        2. Count all such changes


        If it's your case, you can try



         private static long FilterDuplicates(string Arr) {
        HashSet<string> appeared = new HashSet<string>();

        long result = 0;

        for (int i = 0; i < Arr.Length; ++i)
        if (!appeared.Add(Arr[i])) {
        result += 1;

        Arr[i] = null;
        }

        return result;
        }





        share|improve this answer















        Often it's easier to implement the routine than convert it. It seems, you want




        1. Change each duplcate (i.e. second, third etc value) into null

        2. Count all such changes


        If it's your case, you can try



         private static long FilterDuplicates(string Arr) {
        HashSet<string> appeared = new HashSet<string>();

        long result = 0;

        for (int i = 0; i < Arr.Length; ++i)
        if (!appeared.Add(Arr[i])) {
        result += 1;

        Arr[i] = null;
        }

        return result;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 8:20

























        answered Nov 15 '18 at 7:20









        Dmitry BychenkoDmitry Bychenko

        109k1093133




        109k1093133
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313645%2fconverting-usage-of-vb6-err-object-to-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python