Avatar of goodk
goodk
Flag for United States of America asked on

classes passing variables

The following is only an example.  I need to pass several variables when Call myTable.

Is passing variables like this is a better thing or what is a more professional way? thanks


 public static string myTable( string xTable)
        {

               string txTable = MiscStrings.fRQueryString("xqTable");  // returns blank or the variable

if (txTable.Length <1) txTable=xTable

// Use the variable

}
ASP.NETC#

Avatar of undefined
Last Comment
goodk

8/22/2022 - Mon
binaryevo

Its perfectly fine to do it this way or you could also pass in an object as an alternative.  See below:

Object Definition
public class Table
{
       public string TableName{get;set;}
       public string Description{get;set;}
}

Open in new window


Modified Method

public static string myTable(Table xTable){

     //here you can unpack the contents of the object
     string name = xTable.Name;
     string description = xTable.Description;
}

Open in new window


Hope this helps
goodk

ASKER
Can you give a little more detail?

Like how do we use it and how do we set the value? thanks
goodk

ASKER
Is this how it works?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myCLib
{
    public class gVariables
    {
        /* How to use
        gVariables gVar = new gVariables();

        gVar.SetID(1);
        gVar.SetName("Amelio Rosales");

        */
        private int m_id = -1;

        public int GetID()
        {
            return m_id;
        }

        public void SetID(int id)
        {
            m_id = id;
        }

        private string m_name = string.Empty;

        public string GetName()
        {
            return m_name;
        }

        public void SetName(string name)
        {
            m_name = name;
        }

    }
}
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
binaryevo

If your referring to setting the value of the object that your going to pass into the myTable method then you can do it like so:

Table table = new Table();
table.Name = "My Test Name";
table.Description = "My Test Description";
string myTableResult = myTable(table);

Open in new window

goodk

ASKER
so could I continue to define table
table.Name2= "My Test Name2" ??
goodk

ASKER
able table = new Table();
table.Name = "My Test Name";
table.Description = "My Test Description";
table.Description2 = "My Test Description2";
table.Description3 = "My Test Description3";
string myTableResult = myTable(table);


Is above correct?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
goodk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
goodk

ASKER
solution is not understood