-6

I just picked up learning C# less than a week ago. It's all I've been doing in my spare time, so I've made some nice progress on my own. But there's one question I can't seem to find an answer to through Google or any other site.

What I have written is something like the following:

vname = class1.itemName;
vval = class1.Value;
vamt = class1.Amount;
vbool = class1.isThisActive;

This repeats a great many times, but if I wanted to change the name of class1, I have to go in and change all of the lines (there are a lot more than four in my actual code, and this takes some time)

Would it at all be possible to do the following without running into errors?

vclass = class1;

vname = vclass.itemName; //returns the same as class1.itemName, ditto for all examples below
vval = vclass.Value;
vamt = vclass.Amount;
vbool = vclass.isThisActive;

This way I only need to have the actual cs document referenced once instead of a bunch of times. Keep in mind that all of the attributes of class1 (itemName, Value, Amount) need to be static, so I can't reference them through a public class variable at the top of the document.

Any help is greatly appreciated.

Ryan L
  • 3
  • 1
  • 2
    Why are you copying all of those values to local variables anyways? And if you need to rename something, your IDE (preferably Visual Studio...) can find all the references to a variable and allow you to do a safe rename. – mason Oct 24 '19 at 23:18
  • Both Visual Studio and VS Code support refactoring in the form of renaming classes and variables. Use that feature. It's not entirely clear what you need that for though... you don't typically rename a class a large number of times. Sounds like an XY problem. – Herohtar Oct 24 '19 at 23:19
  • IMHO This question is not so bad if OP is newbie. You are looking for `Refactoring/Rename` function like comments above. – daremachine Oct 24 '19 at 23:23
  • If you really want to do this you can use [type alias](https://learn.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/using-directive) `using vclass = MyNamespace.class1;` – Kalten Oct 24 '19 at 23:31
  • i guess i'm just too variable happy. i wanted something very dynamic that i didn't have to CTRL+F every time i wanted to change it. @Kalten's namespace suggestion is a good start but that looks a little complicated from my perspective, so i'd have to mess around with that. thanks for your advice. – Ryan L Oct 24 '19 at 23:42
  • If all you want to do is rename something, just use the rename feature built in to your IDE. It's very simple, and there is documentation for [Visual Studio](https://learn.microsoft.com/en-us/visualstudio/ide/reference/rename?view=vs-2019) and [VS Code](https://code.visualstudio.com/docs/editor/refactoring#_rename-symbol). (You *are* using one of those, right?) – Herohtar Oct 24 '19 at 23:48
  • However, that aside, you should probably look more into how to properly structure your code. Having a lot of static members on a class and copying them into other local variables indicates something that isn't really the best way to do things. And having to rename a class should be a somewhat rare occurence. – Herohtar Oct 24 '19 at 23:51
  • yes, yes. i suppose i was just overthinking a simple solution. – Ryan L Oct 24 '19 at 23:51
  • @AlexeiLevenkov nah, if i do the above in my IDE, it returns a CS0118 error, saying that it's a "type" and it expected a variable. i'm not sure what i'm supposed to be declaring vclass as though, string doesn't quite seem to work here and i know it's probably not supposed to. don't quite know what term to use. reading might help me a lot, so i'll look into that recommendation. – Ryan L Oct 25 '19 at 00:04
  • @RyanL sorry - I missed your (unusual) requirement that everything must be static... Providing good [MCVE] usually helps. The answer is https://stackoverflow.com/questions/244246/how-do-i-alias-a-class-name-in-c-without-having-to-add-a-line-of-code-to-every (also it is very likely you are going down wrong way of using classes). – Alexei Levenkov Oct 25 '19 at 00:08
  • @AlexeiLevenkov thank you for the resources. yeah, still trying to wrap my head around how classes work. just spent about 10-12 hours on saturday learning how int/float/double work. i don't think it *has* to be static, but if it isn't, the script doesn't update when i change the variables for some reason. ie public static value = 5 sets itself to 5 on launch, but just public value will equal the first value its given on that and all subsequent launches even if i write public value = 500 in the code, it will still treat value like it equals 5. slowly working out the kinks! – Ryan L Oct 25 '19 at 00:32
  • @Kalten After playing around with the using/namespace/alias commands, I found how to use that to work exactly how I wanted it. So thank you for that! If you want to post that as an answer, I will give you the green check mark. – Ryan L Oct 25 '19 at 02:01
  • Welcome to StackOverflow. Thank you for taking the time to share your problem. But there is something missing from your question. ***What is your goal and your difficulty?*** What have you done so far? Please try to better explain your issue, your dev. env. and the data structures, ***as well as to share more code*** (no screenshot), some samples, images or sketches of the screen, and user stories or scenario diagrams. To help you improve your requests, please read the *[How do I ask a good question](https://stackoverflow.com/help/how-to-ask)* and **Questions I avoid asking** at the top right. –  Oct 25 '19 at 03:12

1 Answers1

0

It's perfectly normal to have static variables in a non-static class. However, you should know that the value of these variables are shared among all instances in the class so it's pointless to access them through an instance. The simplest and correct way to approach your situation is to use Refactoring to rename your class, or you can use a type alias as suggested. What you want exactly, can't be done, as long as you work with the static variables. If you didn't, a possible (ugly) workaround is through the reference semantics of classes, this will work for your non-static data. For this, assume that your variables itemName, Value, Amount, and isThisActive are not static, then you can use the snippet

class1 cl1 = new class1();
class1 vclass = new class1();
vclass = cl1;

vname = vclass.itemName;
vval = vclass.Value;
vamt = vclass.Amount;
vbool = vclass.isThisActive

Note, however, that any changes to vclass will also change cl1 because both reference the same instance of class1. Hopefully by now you've realized that is better to do it correctly as stated above and to stay away from this kind of bad hacks. Use the tools for what they're meant and use the language to code, both have different purposes.

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21