locked
passing values to vector RRS feed

  • Question

  • hi all,

    I'm trying to pass a integer value into an vector like below:

    int x = Console.Read();

    .......new Vector (x);

    I know that you can't convert an int to vector but was wondering if there is another approach?

     

    Monday, April 11, 2011 5:23 PM

Answers

  • If you want to pass the variable to a Vector class, you can do:

      class Program
      {
        static void Main(string[] args)
        {
          int x = Convert.ToInt32(Console.ReadLine());
          Vector vec = new Vector(x);
          vec.ShowVector();
          Console.ReadLine();
        }
      }
    
      class Vector
      {
        int v;
        public Vector(int _v)
        {
          this.v = _v;
        }
    
        //use v variable in this class!
        public void ShowVector()
        {
          Console.WriteLine("Vector is {0}.", v);
        }
      }
    


    Mitja
    • Marked as answer by vba123 Tuesday, April 12, 2011 4:39 PM
    Tuesday, April 12, 2011 4:35 PM

All replies

  • Is Vector a custom collection class you are using?
    http://blog.voidnish.com
    Monday, April 11, 2011 5:38 PM
  • What "Vector" type are you using?  A Vector is typically >1 number.

     

    Also, Console.Read() returns the ascii code of the letter typed - not the number typed.  If you want the number, you should use Console.ReadKey() or Console.ReadLine() and then convert to an integer.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, April 11, 2011 5:38 PM
  • Perhaps you just want to use a List<>. Example below:

    int x = Console.Read();
    List<int> ints = new List<int>() { x };


    http://blog.voidnish.com
    Monday, April 11, 2011 5:40 PM
  • Ok. Thank you.  I will try some of these suggestions and let you know.  Thanks again. 
    Tuesday, April 12, 2011 4:29 PM
  • If you want to pass the variable to a Vector class, you can do:

      class Program
      {
        static void Main(string[] args)
        {
          int x = Convert.ToInt32(Console.ReadLine());
          Vector vec = new Vector(x);
          vec.ShowVector();
          Console.ReadLine();
        }
      }
    
      class Vector
      {
        int v;
        public Vector(int _v)
        {
          this.v = _v;
        }
    
        //use v variable in this class!
        public void ShowVector()
        {
          Console.WriteLine("Vector is {0}.", v);
        }
      }
    


    Mitja
    • Marked as answer by vba123 Tuesday, April 12, 2011 4:39 PM
    Tuesday, April 12, 2011 4:35 PM