Solved

using Classes

Posted on 2009-05-11
13
127 Views
Last Modified: 2012-05-06
Is it possible to use the public string in a class in aspx pages without declaring a class in the aspx page?

I am trying to have less lines of code

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How can I write the above in one line only?
0
Comment
Question by:goodk
  • 7
  • 6
13 Comments
 
LVL 12

Expert Comment

by:rgn2121
ID: 24361074
You could create your AceClasses to take a string parameter when you declare and instantiate it by overloading your New()..
I don't use a lot of C#, but the New() could look something lke what is below.

Public Sub New( byval s as string)
 

verify 's' and set it equal to the string in your class...
 

End Sub
 

'''Then you could delare it by doing this...If GetConString returns a string...

dim aClass as New AceClasses(GetConnString())

Open in new window

0
 

Author Comment

by:goodk
ID: 24364553
so how it is one line?
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24370983
In VB, your one line is the :
Dim aClass as New AceClasses(GetConnString())

The only way I know to make it one line is if your NEW() constructor takes a parameter.  If it doesn't then I don't think there is anything you can do.  That's why I said to modify your class so that you can declare and instantiate your class in one line as well as provide the connection string.
 
0
 

Author Comment

by:goodk
ID: 24381647
Sorry I am not familiar with vb, I use c#.

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How would I combine the above into one line
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381945
Following what I have stated about overloading your New() constructor, you would be able to use the following code:
AceClasses aClass = new AceClasses(GetConnString());  
 If not...since GetConnString() is a method in your AceClasses class then you have to declare a variable as AceClasses to be able to use the GetConnString().   Which would mean your code is as good as it gets...although not ideal.
Unless, you have created the GetConnString as shared (I think this is static in C#)...then you could use code similar to the line below.
AceClasses aClass = AceClasses.GetConnString();  
This is assuming your GetConnString() is similar to what is below:
VB:
Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 C# by way of a converter.   http://www.developerfusion.com/tools/convert/vb-to-csharp/

Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381953
Sorry...That last section of code should have been in C#..
public static string GetConnString()
 {    
//Code to get the connection string
}
 
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:goodk
ID: 24384492
AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

so you are saying I could write the above as

AceClasses connString = new AceClasses(GetConnString());

??
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391860
Unless I am completely misunderstanding, you are trying to delare a class of type AceClasses correct?
I assumed that you were trying to get a connection string to use with the class you just created.  If that is not the case, then I am confused by what you are doing.  
The top section of code in the window below is declaring aClass as an AceClasses Type.  Then you are declaring a string and setting it equal to the string returned by the AceClasses function GetConnString()..
The bottom section of code is totally different...  In it you are declaring connString as type AceClasses and setting it equal to the string returned by GetConnString();  I doubt that you want to or you are able to set your AceClasses object equal to a string.

AceClasses aClass = new AceClasses();

        string connString = aClass.GetConnString();
 
 
 

AceClasses connString = new AceClasses(GetConnString());

Open in new window

0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391877
I know nothing about AceClasses, but I would assume you have created a class called AceClasses.  In this class there is a function that returns a string called GetConnString().  Is this correct?
If the above is true, then my next question is do you need to declare an object of type AceClasses or do you just want to get the string returned by GetConnString() into a different string variable?
0
 

Author Comment

by:goodk
ID: 24392062
first paragrph -- YES

second paragraph -- True, all I want is move the return string into a local variable.  Is it possible to do it in one line. If possible, how?

0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 500 total points
ID: 24392392
Yes, but your GetConnString() will have to be declared as Static so that you don't have to create an instance of AceClasses to use it.
Like I stated above your code for GetConnString() would have to look something like what I have below:

public static string GetConnString()
{    
//Code to get the connection string
}
If it does, then you should be able to do the following:
 string connString = AceClasses.GetConnString();

Like I stated early...I really don't use C#, but I think this code works.  I use this all the time in VB.  I have numerous classes that have "Shared" members.  This way I don't have to create an instance of the class to use the functions, properties, methods, etc that it contains.  The "Static" keyword is used in C#...
If you use private or class level variables inside of GetConnString, then those will need to be declared Static as well or you will get an error about access to a Static member...
0
 

Author Comment

by:goodk
ID: 24395713
works, thanks - I think the trick was to declare the string as static.  I do not have a need to manupulate the string externally anyway.

thanks again - bye
0
 

Author Closing Comment

by:goodk
ID: 31580395
thanks.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web Modal Window 1 75
About Wordpress website 3 102
What xml editor to use. 8 84
What is the best software for scanning a website and then create a website map? 2 105
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now