-5

A question about naming local variables in a C# method that takes a parameter of the same name Please see the code below

 private int DoSomething(string activationCode)
    {
        ...

        int ??WhatNameToChooseHere?? = Convert.ToInt32(activationCode);

        ...
    }

What could be a good strategy to apply in the above scenario

Note: method paramter and local variable only differ by type only

Venu b
  • 428
  • 1
  • 3
  • 9
  • 2
    If `activationCode` is always a valid `Int32` why not make the method only accept it as `Int32` and in places you need the actual `String` use `activationCode.ToString()`? You can even make an overload of `DoSomething` that accepts a `String`, converts that to an `Int32` and calls the other `DoSomething`. – Corak Apr 06 '13 at 05:01
  • A few projects I worked on used the convention `code` is a text, `id` is an integer number (`int`, `long`, `uint`, `ulong`). So we probably would've called it `activationId`. Don't know if that's a *good* convention, though, or if it should be applied here. Like I said, if it's an `int`, treat it like an `int`. – Corak Apr 06 '13 at 05:15

2 Answers2

1

You cannot do that. A proper way of doing it is to name your variables with different names.

Alan
  • 7,875
  • 1
  • 28
  • 48
  • 3
    The OP knows that he can't do it. He's asking what to do instead... – Jason Watkins Apr 06 '13 at 05:06
  • 1
    @JasonWatkins I believe this is Alan's suggestion for what to do instead: "name your variables with different names". – Adam Lear Apr 06 '13 at 05:58
  • 1
    @JasonWatkins The OP modified his question, he originally had named his local variable and parameter the same exact thing and asked how to make it work. It is not my fault my answer doesn't match so well after he changed his question. – Alan Apr 07 '13 at 00:01
  • No, I read your answer before the OP edited his question. To me, it was quite clear that the OP knew he couldn't actually name them the same thing. – Jason Watkins Apr 07 '13 at 01:46
  • @JasonWatkins You're mistaken "method paramter and local variable only differ by type only". Check the revision history. It is not possible, and my answer tells him that he must name them differently. This is correct. – Alan Apr 07 '13 at 06:40
0
private int DoSomething(string activationCodeStoredInStringAndPassedAsAnArgument)    {
    int activationCodeThatDeclaredAsAnIntegerToStoreTheValueConvertedFromString = Convert.ToInt32(activationCodeStoredInStringAndPassedAsAnArgument);
}
Ken Kin
  • 4,503
  • 3
  • 38
  • 76