C# variable declaring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dragas
    New Member
    • Aug 2008
    • 4

    C# variable declaring

    Ok my quiestion is about varibles something like in VFox:

    string variable;
    for(int i=0; i=10 ; i++)
    {
    variable = "mem"+i.SoStrin g();
    // now part i need some solution in fox would be like this &variable
    string &variable;

    }
    mem2 = "Somthing.. .";

    So question is easy way to declare variable from data in other variable....

    Thx
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I have no idea what the goal you are trying to achieve is. Are you trying to concatenate strings? If so, you can use the += operator:
    Code:
    string s = "something ";
    string t = "else";
    s += t;
    //now s contains "something else"
    If this doesn't answer your question, please clarify.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      1.) Careful with your typing. ToString != SoString and
      for(int i =0; i=10;i++) != for(int i = 0; i<10;i++)
      2.) Do not use + to concatenate strings in a loop. Use a StringBuilder instead.

      Comment

      • artov
        New Member
        • Jul 2008
        • 40

        #4
        C# is not interpreted language, so you cannot create variables at the runtime.
        However, you might like to check reflection, where you can compile and load
        code at the runtime. I have used it in creating classes and instatiating objects
        from the created classes and then calling methods from it.

        Comment

        • Dragas
          New Member
          • Aug 2008
          • 4

          #5
          Ok sorry for bad typing but problem is:

          string var1 = "var2";

          So var1 have name of my new variable i want to declare

          // code i have problem //
          var2 = "It works now";


          Thx for replys

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Dragas
            Ok sorry for bad typing but problem is:

            string var1 = "var2";

            So var1 have name of my new variable i want to declare

            // code i have problem //
            var2 = "It works now";


            Thx for replys
            Not possible.
            Why do you want to do that?

            Comment

            • Dragas
              New Member
              • Aug 2008
              • 4

              #7
              Well have some bookkeeping prog which is writen in VFOX as client,
              we migrating to C# and porting most code.

              In VFox we used alot &var1 (macro sign) which do that what i want.

              All forms, classes ... are in tables, so i from tables take all i need
              and making forms, toolbars, menus on fly and rest things ...

              So that would come handy but if cant cant.

              Anyway thx all for replys

              Comment

              • cloud255
                Recognized Expert Contributor
                • Jun 2008
                • 427

                #8
                Originally posted by Dragas
                string var1 = "var2";

                So var1 have name of my new variable i want to declare

                // code i have problem //
                var2 = "It works now";
                This will cause a compiler error, something along the lines of "use of unasigned variable" i think.
                What you have done is create a string (var1) which has a value of "var2" . That part is ok. but now you have a line saying "var2 =" the compiler has no idea what a var2 is...
                You need to declare var2 first.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  If you want to be able to reference variables by a string name, take a look at some of the collections. Like a Dictionary<stri ng,object>.

                  [code=c#]
                  Dictionary<stri ng, object> myd = new Dictionary<stri ng, object>();
                  myd.Add("var1", "var2");
                  myd.Add("var2", "it works now");
                  myd.Add("var3", 12);//this just shows you can add other datatypes
                  string mys =myd[myd["var1"].ToString()].ToString();
                  //mys will now be "this works now"
                  [/code]

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by Plater
                    If you want to be able to reference variables by a string name, take a look at some of the collections. Like a Dictionary<stri ng,object>.

                    [code=c#]
                    Dictionary<stri ng, object> myd = new Dictionary<stri ng, object>();
                    myd.Add("var1", "var2");
                    myd.Add("var2", "it works now");
                    myd.Add("var3", 12);//this just shows you can add other datatypes
                    string mys =myd[myd["var1"].ToString()].ToString();
                    //mys will now be "this works now"
                    [/code]
                    If you need to be able to instanciate classes from strings like you mentioned in a table, you will need to to use System.Reflecti on, or start to re-think your code plan.

                    Comment

                    • cloud255
                      Recognized Expert Contributor
                      • Jun 2008
                      • 427

                      #11
                      Originally posted by Plater
                      If you want to be able to reference variables by a string name, take a look at some of the collections. Like a Dictionary<stri ng,object>.

                      [code=c#]
                      Dictionary<stri ng, object> myd = new Dictionary<stri ng, object>();
                      myd.Add("var1", "var2");
                      myd.Add("var2", "it works now");
                      myd.Add("var3", 12);//this just shows you can add other datatypes
                      string mys =myd[myd["var1"].ToString()].ToString();
                      //mys will now be "this works now"
                      [/code]

                      hey there,

                      could you provide some explanation on this please?

                      the way i read it is that you are creating an objec "var2" as a value for the key "var1". how is the dictionary then aware that "var2" is a key and not just some random string?

                      thanks in advance

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Originally posted by cloud255
                        hey there,

                        could you provide some explanation on this please?

                        the way i read it is that you are creating an objec "var2" as a value for the key "var1". how is the dictionary then aware that "var2" is a key and not just some random string?

                        thanks in advance
                        Dictionary.Add( key,value)
                        And since my Dictionary defined the key to be of type string and the value of type object, my Add function is
                        Dictionary.Add( string key, object value)

                        Comment

                        • cloud255
                          Recognized Expert Contributor
                          • Jun 2008
                          • 427

                          #13
                          Originally posted by Plater
                          Dictionary.Add( key,value)
                          And since my Dictionary defined the key to be of type string and the value of type object, my Add function is
                          Dictionary.Add( string key, object value)

                          yeah this is what i dont get, how is the dictionary aware that the object "var2" is actaully a key within itself and not something else,
                          doesn't the dictionary see "var1" as an object with a set string value of "var2".
                          sorry if i sound stupid, but i just dont see how this works...

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            (Ignoring the integer) I added two values to my dictionary. One value called "var1" and one value called "var2" with the given values.
                            As far as the dictionary is concerned, I have done this:
                            [code=c#]
                            object var1="var2";
                            object var2="it works now";
                            [/code]

                            And since Dictionary[key] for me is Dictionary[string key], all I need to do is provide a key name in the form of a string and I can get the value.
                            I shortened up my code in the example.

                            It could be expanded out a little to this:
                            [code=c#]
                            Dictionary<stri ng, object> myd = new Dictionary<stri ng, object>();
                            myd.Add("var1", "var2");
                            myd.Add("var2", "it works now");
                            string akeyname=myd["var1"].ToString();
                            string mys =myd[akeyname].ToString();
                            //mys will now be "this works now"
                            [/code]

                            Comment

                            • cloud255
                              Recognized Expert Contributor
                              • Jun 2008
                              • 427

                              #15
                              Originally posted by Plater
                              (Ignoring the integer) I added two values to my dictionary. One value called "var1" and one value called "var2" with the given values.
                              As far as the dictionary is concerned, I have done this:
                              [code=c#]
                              object var1="var2";
                              object var2="it works now";
                              [/code]

                              And since Dictionary[key] for me is Dictionary[string key], all I need to do is provide a key name in the form of a string and I can get the value.
                              I shortened up my code in the example.

                              It could be expanded out a little to this:
                              [code=c#]
                              Dictionary<stri ng, object> myd = new Dictionary<stri ng, object>();
                              myd.Add("var1", "var2");
                              myd.Add("var2", "it works now");
                              string akeyname=myd["var1"].ToString();
                              string mys =myd[akeyname].ToString();
                              //mys will now be "this works now"
                              [/code]

                              ah i see,

                              its actaully a pretty nice trick this.

                              thank you, for the patience and the explanation

                              Comment

                              Working...